ADC Worked Example

Using the ADC to read a voltage and display the binary value

Turn a potentiometer to change a voltage, then watch the Arduino's ADC turn it into a number.

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 the voltage on A0 with the UNO R4's analogue-to-digital converter and shows the raw ADC count (0 to 1023) on the built-in LED matrix and in the Serial Monitor. Use the Copy button, paste it into the Arduino IDE and upload.

#include "Arduino_LED_Matrix.h"

// Code provided on https://littleengineeringbooks.com for educational purposes only
// Create an object to control the built-in LED matrix
ArduinoLEDMatrix matrix;

// Create an 8-row by 12-column display buffer.
//
// Each element can be:
// 0 = LED OFF
// 1 = LED ON
//
// We draw into this buffer first, then send it to the matrix.
uint8_t frame[8][12];

// Simple 3x5 font for the digits 0-9.
//
// Each digit is made of 5 rows.
// Each row contains 3 bits:
//
// Example:
// 0b111 = ###
// 0b101 = # #
//
// Digit 0:
//
// ###
// # #
// # #
// # #
// ###
//
// This allows us to draw numbers on the matrix.
const uint8_t digits[10][5] = {
  {0b111, 0b101, 0b101, 0b101, 0b111}, // 0
  {0b010, 0b110, 0b010, 0b010, 0b111}, // 1
  {0b111, 0b001, 0b111, 0b100, 0b111}, // 2
  {0b111, 0b001, 0b111, 0b001, 0b111}, // 3
  {0b101, 0b101, 0b111, 0b001, 0b001}, // 4
  {0b111, 0b100, 0b111, 0b001, 0b111}, // 5
  {0b111, 0b100, 0b111, 0b101, 0b111}, // 6
  {0b111, 0b001, 0b010, 0b010, 0b010}, // 7
  {0b111, 0b101, 0b111, 0b101, 0b111}, // 8
  {0b111, 0b101, 0b111, 0b001, 0b111}  // 9
};

// --------------------------------------------------
// Clear the display buffer
// --------------------------------------------------
void clearFrame()
{
  // Set every element in the frame to 0
  memset(frame, 0, sizeof(frame));
}

// --------------------------------------------------
// Draw a single digit at a chosen X position
// --------------------------------------------------
void drawDigit(int digit, int xOffset)
{
  // Ignore invalid digits
  if (digit < 0 || digit > 9) return;

  // Go through the 5 rows of the digit
  for (int row = 0; row < 5; row++)
  {
    // Go through the 3 columns of the digit
    for (int col = 0; col < 3; col++)
    {
      // Check if the corresponding bit is set
      if (digits[digit][row] & (1 << (2 - col)))
      {
        // Turn on an LED in the display buffer
        frame[row + 1][xOffset + col] = 1;
      }
    }
  }
}

// --------------------------------------------------
// Draw a decimal point
// --------------------------------------------------
void drawDecimalPoint(int x, int y)
{
  frame[y][x] = 1;
}

// --------------------------------------------------
// Runs once when the Arduino starts
// --------------------------------------------------
void setup()
{
  // Start the LED matrix
  matrix.begin();

  // Use a standard 10-bit ADC
  //
  // ADC values:
  // 0V = 0
  // 5V = 1023
  analogReadResolution(10);

  // Start Serial Monitor for debugging
  Serial.begin(115200);
}

// --------------------------------------------------
// Main program loop
// --------------------------------------------------
void loop()
{
  int adc = analogRead(A0);

  // Split into 4 decimal digits
  int thousands = adc / 1000;
  int hundreds  = (adc / 100) % 10;
  int tens      = (adc / 10) % 10;
  int ones      = adc % 10;

  clearFrame();

  // Draw four touching digits
  drawDigit(thousands, 0);
  drawDigit(hundreds, 3);
  drawDigit(tens, 6);
  drawDigit(ones, 9);

  matrix.renderBitmap(frame, 8, 12);

  Serial.println(adc);

  delay(50);
}
Using the ADC to read a voltage and display the binary value

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 Arduino's on-board 5 V and 0 V are connected across the ends of a 10 kΩ potentiometer, making a voltage divider. As you turn the pot, the wiper picks off a voltage anywhere between 0 V and 5 V, depending on its position. The wiper is connected to analogue input A0, which is read by the Arduino's analogue-to-digital converter (ADC).

The ADC is set to 10 bits, so it turns the voltage into a number from 0 (0 V) to 1023 (5 V). The sketch shows that number on the LED matrix. (Curious how ADCs work? See the converters book!)

Voltage = ADC value × (5 / 1023)

Testing and results

Photos of the LED matrix display for two pot settings:

Arduino UNO R4 LED matrix showing 0779 with the potentiometer turned up
The LED matrix showing 0779.

0779: the ADC reads 779 out of 1023, which corresponds to about 779 × 5 / 1023 ≈ 3.81 V on the wiper, so the pot is turned about three-quarters of the way towards 5 V.

Arduino UNO R4 LED matrix showing 0477 with the potentiometer near the middle
The LED matrix showing 0477.

0477: the ADC reads 477 out of 1023, which corresponds to about 477 × 5 / 1023 ≈ 2.33 V, so the pot is a little under halfway (halfway would read about 512, or 2.5 V).