ENع
←Calibration
Lesson 25 · Analog: ADC, DAC and sensors

Calibrating a sensor

12 min8 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 25 · Interactive
01 / 08

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Offset 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 C
  3. Point 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)
  4. 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 C
  5. Draw 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.0
  6. Save 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);
  7. 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);
    }
  8. 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

Quiz Unlocks after the last step.