Calibrating a sensor
About this lesson
A sensor read through an ADC is almost a straight line, but a little shifted (an offset error) and a little tilted (a gain error). Put a TMP36 probe on an Arduino Uno R3 in ice water and it reads 3.2 °C instead of 0.0 °C. Measure it at two temperatures you know, fit the straight line through those two points, save its slope and offset in EEPROM, and every later reading comes out right: 25.1 °C in a 25.0 °C room instead of 29.6 °C. The lesson "Reference voltage, resolution and scaling" explains the ADC formula used here, and "Smoothing noisy readings" the averaging.
The reading is off
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.
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
Read the lesson step by step
The reading is off
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.
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 highOffset and gain errors
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.
reading = gain x truth + offset (a straight line) gain ADC = Vin * 1024 / Vref, Vref is 4.80 V, not 5.00 V: x1.042 offset probe +1.5 C x 1.042 = +1.6 C (TMP36 G grade, TO-92: +-2 C typ, +-4 C max) + Vref scaling the 500 mV at 0 C = +2.1 C = +3.6 C at 0 C, before rounding round the ADC drops the fraction: 109.87 -> 109, -0.4 C, so 0 C reads 3.2 C at 40 C +3.6 + tilt 1.7 = +5.3 C, after rounding 5.2 CPoint 1: ice water
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.
line 1 float r1 = readAt("ice"); average 16 readings -> r1 = 109 point 1 (109 counts, 0.0 C)Point 2: warm water
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).
line 2 float r2 = readAt("warm"); average 16 readings -> r2 = 195 point 2 (195 counts, 40.0 C) the old formula would say 45.2 CDraw the line
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.
line 3 m = (T2 - T1) / (r2 - r1) = 40.0 / 86 = 0.465 line 4 b = T1 - m * r1 = -0.465 * 109 = -50.7 check m * 109 + b = 0.0 m * 195 + b = 40.0Save m and 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.
line 5 EEPROM.put(0, m); bytes 0-3 = 0.465 line 6 EEPROM.put(4, b); bytes 4-7 = -50.7 EEPROM 1 KB, kept with the power off, ~100,000 writes per cell setup() EEPROM.get(0, m); EEPROM.get(4, b);Correct every reading
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.
line 7 float t = m * readAvg() + b; raw 163 before 29.6 C (off by 4.6) after 0.465 * 163 + (-50.7) = 25.1 C (off by 0.1) // Arduino Uno R3: TMP36 probe on A0, two-point calibration kept in EEPROM #include <EEPROM.h> const float T1 = 0.0; // ice water, C const float T2 = 40.0; // warm water, C, from the reference thermometer float m, b; // gain (C per count) and offset (C) float readAvg() { // average of 16 readings long sum = 0; for (int i = 0; i < 16; i++) sum += analogRead(A0); return sum / 16.0; } float readAt(const char *bath) { // wait for Enter, then read Serial.print("Probe in "); Serial.print(bath); Serial.println(" water, then press Enter"); while (Serial.available() == 0) {} while (Serial.available() > 0) Serial.read(); return readAvg(); } void calibrate() { float r1 = readAt("ice"); float r2 = readAt("warm"); m = (T2 - T1) / (r2 - r1); b = T1 - m * r1; EEPROM.put(0, m); EEPROM.put(4, b); } void setup() { Serial.begin(9600); EEPROM.get(0, m); EEPROM.get(4, b); if (isnan(m)) calibrate(); // a blank EEPROM reads 0xFF bytes: NaN } void loop() { float t = m * readAvg() + b; Serial.println(t, 1); delay(1000); }How good is it now?
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.
resolution 1 count = 0.465 C linearity TMP36 +-0.5 C typical: a straight line cannot remove a bend check a third known temperature: within about 1 C (bend 0.5 + one count 0.47 can add up) redo it new probe, new board or new power supply
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In ice water (0.0 °C) the Arduino reports 3.2 °C, and in water at 40.0 °C it reports 45.2 °C. What kind of error is this?
The error is 3.2 °C at 0 °C (an offset) and 5.2 °C at 40 °C, so the slope is also wrong (a gain error). The low reference voltage causes part of both: it tilts the line and also scales the probe's 500 mV at 0 °C. Averaging removes noise, not a steady error like this.
02Why does a two-point calibration need two known temperatures and not just one?
A straight line has two unknowns, a slope and an offset. One point lets you shift the line; a second point fixes its tilt as well.
03Point 1 is (109 counts, 0.0 °C) and point 2 is (195 counts, 40.0 °C). What is the slope m?
m = (40.0 − 0.0) / (195 − 109) = 40 / 86 = 0.465 °C per count. 0.488 is what the datasheet formula assumes with a perfect 5.00 V reference.
04With m = 0.465 and b = -50.7, the ADC reads 163. What temperature does the firmware report?
t = m × raw + b = 0.465 × 163 + (-50.7) = 25.1 °C. The uncorrected formula gave 29.6 °C; the reference thermometer says 25.0 °C.
05Why does the sketch save m and b with EEPROM.put once, instead of writing them on every loop?
The ATmega328P datasheet rates its EEPROM for 100,000 write/erase cycles. Calibrate once, store the two numbers, and read them back with EEPROM.get in setup().
Glossary
- Calibration
- Comparing a sensor's readings with known true values and working out a correction, here a straight line, that makes them agree.
- Offset error
- A steady shift: the reading is too high or too low by the same amount everywhere. On a plot, the line is moved up or down.
- Gain error
- A wrong slope: the error grows as the value grows. On a plot, the line is tilted. A wrong reference voltage causes one, and because it scales every voltage it can shift the zero reading too.
- Reference point
- A condition whose true value you know, such as an ice-water bath at 0 °C or a reading from a trusted reference thermometer.
- Two-point calibration
- Measuring at two reference points and fitting the straight line t = m × raw + b through them, which fixes both offset and gain.
- Reference voltage (Vref)
- The voltage the ADC compares its input against: ADC = Vin × 1024 / Vref on the ATmega328P. On a 5 V Uno it is the 5 V supply by default.
- EEPROM
- Memory inside the microcontroller that keeps small values, like calibration numbers, with the power off. Each cell can be written a limited number of times.
- Non-linearity
- How far a sensor bends away from a straight line. A straight-line calibration cannot remove it.
Notes and sources
- 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 readings in the example are steady, so the average of 16 is a whole number. With real noise it is usually a fraction, which is why r1 and r2 are floats. The lesson "Smoothing noisy readings" covers averaging.
- An ice point is made from small pieces of ice packed in water from top to bottom, ideally both from distilled water. NIST describes it as nearly 0 °C, within 0.01 °C when made carefully. Stir it and keep the probe well inside the ice.
- EEPROM.put only rewrites bytes that changed, and EEPROM.get reads them back in setup(). On a chip whose EEPROM was never written, the bytes read 0xFF, which as a float is NaN (not a number), so the sketch calibrates when isnan(m) is true. A float is 4 bytes on the Uno.
- 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.
- Analog Devices TMP35/TMP36/TMP37 datasheet (Rev. F): accuracy, scale factor, linearity
- Microchip ATmega328P datasheet (DS40002061B): ADC conversion result, offset and gain error, EEPROM endurance
- Arduino language reference: analogRead() (source of docs.arduino.cc)
- Arduino language reference: analogReference() (source of docs.arduino.cc)
- Arduino language reference: float (source of docs.arduino.cc)
- Arduino AVR core: EEPROM library README (put, get, update)
- NIST: Mercury thermometer alternatives, verification methods (the ice melting point)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 14: ADC and data acquisition (UT Austin)
Quiz Unlocks after the last step.