Using the ADC To read a voltage and display the voltage level
Turn a potentiometer to change a voltage, then watch the Arduino read it and display the voltage level.
Getting started: what you'll need
This section lists everything you need for the worked example below. Equivalent parts will work.
Equipment list
| Part number | Description | Qty |
|---|---|---|
| ABX00087 | Arduino UNO R4 WiFi | 1 |
| USB-C data cable | USB-C to USB-A/C data cable (not charge-only) | 1 |
| BB830 (or equivalent) | 830-point solderless breadboard | 1 |
| Jumper wire kit | Male-to-male breadboard jumper wires, assorted | 1 |
| 10k potentiometer | 10 kΩ slider potentiometer. A 10k pot is also included in the Elegoo starter kit below | 1 |
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 averages 16 readings, converts the ADC count back into a voltage and shows it as X.X volts on the built-in LED matrix, with the count and voltage also sent to the Serial Monitor. 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.
// Create an object to control the built-in LED matrix
ArduinoLEDMatrix matrix;
// 8 rows x 12 columns display buffer
uint8_t frame[8][12];
// 3x5 font for digits 0-9
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 display buffer
// --------------------------------------------------
void clearFrame()
{
memset(frame, 0, sizeof(frame));
}
// --------------------------------------------------
// Draw a digit at a specified X position
// --------------------------------------------------
void drawDigit(int digit, int xOffset)
{
if (digit < 0 || digit > 9) return;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 3; col++)
{
if (digits[digit][row] & (1 << (2 - col)))
{
frame[row + 1][xOffset + col] = 1;
}
}
}
}
// --------------------------------------------------
// Draw decimal point
// --------------------------------------------------
void drawDecimalPoint(int x, int y)
{
frame[y][x] = 1;
}
// --------------------------------------------------
// Setup
// --------------------------------------------------
void setup()
{
matrix.begin();
// ADC: 10-bit (0-1023)
analogReadResolution(10);
Serial.begin(115200);
}
// --------------------------------------------------
// Main Loop
// --------------------------------------------------
void loop()
{
// Take 16 ADC readings and average them
long total = 0;
for (int i = 0; i < 16; i++)
{
total += analogRead(A0);
}
int adc = total / 16;
// Convert ADC reading to voltage
float voltage = adc * (5.0f / 1023.0f);
// Extract digits for X.X display
int whole = (int)voltage;
int decimal = ((int)(voltage * 10)) % 10;
// Clear the display
clearFrame();
// Display voltage as X.X
drawDigit(whole, 0);
drawDecimalPoint(4, 5);
drawDigit(decimal, 6);
// Update LED matrix
matrix.renderBitmap(frame, 8, 12);
// Debug output
Serial.print("ADC = ");
Serial.print(adc);
Serial.print(" Voltage = ");
Serial.println(voltage, 2);
delay(100);
}
// Hosted on https://LittleEngineeringBooks.com for educational purposes.
Using the ADC To read a voltage and display the voltage level
Breadboard schematic
Pinout wiring
The 10k pot pins are 1 (one end), 2 (wiper) and 3 (other end).
| From | To | Purpose |
|---|---|---|
| 5V | 10k pot pin 1 | Top of the voltage divider |
| 0V (GND) | 10k pot pin 3 | Bottom of the voltage divider |
| 10k pot pin 2 (wiper) | Arduino A0 | Divider 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 binary number from 0 (0 V) to 1023 (5 V). To reduce noise, the sketch averages 16 readings. It then converts that count back into a voltage and shows it on the LED matrix as X.X.
Voltage = ADC value × (5 / 1023)
Each ADC step is worth 5 / 1023 ≈ 4.89 mV. The whole number of volts is the integer part of the result, and the digit after the decimal point is found by multiplying by 10 and taking the remainder after dividing by 10. For example, an ADC count of 614 gives 614 × 5 / 1023 ≈ 3.0 V.
Testing and results
Photos of the LED matrix display for three pot settings:
0 V: the wiper is at 0 V, so the ADC reads 0 and 0 × 5 / 1023 = 0.0 V.
3.0 V: the ADC reads about 614 out of 1023, and 614 × 5 / 1023 ≈ 3.0 V.
5.0 V: the wiper is at 5 V, so the ADC reads its maximum of 1023 and 1023 × 5 / 1023 = 5.0 V.