CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Temperature Measurement with NTC Thermistors

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GiG



Joined: 03 Sep 2021
Posts: 26

View user's profile Send private message

Temperature Measurement with NTC Thermistors
PostPosted: Sat Oct 16, 2021 12:01 am     Reply with quote

Hello everyone,
I wanted to measure the temperature in Celsius with a 2k thermistor.
Does anyone have an idea?
The measurement range is between 0 and 45 degrees
-Maybe a sample code can be a good Help! Wink
Ttelmah



Joined: 11 Mar 2010
Posts: 17468

View user's profile Send private message

PostPosted: Sat Oct 16, 2021 3:16 am     Reply with quote

There is example code with the compiler. This is for the E3 mini board,
with a KY-013 temperature sensor module plugged in. The Ky-013 has
a 10K resistor and a thermistor.
This uses a function 'thermistor_convert', which is supplied in the library
thermistor.c, and if you look at this it describes how to use it. You need the
rated resistance of the thermistor at 25C, and the beta of your thermistor.
If you don't have this, the formulae to calculate beta for a thermistor is
online.
For good accurate readings, it is much better if you use a proper Vref
source for the ADC, and the same voltage fed to the divider from the
thermistor.
temtronic



Joined: 01 Jul 2010
Posts: 8219
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 16, 2021 5:16 am     Reply with quote

As Mr. T says to get accurate results ,you need a stable Vref for the thermistor. Depending on the PIC you use, it may have one, or you use an external 'precision voltage reference 'chip'.The VDD of a PIC will change depending on what the PIC is doing. Turning on an LED will drop VDD for sure ! That then changes the vref and affects the 'math'.
In the past I've used thermistors in constant current mode.You need to make a bridge from 4 resistors, feed into an amplifier, then to the ADC input pin, It will give beter results(more accurate), if designed properly but costs more in parts.
You'll also have to filter the PSU as well as the sensor pin in order to get good,repeatable 10 bit ADC results.
Another 'trick' to get good readings is to take several and average them. Using Olympic averaging, you take 10 readings, delete the highest and lowest, then average the remaining 8. It's fast and gets rid of 'bad' (out of range) readings. You should never rely on just one reading for control purposes. If there's an EMI event, that could give you a really,really bad result !
GiG



Joined: 03 Sep 2021
Posts: 26

View user's profile Send private message

PostPosted: Sat Oct 16, 2021 11:37 pm     Reply with quote

ok thank you helpful
but any sample code ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21505

View user's profile Send private message

PostPosted: Sun Oct 17, 2021 12:59 am     Reply with quote

Ttelmah told you where to get it.

This file has the thermistor_convert() function:
Quote:
c:\...\picc\drivers\thermistor.c

This file has code that shows how to call the function:
Quote:

c:\...\picc\examples\ex_sk_temp_thermistor.c
GiG



Joined: 03 Sep 2021
Posts: 26

View user's profile Send private message

PostPosted: Sun Oct 17, 2021 2:13 am     Reply with quote

PCM programmer wrote:
Ttelmah told you where to get it.

This file has the thermistor_convert() function:
Quote:
c:\...\picc\drivers\thermistor.c

This file has code that shows how to call the function:
Quote:

c:\...\picc\examples\ex_sk_temp_thermistor.c



i dont have this file ex_sk_temp_thermistor.c
can u upload it for me ?
Ttelmah



Joined: 11 Mar 2010
Posts: 17468

View user's profile Send private message

PostPosted: Sun Oct 17, 2021 3:22 am     Reply with quote

The compiler comes with that file.
No, we cannot upload it, it is CCS copyright.
If you have bought the compiler, then talk to CCS and ask why you do not
have the examples.
temtronic



Joined: 01 Jul 2010
Posts: 8219
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Oct 17, 2021 5:16 am     Reply with quote

As Mr. T says, contact CCS.
If you have an old version of the compiler, they can send you JUST the examples folder of files, once they confirm your information (compiler version, serial number, date bought, etc.).
What's odd is that you have the driver, and I'd assume, the example program would have been in the same version of compiler package.
Ttelmah



Joined: 11 Mar 2010
Posts: 17468

View user's profile Send private message

PostPosted: Sun Oct 17, 2021 7:45 am     Reply with quote

The thermistor example was added about 2016. I think mid 5.06xish.
Given it doesn't involve anything too mind blowing, I'd expect CCS to be
OK supplying it if he has an older compiler than this. The actual thermistor.c
driver was added about four versions earlier, so if he had for example
5.062, he could have the driver, but not the example.
pmuldoon



Joined: 26 Sep 2003
Posts: 204
Location: Northern Indiana

View user's profile Send private message

PostPosted: Mon Oct 18, 2021 12:35 pm     Reply with quote

I made up my own circuit and it worked very well.
circuit:
3 fets (as switches to steer the cap to the desired resistor)
2 precision resistors
1 capacitor (held in discharge via the CCP pin between readings)
and used the CCP timer

I would alternately charge the cap with the low resistor, thermistor and hi resistor, timing the charge for each.
From the low and high result I could calculate the counts per ohm.
From that I could calculate the resistance of the thermistor.
(since a thermistor is just a resistor and I can only measure it's resistance anyway.)
And from that I used a lookup table to interpolate the temperature.

It's actually not as complicated as it sounds, and was a lot of fun to make. Much more fun than using somebody else's code.

It has the advantage of only needing precision resistors. The osc, cap and Vref only need to be stable for the time it takes to take all 3 readings.

And bigger caps give you a bigger count.

I considered using the ADC and a precision res/div but I didn't think the adc accuracy over 10-bits would be very precise. The 12-bit adc's seem to come with a lot of errata.

Of course I had the luxury of free PIC pins and enough spare time to keep everything quite during the readings.
temtronic



Joined: 01 Jul 2010
Posts: 8219
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Oct 19, 2021 5:14 am     Reply with quote

PM's way is similar to a very early (20 year ago ?) appnote from Microchip, back in the good old dayze when PICs had quartz windows on them....
pmuldoon



Joined: 26 Sep 2003
Posts: 204
Location: Northern Indiana

View user's profile Send private message

PostPosted: Tue Oct 19, 2021 5:26 am     Reply with quote

Hmm...I did read a lot of app notes back then.
It seems like every idea I have is probably a spinoff of something I've read or seen and then forgotten about.

Thanks, T.
At least my subconscious is kind enough to let me forget and think it was my idea.

Did I ever mention the interesting relationship I noticed between Voltage, Current and Resistance?
Ttelmah



Joined: 11 Mar 2010
Posts: 17468

View user's profile Send private message

PostPosted: Tue Oct 19, 2021 7:52 am     Reply with quote

Remember that with a thermistor, if you use more than a very little
current to measure it, you will get 'self heating' from the current, and the
resistance will change. It's a 'classic' issue working with them... Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group