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.
Beginner Runs in your browser. Free, and no account needed.
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);
}
Parts list
3 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Potentiometer
How it is wired
3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 5V; Potentiometer pin 3
- Ground: Arduino Uno R3 pin GND; Potentiometer pin 1
- Arduino Uno R3 pin A0; Potentiometer pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.
Free forever. Every board and part. Saves your projects. Works on Chromebooks.