ENع
←ADC reference
Lesson 21 · Analog: ADC, DAC and sensors

Reference voltage, resolution and scaling

12 min9 steps5 questionsIntermediate
About this lesson

An ADC never measures volts directly: it measures what fraction of a reference voltage its input is. Follow a TMP36 temperature sensor at 25 °C into pin A0 of an Arduino Uno R3: 750 mV reads 153 against the 5 V reference and 698 against the internal 1.1 V reference. See why the reference sets both the range and the size of one step, how to scale a reading back to millivolts and degrees with whole-number maths, and why a wrong or drifting reference gives a confident wrong answer. The Signals lessons “Quantization and bit depth” and “Inside an ADC: flash and successive approximation” explain the converter itself; here we use it from firmware.

You'll learn to

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

Fig 21 · Interactive
01 / 09

An ADC measures with a ruler

An ADC cannot measure volts on its own. It compares the pin with a reference voltage, VREF, and reports what fraction of VREF it sees, as a whole number from 0 to 1023. On an Uno, VREF is the 5 V rail by default.

ADC   analog-to-digital converter
VREF  the reference voltage: the length of the ruler
      Uno default: AVCC = 5 V
pin A0 sees 750 mV from the TMP36 (25 °C)
answer: a whole number 0 .. 1023  (10 bits)
Loading figure…
or ← →
Read the lesson step by step
  1. An ADC measures with a ruler

    An ADC cannot measure volts on its own. It compares the pin with a reference voltage, VREF, and reports what fraction of VREF it sees, as a whole number from 0 to 1023. On an Uno, VREF is the 5 V rail by default.

    ADC   analog-to-digital converter
    VREF  the reference voltage: the length of the ruler
          Uno default: AVCC = 5 V
    pin A0 sees 750 mV from the TMP36 (25 °C)
    answer: a whole number 0 .. 1023  (10 bits)
  2. 750 mV becomes 153

    The datasheet's rule is raw = Vin × 1024 ÷ VREF. So 750 × 1024 ÷ 5000 = 153.6, and the ADC keeps only the whole part: analogRead(A0) returns 153.

    line 2   int raw = analogRead(A0);
    rule     raw = Vin * 1024 / VREF   (whole part only)
             750 * 1024 / 5000 = 153.60
    raw      153
    0 = 0 V, 1023 = VREF minus one step
  3. One step is 4.88 mV

    10 bits give 1,024 steps, so one step, called 1 LSB, is VREF ÷ 1024 = 4.88 mV. Smaller changes are invisible: 750 and 751 mV both read 153. For the TMP36 one step is about half a degree.

    resolution  10 bits = 1,024 steps
    1 LSB       VREF / 1024 = 5000 / 1024 = 4.88 mV
                750 mV -> 153   751 mV -> 153
    TMP36       10 mV per °C  ->  0.49 °C per step
    plot        the reading climbs in stairs, not a ramp
  4. Scale back to millivolts

    Run the rule backwards with the same VREF: mV = raw × 5000 ÷ 1024. 153 gives 747 mV. The L in 5000L makes it long maths: 153 × 5000 = 765,000, far past an int's 32,767.

    line 3   long mV = raw * 5000L / 1024;
             153 * 5000 = 765,000
             / 1024 = 747.07  ->  747 (whole part)
    int on the Uno: 16 bits, at most 32,767
    true input 750 mV, read back 747 mV
  5. Then into degrees

    The TMP36 gives 500 mV at 0 °C plus 10 mV per degree, so t = (747 − 500) ÷ 10 = 24.7 °C. The room is really 25 °C: the reading can only be as fine as one step.

    line 4   float t = (mV - 500) / 10.0;
             (747 - 500) / 10 = 24.7 °C
    true     25 °C   (error 0.3 °C, under one step)
  6. A shorter ruler: 1.1 V

    analogReference(INTERNAL) swaps VREF for the chip's built-in 1.1 V. The same 1024 steps now span 1.1 V, so one step is 1.07 mV, about a tenth of a degree. The same 750 mV now reads 698.

    line 1   analogReference(INTERNAL);
    VREF     1100 mV (inside the ATmega328P)
    1 LSB    1100 / 1024 = 1.07 mV  ->  0.11 °C
    raw      750 * 1024 / 1100 = 698.18  ->  698
    the first reading after a switch may be off: discard it
  7. Scale with the new VREF

    Leave 5000 in the scaling and 698 becomes 3,408 mV: nonsense. Scaling must use the reference you read with: 698 × 1100 ÷ 1024 = 749 mV, so 24.9 °C. That is within 0.1 °C.

    wrong    698 * 5000 / 1024 = 3,408 mV
    line 3   long mV = raw * 1100L / 1024;
    right    698 * 1100 / 1024 = 749 mV
    t        (749 - 500) / 10 = 24.9 °C
    rule: scale with the same VREF the ADC used
  8. The price: a shorter range

    Anything near 1.1 V or above reads 1023. The TMP36 reaches 1100 mV at 60 °C, so this thermometer tops out there: 70 °C reads the same 1023. Pick the smallest VREF that still covers your whole signal.

    60 °C   TMP36 = 1100 mV  ->  raw 1023  ->  59.8 °C
    70 °C   TMP36 = 1200 mV  ->  raw 1023   (stuck)
    5 V ref: range 0..5 V, 4.88 mV steps
    1.1 V ref: range 0..1.1 V, 1.07 mV steps
    range and resolution trade off: same 1024 steps
  9. The ruler must be true

    The maths trusts VREF. Back on the 5 V reference, suppose the rail is really 4.8 V: 750 mV reads 160, and the code says 781 mV, 28.1 °C, 3 degrees off. The internal 1.1 V can be 1.0 to 1.2 V, so calibrate.

    line 1   analogReference(DEFAULT);   line 3 back to 5000L
    rail     really 4800 mV, code assumes 5000
    raw      750 * 1024 / 4800 = 160
    scaled   160 * 5000 / 1024 = 781 mV  ->  28.1 °C
    datasheet: internal ref 1 .. 1.1 .. 1.2 V (min typ max)

Quiz Unlocks after the last step.