Sub topic 7
Reading a Potentiometer with `analogRead()`
This chapter assumes you know the basic Arduino sketch structure, can upload a sketch and open the Serial Monitor, and understand voltage, ground, breadboard wiring, pinMode(), and digital outputs.
A potentiometer is a variable resistor with three terminals. When its two outer terminals connect to 5 V and GND, the center terminal produces a voltage between 0 V and 5 V as you turn the knob. That center terminal connects to an Arduino analog input such as A0.
What `analogRead()` measures
On an Arduino Uno, analogRead(A0) uses an analog-to-digital converter (ADC). An ADC changes a voltage into a number that a program can use.
The Uno has a 10-bit ADC. It reports 1,024 possible values:
| Input voltage | Typical raw reading |
|---|---|
| Near 0 V | 0 |
| About 2.5 V | 512 |
| Near 5 V | 1023 |
The reading is an integer, so it does not directly contain a unit such as volts. For an Uno using a 5 V reference, you can estimate voltage with:
voltage = raw reading × 5.0 ÷ 1023.0The reference voltage is the voltage the ADC treats as its maximum input. The exact value may differ from the nominal supply voltage, so this calculation is an estimate unless you measure or configure the reference accurately.
Wire and read the potentiometer
Use the potentiometer as a voltage divider:
- One outer terminal to
5V. - The other outer terminal to
GND. - The center terminal to
A0.
If the knob turns in the opposite direction from what you expect, swap the two outer connections. Never connect the center terminal directly to 5 V or GND while using it as an adjustable input.
Upload this complete sketch:
const int POT_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT_PIN);
float volts = raw * (5.0 / 1023.0);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" volts=");
Serial.println(volts, 2);
delay(100);
}analogRead() returns a value from 0 to 1023 on an Uno. The Serial.print() calls send the value to the computer. The final argument 2 in Serial.println(volts, 2) displays two digits after the decimal point.
Open the Serial Monitor and select 9600 baud. Turn the knob slowly. The raw value should move across most of the range, and the calculated voltage should move between approximately 0 V and 5 V.
Common errors
- Reading stays near 0: Check that the center terminal reaches
A0and that the circuit has a shared ground. - Reading stays near 1023: Check for a mistaken connection to 5 V.
- Reading changes in the wrong direction: Swap the outer potentiometer terminals.
- Reading jumps slightly: Small changes are normal. Loose wires, electrical interference, and the potentiometer’s mechanical contact can add noise.
- Values exceed the expected range: Confirm that the selected analog pin is correct and that the input is not being driven above the board’s permitted voltage.
The voltage formula depends on the board. A board with a 12-bit ADC may return 0 to 4095 instead of 0 to 1023. Always check the board’s ADC resolution and reference voltage before converting a raw reading to volts.
Exercise
On an Arduino Uno, what voltage does a raw reading of 768 represent when the reference voltage is 5 V?
Show answer
Approximately 3.75 V, calculated as 768 × 5.0 ÷ 1023.0.