A potentiometer on Arduino Uno

Steps done: 0 of 5

~9GNDUNOA05VATMEGA328P-PU

Guess the answer and press Run

A0512D9, PWM0 %time, s0.0

Lesson steps

What will the monitor show with the knob in the middle?

Pick an answer before you run it.

The knob is in the middle. What will the serial monitor print?

Sketch

pot_dimmer.inoTo board
// Potentiometer: outer pins to GND and 5V, wiper to A0
// LED on D9 through 220 Ω

const int pot = A0;
const int led = 9;
int maxBright = 255;  // brightness with the knob all the way up, 0-255
int waitMs = 100;     // pause between readings, ms

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

void loop() {
  int raw = analogRead(pot);
  int bright = map(raw, 0, 1023, 0, maxBright);
  analogWrite(led, bright);
  Serial.println(raw);
  delay(waitMs);
}

Tap a line or an orange number

Serial monitor

Serial, 9600 baudboard time 0.0 s

About the lesson

A potentiometer between 5V and GND, the middle pin on A0, an LED on D9. You turn the knob on the scene with a mouse or a finger, analogRead() reads the voltage, analogWrite() sets the brightness. About 12 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.

Build it for real

Parts

  • Arduino Uno R3
  • Mini breadboard
  • 10 kΩ potentiometer
  • 5 mm LED
  • 220 Ω resistor
  • 5 male-to-male jumper wires

Wires

FromTo
Left potentiometer pinGND
Middle pinA0
Right pin5V
D9220 Ω resistor, then the LED anode
LED cathodeGND

If the brightness grows counterclockwise, swap the wires on the outer pins of the potentiometer.

The sketch below goes to the board with the default numbers: maxBright 255, waitMs 100. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.

Flash it to your own board

The whole sketch

// Potentiometer: outer pins to GND and 5V, wiper to A0
// LED on D9 through 220 Ω

const int pot = A0;
const int led = 9;
int maxBright = 255;  // brightness with the knob all the way up, 0-255
int waitMs = 100;     // pause between readings, ms

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

void loop() {
  int raw = analogRead(pot);
  int bright = map(raw, 0, 1023, 0, maxBright);
  analogWrite(led, bright);
  Serial.println(raw);
  delay(waitMs);
}

What to keep in mind on a real board

Any potentiometer from 1 to 100 kΩ works: a current of 5 V / R flows through it, and the A0 input takes less than 1 µA. With 10 kΩ that is 0.5 mA.

PWM works only on the pins marked ~: 3, 5, 6, 9, 10, 11. On the others, analogWrite() gives HIGH from 128 up and LOW below.

The sketch takes 2568 bytes of flash and 192 bytes of RAM.

Licenses of the lesson code

The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.

The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.

MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.