Reference voltage, resolution and scaling
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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)اقرأ الدرس خطوة بخطوة
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)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 stepOne 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 rampScale 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 mVThen 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)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 itScale 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 usedThe 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 stepsThe 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)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01On an Uno with the default 5 V reference, the input is 750 mV. What does analogRead() return?
raw = Vin × 1024 ÷ VREF = 750 × 1024 ÷ 5000 = 153.6, and the ADC keeps only the whole part: 153.
02What is the size of one step (1 LSB) of a 10-bit ADC with a 1.1 V reference?
One step is VREF ÷ 1024 = 1100 ÷ 1024 ≈ 1.07 mV. With the 5 V reference it is 4.88 mV.
03After analogReference(INTERNAL) the reading is 698, but the code still computes raw * 5000L / 1024. What does it print?
Scaling must use the reference the ADC used. 698 × 5000 ÷ 1024 = 3,408; the right maths is 698 × 1100 ÷ 1024 = 749 mV.
04Why write raw * 5000L instead of raw * 5000 on an Uno?
An int on the Uno holds at most 32,767. The L makes 5000 a long, so the multiplication is done in 32-bit long arithmetic.
05With the 1.1 V reference, a TMP36 at 80 °C gives 1300 mV. What happens?
Any input at or above about 1.1 V reads 1023, so the thermometer cannot tell 60 °C from 80 °C. A smaller VREF buys resolution at the cost of range. (A 5 V rail that is really 4.8 V is the opposite problem: 750 mV reads 160 and the code reports 28.1 °C.)
المسرد
- ADC
- Analog-to-digital converter: turns a voltage into a whole number. The Uno's is 10 bits, so 0 to 1023.
- Reference voltage (VREF)
- The voltage the ADC compares its input with. A reading is the input as a fraction of VREF.
- Resolution
- How many steps the ADC has (1024 for 10 bits), and so how small a change it can see.
- LSB (one step)
- The voltage of one step: VREF ÷ 1024 on a 10-bit ADC. The smallest change that moves the reading.
- Scaling
- Turning a raw reading back into volts (raw × VREF ÷ 1024) and then into a physical unit like °C.
- AVCC
- The ATmega328P's analog supply pin. analogReference(DEFAULT) on an Uno uses it, about 5 V, as VREF.
- Bandgap reference
- A voltage made inside the chip, about 1.1 V on the ATmega328P, chosen with analogReference(INTERNAL).
- Clipping
- An input at or above VREF reads the maximum code (1023), so larger inputs cannot be told apart.
الملاحظات والمصادر
- The card shows the four lines that matter. A full sketch for the Arduino Uno R3 also opens Serial, throws away the first reading after analogReference() (the Arduino reference warns the first few may be wrong) and prints t once a second.
- The ATmega328P datasheet gives the result as ADC = VIN × 1024 / VREF, where 0 is ground and 1023 (0x3FF) is VREF minus one step. So code ÷ 1024, not ÷ 1023, turns a reading back into a fraction of VREF.
- One step is not the whole error. The datasheet lists ±2 LSB absolute accuracy for the ADC itself, and the TMP36 is only accurate to about ±2 °C. A fine step shows small changes; it does not make the sensor more correct.
- The third option, analogReference(EXTERNAL), uses whatever voltage you put on the AREF pin, 0 to 5 V. Set EXTERNAL before the first analogRead(), or the internal reference is shorted to the AREF pin.
- An ESP32 is different: its ADC has more bits, and the input range is set by an attenuation setting rather than a VREF pin. Espressif recommends its calibration function adc_cali_raw_to_voltage() over doing the scaling by hand.
- Microchip ATmega328P datasheet (DS40002061B), ch. 24 Analog-to-Digital Converter and ADC characteristics
- Arduino language reference: analogReference() (source)
- Arduino language reference: analogRead() (source)
- Arduino language reference: int (source)
- Analog Devices TMP35/TMP36/TMP37 datasheet
- Espressif ESP-IDF: ADC oneshot mode driver (ESP32)
اختبار يتاح بعد الخطوة الأخيرة.