Arduino Uno · Potentiometer

Arduino Uno Potentiometer Simulator: Wiring, Code and a Live Circuit

This is a 10 k potentiometer wired to an Arduino Uno, read live: the serial monitor prints the raw reading and a percent five times a second. Open it in the editor and turn the knob to see both change.

The ends go to 5V and GND and the wiper to pin A0.

  • 5 V logic
  • No library: analogRead()
  • Wiper on pin A0
  • 0 to 1023
Arduino Uno · Potentiometerlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the Potentiometer to the Arduino Uno

Every connection in the circuit above, with the board’s own pin names.

Potentiometer pinArduino Uno pinWhy
Pin 1 (one end)5VThe top of the range: the ADC measures against 5 V.
Pin 2 (wiper)pin A0The middle leg: a voltage between the two ends, set by the shaft.
Pin 3 (other end)GNDThe bottom of the range.

The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023. Any value of pot from 1 k to 100 k works; 10 k is the usual one.

Pins to leave alone on the Arduino Uno

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

On a real bench

On a real bench the Uno stands beside the breadboard: the Potentiometer goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

Reading a potentiometer on Arduino Uno pin A0
const int POT_PIN = A0;   // the wiper, pin A0

void setup() {
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(POT_PIN);            // 0 to 1023
  int percent = map(raw, 0, 1023, 0, 100);
  Serial.print("Raw: ");
  Serial.print(raw);
  Serial.print("  Percent: ");
  Serial.println(percent);
  delay(200);
}

analogRead(POT_PIN) converts the wiper’s voltage to a number. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.

map(raw, 0, 1023, 0, 100) rescales it to a percent with whole-number arithmetic. For a PWM duty use map(raw, 0, 1023, 0, 255), or divide.

Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.

Common mistakes with the Potentiometer on the Arduino Uno

Reading a digital pin

analogRead() needs one of A0 to A5. On a digital pin it reads nothing useful.

Wiring the wiper to an end

The middle leg is the wiper. With an end leg on the analog pin the reading never moves, or jumps from 0 to the top at one end of the travel.

Trusting the last count

A real pot’s reading wobbles by a count or two. Average a few readings, or ignore changes smaller than 3, before driving something the eye or ear will notice.

Questions and answers

Can I simulate a potentiometer with an Arduino Uno?

Yes. The circuit at the top of this page is an Arduino Uno with a potentiometer, running in your browser on a simulated ATmega328P. Drag the knob and the analog pin sees the divided voltage, converted the way the board’s own ADC converts it. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

What range does analogRead() give on the Arduino Uno?

The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.

Which way should the pot turn?

Swap the two end wires to reverse it. The wiper stays in the middle either way.

How do I dim an LED with the pot?

Read the pot, then analogWrite(ledPin, map(raw, 0, 1023, 0, 255)) on a PWM pin marked with a tilde.

Build your own Potentiometer project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.