Measuring temperature with thermistors

/*
Measuring temperature with Arduino

Alan Wendt, PhD

A thermistor is a resistor whose resistance decreases
as the temperature increases. With some care, you can
use Arduino to read accurate temperatures. My project
is a decent evaporative cooler controller. In Arizona, you
can usually run the fan motor on high or low, and you can
also turn the water on and off. If you measure the temperature
in the house, outside in the shade, and the air right after the
evaporative pads, you can calculate the relative humidity,
and also program the unit to shut down the pump if the outside
air is already cool enough.

To measure the current resistance of the thermistor,
wire a thermistor and a 10K resistor up in serial.
Connect the open end of the thermistor to +5V, and the
open end of the resistor to ground. Now, there's a 5V drop
across the pair, and the drop across the thermistor is
proportional to its resistance as a fraction of the
sum of both resistances. For example, if the voltage at the
junction of resistor and thermistor is 4, that means that the
thermistor dropped 1 volt, and the fixed resistor dropped 4, so the
thermistor's current resistance is 1/4th of the fixed.

Once you have the resistance, the temperature of the thermistor is approximated with the formula:

T = 1 / (A + B log(R) + C log(R) ^ 3)

where A, B, and C are experimentally-determined constants.
(The Steinhart-Hart approximation). You need to collect
4-5 few data points, preferably with a big temperature spread.

I put the board into a refrigerator along with a little
Radio Shack remote thermometer sensor, and snaked out
the USB cable and the remote sensor cable. Once the program's
downloaded, enter the "serial monitor" in the Arduino
SDK to see the output.

Google "steinhart hart moshier" for further discussion of calibration, and for software to calculate the constants.

*/

int firstSensor = 5; // first analog sensor

void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}

void loop()
{
// read the analog input
float a5 = analogRead(5);
Serial.print("analogread = ");
Serial.print(a5);
Serial.print("\n");

// calculate voltage
float voltage = a5 / 1024 * 5.0;
Serial.print("voltage = ");
Serial.print(voltage);
Serial.print("\n");

// calculate resistance
float resistance = (10000 * voltage) / (5.0 - voltage);
Serial.print("resistance = ");
Serial.print(resistance);
Serial.print("\n");

// calcuate temperature. Use these values for A, B, and C till you
// get everything working, and then do some measurements to calibrate
// your thermistor in circuit.
float logcubed = log(resistance);
logcubed = logcubed * logcubed * logcubed;
float kelvin = 1.0 / (-7.5e-4 + 6.23e-4 * log(resistance) - 1.73e-6 * (logcubed));

// Convert to Fahrenheit
float f = (kelvin - 273.15) * 9.0/5.0 + 32.0;
Serial.print("temp = ");
Serial.print(f);
Serial.print("\n");

// delay 1s to let the ADC recover:
delay(1000);
}

Thanks for posting this - good intro to something I was about to start looking into!
-SA

Some thermistors (especially tiny ones) can self-heat with quite small amounts of current. Check the specs. You may have to switch on the 5V just briefly while making the measurement.

Regarding jdoll's comment about heat generation, would the solution be to wire the +5 of the circuit to a digital output and set it high shortly before taking a reading, then setting it low immediately after? If so, any suggestions on how long before taking the reading to set the output high to get the most accurate reading?

Also, is anyone else working with the thermistor that comes as part of the makershed advanced arduino kit? I'm trying to find out the specs for this part so that I can plug in the appropriate constants to the program as the default calculations don't appear to be correct for this part.