Skip to content
Tactiverge

Analog-to-digital conversion

How an ADC turns a voltage into a number: resolution, reference voltage, sampling and quantisation, and how to convert counts back into volts correctly.

Lesson 6 of 6 · 30 min · Self-paced · Free

Measuring a smooth world with numbers

A gripper's force sensor, a temperature probe and a battery monitor all produce a voltage that varies smoothly. A microcontroller only understands numbers. The bridge between the two is the analog-to-digital converter (ADC), a peripheral that measures a voltage and reports it as an integer. Almost every sensor you will read in the sensor integration module passes through an ADC, so understanding exactly what its numbers mean is essential. Most sensor bugs are really unit-conversion bugs.

What an ADC does

An ADC compares the input voltage with a reference voltage, V_ref, and reports where it falls as a whole number called a count or code. An N-bit ADC has 2^N possible codes:

  • 8-bit: 2^8 = 256 codes (0 to 255)
  • 10-bit: 2^10 = 1,024 codes (0 to 1,023)
  • 12-bit: 2^12 = 4,096 codes (0 to 4,095)

0 V gives a code of 0; a voltage at or near V_ref gives the maximum code. Everything in between is divided into equal steps.

Resolution: the size of one step

The voltage represented by one step is called one LSB (least significant bit):

step = V_ref / 2^N

For a 10-bit ADC with a 3.3 V reference:

step = 3.3 V / 1024 ≈ 0.00322 V = 3.22 mV

A 12-bit ADC with the same reference has steps of 3.3 / 4096 ≈ 0.806 mV, four times finer. Any voltage within one step gives the same code, so the reading can be off by up to half a step from the true value. This unavoidable rounding is called quantisation error.

Be careful not to confuse resolution with accuracy. A 12-bit ADC whose reference is 2 % wrong produces very finely stepped readings that are all 2 % wrong. Real ADCs also have offset, gain and noise errors, listed in the datasheet.

Converting counts to volts

The usual conversion is:

V = count × V_ref / 2^N

Some datasheets use 2^N − 1 instead; the difference is less than one step, but check which your chip uses.

Worked example

A 10-bit ADC with a 3.3 V reference reads 512:

V = 512 × 3.3 / 1024 = 1.65 V

That is exactly half the reference, as you would expect for half the maximum code.

Small microcontrollers often lack fast floating-point hardware, so conversions are frequently done in whole millivolts using integers. Multiply before you divide, or the integer division throws away almost everything, and use a 32-bit type so the multiplication cannot overflow. (The next module explains overflow in detail.) This portable C program runs on any computer:

#include <stdio.h>
#include <stdint.h>

#define ADC_BITS 10        /* counts run from 0 to 1023 */
#define VREF_MV  3300u     /* reference voltage in millivolts */

uint32_t counts_to_millivolts(uint16_t counts) {
    /* multiply first, then divide by 2^10 = 1024 */
    return ((uint32_t)counts * VREF_MV) >> ADC_BITS;
}

int main(void) {
    uint16_t samples[] = {0, 1, 230, 512, 1023};
    int n = sizeof samples / sizeof samples[0];
    for (int i = 0; i < n; i++) {
        printf("%4u counts -> %4lu mV\n", (unsigned)samples[i],
               (unsigned long)counts_to_millivolts(samples[i]));
    }
    return 0;
}

Output:

   0 counts ->    0 mV
   1 counts ->    3 mV
 230 counts ->  741 mV
 512 counts -> 1650 mV
1023 counts -> 3296 mV

The >> ADC_BITS shifts the bits right by 10 places, which divides by 1,024 and discards the remainder. That is why 1 count shows 3 mV rather than 3.22 mV. Notice too that the maximum code, 1,023, gives 3,296 mV, not 3,300: the top code represents the last step below V_ref.

From volts to a physical quantity

Suppose a hypothetical temperature sensor outputs 500 mV at 0 °C and rises by 10 mV for each degree. The reading of 741 mV means:

temperature = (741 − 500) / 10 = 24.1 °C

So the chain is: counts → millivolts → physical units. Keep each step separate and labelled, and you will catch mistakes far more easily.

The reference voltage matters

Many boards use the supply voltage as V_ref by default. If the supply sags, perhaps when a motor starts and draws a surge of current, every reading shifts with it, even though the sensor has not changed. For measurements that must be accurate, use a stable dedicated reference if the chip offers one.

Also, never apply a voltage above the ADC's allowed range (usually the supply) or below 0 V. To measure a larger voltage, such as a 12 V robot battery, scale it down with a voltage divider. Design for the highest voltage the battery can reach, not its nominal value: a "12 V" pack made of three lithium cells reaches about 12.6 V when fully charged. With R1 = 33 kΩ on top and R2 = 10 kΩ below:

V_out = V_in × R2 / (R1 + R2)
at 12.0 V: 12.0 × 10 / 43 ≈ 2.79 V
at 12.6 V: 12.6 × 10 / 43 ≈ 2.93 V

Both stay safely under 3.3 V with some margin. (A 27 kΩ top resistor would give 3.24 V at 12 V but about 3.41 V at 12.6 V, which is over the limit.) In software, multiply the measured voltage back up by 43 / 10 = 4.3.

Sampling

An ADC does not watch the voltage continuously; it takes samples, snapshots at particular moments. Inside, a sample-and-hold circuit charges a tiny capacitor to the input voltage, disconnects it, and then measures that frozen value. Each conversion takes time, often a few to tens of microseconds, which limits the sample rate.

How often should you sample? Fast enough to follow the signal's real changes. A slowly changing temperature may need only a few readings per second; a vibration sensor may need thousands. Sampling too slowly can make a fast signal look like a completely different, slower one. This effect, aliasing, is the subject of a later lesson in the sensor integration module.

Readings also wobble by a count or two because of electrical noise. Averaging several samples is a simple, effective remedy, also covered in the sensor integration module.

Recap

An N-bit ADC maps 0 V to V_ref onto 2^N codes, with step size V_ref / 2^N. Convert with V = count × V_ref / 2^N, multiplying before dividing and using wide enough integers. Resolution is not accuracy; the reference voltage must be stable; inputs must stay within range, scaled by a divider if necessary. Sample often enough to follow the signal, and convert counts to volts to physical units one labelled step at a time.

Project

Build an ADC conversion table

Using the program in this lesson as a starting point, write a C or Python program that:

  1. Prints the step size in millivolts for 8-, 10- and 12-bit ADCs with a 3.3 V reference.
  2. Converts a list of 10-bit readings, (0, 100, 230, 620, 1023), to millivolts, and then to temperature using the made-up sensor in this lesson (500 mV at 0 °C, 10 mV per °C).
  3. Repeats step 2 assuming the reference has sagged to 3.1 V while the code still assumes 3.3 V. How many degrees of error does that cause at a reading of 230?

This is a software-only activity.

Deliverables

  • Step sizes for the three resolutions
  • A table (printed by your program) of counts, millivolts and temperature
  • The temperature error caused by the sagging reference, with a sentence explaining it

Knowledge check

Check your understanding

  1. 1. How many distinct codes does a 12-bit ADC have?
  2. 2. A 10-bit ADC with a 5.0 V reference reads 256. What is the input voltage, approximately?
  3. 3. Why does the conversion code compute counts × V_ref before dividing by 1,024?
  4. 4. A robot's ADC readings all jump whenever its motors start, even though the sensor output has not changed. What is the most likely cause?
  5. 5. What is quantisation error?

Go further