Why calibrate
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 things are off. The probe reads 1.5 °C high (its datasheet allows ±2 °C). The ADC's 5 V reference is really 4.80 V, so every voltage reads 4% big, even the probe's 500 mV at 0 °C. That lifts and tilts the line.
Two known points
Calibration needs known temperatures. Crushed ice packed in water sits at 0.0 °C. We average 16 readings, so noise cannot nudge the point, and the ADC gives 109. Write it down: 109 counts means 0.0 °C.
One point fixes the offset but not the gain, so we need a second. In warm water the reference thermometer says 40.0 °C and the average reading is 195. Now we have two pairs: (109, 0.0) and (195, 40.0).
Two points fix a straight line. Its slope m = (40.0 − 0.0) / (195 − 109) = 0.465 °C per count. Its offset b = 0.0 − 0.465 × 109 = −50.7 °C. From now on, temperature = m × reading + b.
EEPROM.put writes m and b, 4 bytes each, into the chip's 1 KB of EEPROM, which keeps them with the power off. EEPROM wears out after about 100,000 writes, so we calibrate once and read the numbers back at every start.
Using the line
Back in the room the reference says 25.0 °C and the ADC gives 163. The old formula said 29.6 °C. With our line: 0.465 × 163 − 50.7 = 25.1 °C. A 4.6 °C error is now 0.1 °C.
What is left is small. One ADC count is worth 0.47 °C, before and after calibrating, so no reading is finer. The TMP36 also bends about 0.5 °C from a straight line. Check a third temperature, and recalibrate after changing the probe, board or supply.
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.