This started as notes I made to myself long ago for the LogoChip, but they apply equally to the Arduino A/D converters.
The LM34DZ is a temperature sensor in a TO-92 case available from All Electronics for $2.50. It has the handy characteristic of reading an output voltage that directly corresponds to Fahrenheit temperature in a ratio of 10mV per °F. That is, at 70°F it reads 70 * 10mV = 700mV.
With a direct output voltage (rather than the varying resistance that many thermal probes provide), it’s perfect for hooking to a microcontroller A/D input. So, how to convert the A/D reading back into Fahrenheit temperature?
Well, the sensor reads 10mV (or .01V) per °F. Microcontroller A/D converters tend to have 5V input and read 1024 steps over the 0-5V range.
1024 steps / 5V ≈ 205 steps / V
(.01V / °F) * (205 steps / V) ≈ 2 steps / °F
Thus you can get a “maybe close enough” approximation with code like
tempF = analogRead(lm34Pin) / 2;
With a conversion error of +2.4%, this’ll get you within a couple of degrees at room temperature — close enough to make some macro-level observations about whether it’s getting warmer or colder for a physical computing project. Since the stated accuracy is only 1°F anyway, that’s not too bad.
If you need a more accurate conversion, you’ll need to use floating-point arithmetic if you have it (which the Arduino
doesn’t [correction: does]) or find a fixed-point arithmetic library if you don’t. Or if your integer variables are large enough (at least 17 unsigned bits for temperatures up to 127°F, 18 bits up to 255°F), you can rearrange the order of calculation like so:
tempF = (1024 * analogRead(lm34Pin)) / 5;