ADC Worked Example

Quantisation Explained

Turn a potentiometer and watch the 3-bit code change only when the voltage overflows the top of a band.

Getting started: what you'll need

This section lists everything you need for the worked example below. Equivalent parts will work.

Equipment list

Part numberDescriptionQty
ABX00087Arduino UNO R4 WiFi1
USB-C data cableUSB-C to USB-A/C data cable (not charge-only)1
BB830 (or equivalent)830-point solderless breadboard1
Jumper wire kitMale-to-male breadboard jumper wires, assorted1
10k potentiometer10 kΩ slider potentiometer. A 10k pot is also included in the Elegoo starter kit below1

New to electronics? This Elegoo electronics starter kit includes the Arduino-compatible board, breadboard, jumper wires and 10 kΩ potentiometer, and much more. It has everything you need for this example.

New to Arduino? Start with Arduino's UNO R4 getting-started guide for setting up your board and uploading your first sketch.

Arduino Code

This sketch reads A0 at 12 bits, keeps only the top 3 bits to simulate a 3-bit ADC, and draws those three bits as digits on the built-in LED matrix. The bottom row shows a single moving LED marker for the position within the current band. Use the Copy button, paste it into the Arduino IDE and upload.

#include "Arduino_LED_Matrix.h"
// Hosted on https://LittleEngineeringBooks.com for educational purposes.

ArduinoLEDMatrix matrix;

const int ANALOG_PIN = A0;

uint8_t frame[96];

// 3x5 font for binary digits
const uint8_t DIGITS[2][5] =
{
  // 0
  {
    0b111,
    0b101,
    0b101,
    0b101,
    0b111
  },

  // 1
  {
    0b010,
    0b110,
    0b010,
    0b010,
    0b111
  }
};

void clearFrame()
{
  memset(frame, 0, sizeof(frame));
}

void setPixel(int x, int y, bool state = true)
{
  if (x < 0 || x > 11 || y < 0 || y > 7)
    return;

  frame[y * 12 + x] = state ? 1 : 0;
}

void drawBinaryDigit(uint8_t value, int xOffset)
{
  for (int y = 0; y < 5; y++)
  {
    for (int x = 0; x < 3; x++)
    {
      if (DIGITS[value][y] & (1 << (2 - x)))
      {
        setPixel(xOffset + x, y);
      }
    }
  }
}

void render()
{
  matrix.loadPixels(frame, 96);
}

void drawDisplay(uint8_t code, uint16_t remainder)
{
  clearFrame();

  // Draw three bits
  drawBinaryDigit((code >> 2) & 1, 0);   // MSB
  drawBinaryDigit((code >> 1) & 1, 4);   // Middle bit
  drawBinaryDigit(code & 1, 8);          // LSB

  // Marker along bottom row
  int markerX = map(remainder, 0, 511, 0, 11);

  setPixel(markerX, 7);

  render();
}

void setup()
{
  matrix.begin();

  analogReadResolution(12);

  Serial.begin(115200);

  Serial.println("3-bit ADC Quantisation Demo");
}

void loop()
{
  // Full-resolution reading
  uint16_t adc = analogRead(ANALOG_PIN);

  // Simulated 3-bit ADC
  uint8_t code = adc >> 9;

  // Position within current quantisation bucket
  uint16_t remainder = adc & 0x01FF;

  drawDisplay(code, remainder);

  Serial.print("ADC=");
  Serial.print(adc);

  Serial.print("  Binary=");
  Serial.print((code >> 2) & 1);
  Serial.print((code >> 1) & 1);
  Serial.print(code & 1);

  Serial.print("  Bucket=");
  Serial.println(remainder);

  delay(20);
}

// Hosted on https://LittleEngineeringBooks.com for educational purposes.
Quantisation Explained

Breadboard schematic

Breadboard and schematic for a 10k potentiometer connected to 5V, 0V and Arduino input A0
Click the image to view it full size.

Pinout wiring

The 10k pot pins are 1 (one end), 2 (wiper) and 3 (other end).

FromToPurpose
5V10k pot pin 1Top of the voltage divider
0V (GND)10k pot pin 3Bottom of the voltage divider
10k pot pin 2 (wiper)Arduino A0Divider output, read by the ADC

What we're doing

The potentiometer makes a voltage divider whose output, between 0 V and 5 V, is read on A0. The ADC is run at 12 bits (0 to 4095), but the sketch only keeps the top 3 bits (adc >> 9), as if the converter only had 3 bits. That splits the input range into 23 = 8 equal bands, each 512 counts (about 0.625 V) wide.

The LED matrix shows two things at once:

  • The three digits are the 3-bit output code, from 000 to 111.
  • The single LED on the bottom row shows where you are inside the current band. It is the 9 bits that were thrown away (adc & 0x01FF), scaled across the 12 columns.

Why this matters

Quantisation is the step where a continuous voltage is forced onto a limited set of values. Turn the pot slowly and the marker crawls from left to right while the digits stay perfectly still. Only when the marker runs off the right-hand end, and the voltage overflows the top of the band, do the digits change by one step and the marker jump back to the left.

Every voltage inside a band gives exactly the same code, so the converter cannot tell them apart. That lost detail is the quantisation error, and it can be as large as one whole band. More bits means narrower bands and a smaller error, which is why a 12-bit ADC resolves much finer detail than a 3-bit one.

Testing and results

Photos of the LED matrix for five pot settings. Watch the digits and the marker position:

LED matrix showing 000 with the marker at the far left of the bottom row
The display shows 000 with the marker at the far left.

000, bottom of the band: the ADC reading is near 0, so the bucket position is at its minimum and the marker sits at the left.

LED matrix showing 001 with the marker at the far right of the bottom row
The display shows 001 with the marker at the far right.

001, top of the band: the voltage is about to tip into the next band. The marker is at the far right, but the digits still read 001.

LED matrix showing 010 with the marker at the far left of the bottom row
The display shows 010 with the marker at the far left.

010, bottom of the band: the voltage has just crossed the threshold. The code stepped from 001 to 010 and the marker jumped back to the left.

LED matrix showing 101 with the marker in the middle of the bottom row
The display shows 101 with the marker in the middle.

101, middle of the band: the marker sits half way along. Many different voltages in this band all give the same code, 101.

LED matrix showing 111 with the marker at the far right of the bottom row
The display shows 111 with the marker at the far right.

111, top of the range: the highest code, with the marker at the far right as the input approaches full scale.

Notice that going from 001 at the top of its band to 010 at the bottom of the next one takes only a tiny change in voltage, while moving the marker across a whole band changes nothing in the digits.