MCUATmega328PEEPROM 0EEPROM 43.2 °C1float r1 = readAt("ice");2float r2 = readAt("warm");3m = (T2 - T1) / (r2 - r1);4b = T1 - m * r1;5EEPROM.put(0, m);6EEPROM.put(4, b);7float t = m * readAvg() + b;ice waterwarm waterroom air0.0 °C1091951633.2mb25.1
01020304001020304050true °Creported °CReading vs true temperatureidealbeforeafter
Back: the microcontroller with its EEPROM cells, and the reading it reportsFront: the calibration code, the water bath with the probe, and a reference thermometerTokens: ADC counts, the numbers m and b, and the corrected readingA TMP36 probe on an Arduino Uno R3 reads 3.2 °C in ice water and 45.2 °C in warm water at 40.0 °C: an offset and a gain error. Two known points give a straight line, m = 0.465 °C per count and b = -50.7 °C, kept in EEPROM. In the room, 163 counts become 25.1 °C against a true 25.0 °C.

Why calibrate

  1. Our TMP36 probe sits in ice water, which is 0.0 °C, and a trusted reference thermometer agrees. The Arduino, using the datasheet formula, says 3.2 °C. Nothing is broken: every sensor and ADC is a little off. Calibration measures how far, then corrects it.

Two known points

Using the line

What happens

;; step 1/8 · off
bath         ice water, true 0.0 C (reference thermometer)
ADC          raw 109
formula      raw * 5000 / 1024 / 10 - 50 = 3.2 C
error        3.2 C too high

The errors in the example are chosen for one probe and one board: a probe 1.5 °C high and a 5 V line that measures 4.80 V. Yours will differ, which is the point of calibrating. The ADC count is the ATmega328P's ADC = Vin × 1024 / Vref, rounded down. The card shows only the core lines. The whole sketch also defines T1 and T2, averages 16 readings in readAvg(), waits for Enter on the serial monitor in readAt(), and prints one corrected reading a second.