Arduino Temperature Thermistor Circuit and Calculation Code

Arduino Thermistor CircuitSo you want to meassure temperature of something using an Arduino product. By far the cheapest way to do this is by using a 10k Ohm NTC Thermal Resistor divider circuit and an analog pin on an Arduino! The Circuit will be something like this image. The idea is to pull GND through the variable thermal resistor with the 5V pullup through another 10k (standard) resistor. The effect is that the analog pin will swing through the range of the Thermal resistor from 5V to GND. If you are using a 3.3V Arduino it will also work fine.

I found the calculation I’m going to show you here on Adafruit I think, or maybe it was somewhere else, however I shuffled it into a routine that makes obtaining an accurate value returned as a float (with 2 decimal places) is simple anywhere in your code.

First we have to define some values. If you want the best accuracy (these aren’t the most accurate but usually pretty good) find out the Beta Coefficient of your resistor and enter it instead. Put this stuff before your void setup(){  somewhere.

//Temperature Stuff
#define THERMISTORNOMINAL 10000 //resistance at 25 degrees C
#define TEMPERATURENOMINAL 25
#define BCOEFFICIENT 4450 // Beta Coefficient of Thermistor used Normally between 3000-4000
#define SERIESRESISTOR 10000 // Series Resistor Value 10K here up to VCC 5V

Next we create the routine. Place this bit below your main program loop. NOT inside the loop.

float RETURN_TEMP(float val){

float steinhart;
// Convert Value to resistance
float tempVal = (1023 / val) – 1;
tempVal = SERIESRESISTOR / tempVal;
// Use Steinhart formula to convert to degrees Centigrade and assign to tempVal
steinhart = tempVal / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
tempVal = steinhart;

return tempVal;
}

Ok so thats it. Now all you need to do is plug your analog value into this formula to return the value in Degree’s Celsius. Like this

For example sake lets say the analog pin the temperature circuit is connected to is A2 and we will output the temperature to serial every 1 second. Put this in place of the main loop.

unsigned long TIME1 = 0;
void loop(){

if(millis() – TIME1 >= 1000){
TIME1 = millis();
//A Non Blocking 1 Second interval NO DELAYS!!!
Serial.print(“Temperature: “);
int tval = analogRead(A2);
Serial.println(RETURN_TEMP(tval));


}
}

Questions or similar in the comments :) Hope this works well for your projects! It has served me well. You can even use it to spit the values onto LCD displays too, the good old Liquid Crystal library is an example, something like lcd.print(RETURN_TEMP(tval)); will spit the value out on the screen wherever you set the cursor to before hand…

Another note, if you don’t want the 2 decimal places you can convert the value to an int by just storing the returned value into an int something like. int X = RETURN_TEMP(tval);

 

Share

Tags: , , , , , , ,

No comments yet.

Leave a Reply