Temperature Measurement using Arduino and a Thermistor

17 07 2009

Components

  • Arduino Duemilanove Board, a USB cable and of course a PC/Mac
  • A 10kOhm thermistor
  • A 10kOhm resistor
  • A breadboard wires

Hot topic

It’g getting hotter and hotter. Is it due to global warming? Who knows, but in the meanwhile we can measure the temperature with our Arduino board and a thermistor. A thermistor is simply a type of resistor whose resistance varies with temperature. If we knew how it varies, we could extract the temperature from the measurement of the resistance (or, better, the voltage across its ends).

The Steinhart-Hart equation

In first approximation, the resistance is proportional to the temperature:

R = k(T-T0)

This can work for a narrow temperature range, but we can figure out something more accurate, up to the third order:

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

The temperature T is expressed in Kelvin, the resistance R in Ohm and the parameters A, B and C must be determined experimentally, or taken from the datasheet.

If you want to be even more rigorous, you have to include a self-heating effect in the picture. I’ll give it as a black-box formula:

T = T(steinhart-hart) – V^2 / k*R

Where k is the dissipation constant, usually given in units of mW/degree C.

Setting things up

In this case the setup is really simple. First of all, connect the Arduino board to the computer (in my case, it’s a Mac) via the USB cable. Now, connect the GND terminal to the B line of the breadbord (use a black wire), and Arduino’s +5V output to the A line (use a red wire). Now, put the 10kOhm resistance between the (+) bus and a horizontal line. Now, connect the thermistor between the horizontal line you chose and the (-) bus, the common ground. Last but not least, connect Arduino’s Analog Input #0 to the horizontal line (X).

(+5V) —- R —- X —- Th — GND

Programme Arduino

Now, the geek part. We have to teach Arduino how to calculate the temperature and give us the answer.

#include <math.h>
#define READING_PIN 0

double R1 = 10000.0; //resistance put in parallel
double V_IN = 5.0;

double A = 1.129148e-3;
double B = 2.34125e-4;
double C = 8.76741e-8;
double K = 9.5; // mW/dec C – dissipation factor

double SteinhartHart(double R)
{
// calculate temperature
double logR  = log(R);
double logR3 = logR * logR * logR;

return 1.0 / (A + B * logR – C * logR3 );

}

void setup() {
Serial.begin(9600);
}

void loop() {
double adc_raw = analogRead(READING_PIN);
//Serial.println(adc_raw);
double V =  adc_raw / 1024 * V_IN;
Serial.print(“V =”);
Serial.print(V);

//calculate resistance
double R_th = (R1 * V ) / (V_IN – V);
Serial.print(“V   R_th = “);
Serial.print(R_th);

double kelvin = SteinhartHart(R_th) – V*V/(K * R_th);
double celsius = kelvin – 273.15;
Serial.print(“Ohm  —   T = “);
Serial.print(celsius);
Serial.print(“C\n”);
delay(1000);
}

Compile it and send it to Arduino. Open up the Serial Terminal, and enjoy the output stream.

Now it’s up to you

Probably, the temperature you’re measuring is quite unconvincing. You have to set up the coefficient according to you thermistor. This will be covered in a following post…