Read a knob on an Arduino Uno
A potentiometer’s wiper on A0, read with analogRead five times a second and printed on the serial monitor as a count from 0 to 1023 and in millivolts. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Reading a voltage with
analogRead - How a potentiometer makes a variable voltage
- The 0 to 1023 range of a 10-bit reading
- Converting counts to millivolts
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A potentiometer has 5V on one end and ground on the other. Its middle pin, the wiper, goes to A0. Turning the knob slides the voltage on A0 anywhere from 0 V to 5 V.
Five times a second the sketch reads A0 with analogRead, which gives a count from 0 to 1023. It also converts that count to millivolts and prints both to the serial monitor. The conversion is the count times 5000, divided by 1023.
Parts list
3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
Kits that can build it
These 11 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- SunFounder Universal Maker Sensor Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Potentiometer pin 3 | Arduino Uno R3 pin 5V |
| Potentiometer pin 1 | Arduino Uno R3 pin GND |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Analog: read a voltage on A0 and print it five times a second.
//
// Wire a potentiometer with its ends to 5 V and GND and its wiper to A0, or
// just two resistors making a divider. analogRead returns 0..1023 for 0..5 V
// against the AVCC reference: a 10-bit conversion at 125 kHz, 104 us, which
// analogRead waits out.
//
// The millivolts are worked out in integer arithmetic. There is no soft-float
// runtime here, so a sketch cannot multiply floats, but it can print one,
// and (raw * 5000) / 1023 needs nothing but a 32-bit multiply and divide.
const int SENSOR_PIN = A0;
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: analogRead on A0");
}
void loop() {
int raw = analogRead(SENSOR_PIN);
long millivolts = (long)raw * 5000L / 1023L;
Serial.print("A0 = ");
Serial.print(raw);
Serial.print(" (");
Serial.print(millivolts);
Serial.println(" mV)");
delay(200);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens serial at 115200 baud and prints a line saying the sketch reads A0. An analog input needs no pin mode setup.
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: analogRead on A0");
}loop sketch.ino
Reads SENSOR_PIN into raw, then works out millivolts as raw * 5000 / 1023 using long numbers so it cannot overflow. It prints both values and waits 200 ms.
void loop() {
int raw = analogRead(SENSOR_PIN);
long millivolts = (long)raw * 5000L / 1023L;
Serial.print("A0 = ");
Serial.print(raw);
Serial.print(" (");
Serial.print(millivolts);
Serial.println(" mV)");
delay(200);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The reading never changes This project
The wiper (middle pin 2) must go to A0, with 5V and GND on the two ends. - Garbled text in the monitor This project
Set the monitor to 115200 baud to matchSerial.begin. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.