Analog-to-Digital Conversion: From Voltage to a Code
Analog-to-digital conversion lets a microcontroller represent a sensor's voltage as a number. In an Arduino-based instrument, you use that number to estimate the input voltage and decide whether small signal changes are distinguishable. The result depends on the ADC's bit depth and reference voltage, so a raw reading has meaning only when you know both.
Voltage, reference, and codes
An analog-to-digital converter (ADC) measures an input voltage and assigns it one of a finite set of integer codes. On an Arduino, an analog input such as A0 connects the signal to the ADC, and analogRead() returns a code. The input voltage is measured relative to the board's ground, so a separate sensor needs a shared ground.
The reference voltage, Vref, sets the upper end of the conversion range for the unipolar model used here. It is a measurement scale, not automatically the sensor's supply voltage. An Arduino Uno R3 has a 10-bit ADC; other boards can have different resolutions.
For an n-bit conversion starting at zero volts:
Here N is the number of codes, k is the returned code, and q is the voltage width of one ideal bin, often called one least significant bit (LSB). Codes are dimensionless counts; q has units of volts.
An explicit ideal conversion model
Quantization assigns an interval of input voltages to a single code. For calculations here, use an ideal equal-bin model with boundaries at multiples of q. For an input within 0≤Vin<Vref:
The floor symbol means round down to an integer. At or above the reference, this model saturates at the largest code. Actual ADC transition thresholds and errors depend on the hardware; the stated model makes the calculation convention explicit.
A code identifies a bin, rather than an exact voltage. If you represent that bin by its midpoint, your estimate is
For an unsaturated, ideal conversion, this estimate differs from the input by no more than q/2. That bound describes quantization alone. It does not include sensor errors, noise, or an inaccurate reference. A voltage change smaller than q may stay within one bin, although it can change the code if it crosses a boundary.
Resolution, range, and accuracy
A smaller q gives finer voltage resolution. You can reduce q by increasing bit depth while holding the reference fixed, or by lowering the reference while holding bit depth fixed. These choices have different consequences.
Ideal configuration | One-code voltage width | Consequence |
|---|---|---|
10 bits, 4.00 V reference | 3.91 mV | Baseline conversion range |
11 bits, 4.00 V reference | 1.95 mV | Half the bin width at the same range |
10 bits, 2.00 V reference | 1.95 mV | Half the bin width and half the range |
The displayed widths are rounded; the second and third configurations have exactly half the first configuration's bin width. Lowering the reference helps only if the signal remains within the intended conversion range. A maximum code does not reveal how far an overrange input exceeds the reference.
Resolution describes code spacing. Accuracy describes agreement with the actual input voltage. More bits do not correct an incorrect reference value or a missing ground connection. Use the actual reference when available, and avoid treating every last digit of a calculated estimate as experimentally reliable.
What to remember
- An n-bit ADC provides 2n codes, numbered from zero.
- Use q=Vref/2n for the stated equal-bin model.
- A code identifies a voltage interval; midpoint conversion gives an estimate.
- Finer resolution does not by itself establish better accuracy.
Example: Converting a Position Sensor's Voltage
A position sensor shares ground with an Arduino Uno R3 and sends its output to A0. Model the board's 10-bit ADC as an ideal equal-bin converter with an exact 5.00 V reference. Ignore noise and hardware errors. For inputs below the reference, use q=Vref/210 and k=⌊Vin/q⌋.
Treat the stated voltages as exact for these calculations and retain unrounded values until the final result.
a) Find the width of one voltage bin in millivolts, to three decimal places.
Ten bits provide ________ distinct codes. Divide the full reference range by this number, then convert volts to millivolts:
The bin width is 4.883 mV, to three decimal places. Use the unrounded width in the remaining calculations.
b) The sensor initially outputs 1.730 V. Predict the returned ADC code.
Divide the input voltage by the bin width to locate the input within the sequence of bins. The stated floor convention requires rounding down, rather than rounding to the nearest integer:
Check the boundaries of code 354:
The input lies between these boundaries, so the predicted returned code is 354. The code is a dimensionless count, not a voltage in millivolts.
c) Later, the sensor moves and the returned code becomes 602. Find the input-voltage interval and estimate its midpoint in volts, to three decimal places.
A code identifies a bin, so first calculate its lower boundary and its excluded upper boundary. Use the same reference and bin width because the ADC configuration has not changed:
The interval is therefore
The midpoint is ________ a bin above the lower boundary:
The requested midpoint estimate is 2.942 V. This value represents the bin; the code alone does not establish the exact voltage within it.
An Arduino Uno Rev3 repeatedly reads analog input A2. A technician disconnects the sensor wire from A2, leaving that pin unconnected with its internal pull-up disabled. The readings vary even though the technician expects zero.
Complete the technical description: because no connected source establishes its voltage, A2 is a input.
A battery monitor uses an ideal 10-bit ADC with an exact 3.30 V reference and returns code 731. The conversion range starts at 0 V, and each equal-width bin has width q=Vref/210. Estimate the input voltage using the midpoint of the bin, V^in=(k+0.5)q.
Give your answer in volts, to three decimal places. Ignore noise and reference error.