Dim an LED with a potentiometer

Turn a knob to set the brightness of an LED: read an analog input with analogRead() and set an output with PWM.
  • Level:Beginner
  • Duration: 30 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Dim an LED with a potentiometer – EEMENTOR STEM™ lesson

A button is either on or off, but many things in the real world are somewhere in between: the volume of a speaker, the brightness of a lamp. In this lesson you read a knob, called a potentiometer, as a number from 0 to 1023 and use it to set the brightness of an LED smoothly from off to full.

What you'll learn

  • How a potentiometer works as a voltage divider
  • How to read an analog input with analogRead()
  • How to dim an LED with PWM and analogWrite()
  • How to convert between ranges with map()

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • 10 kΩ potentiometer (UNO R3 Starter Kit)
  • 1 LED and 1 resistor, 220 Ω (UNO R3 Starter Kit)
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Dim an LED with a potentiometer
Breadboard view. Check each connection against the wiring table below.
From To
Potentiometer, left pin GND
Potentiometer, middle pin UNO pin A0
Potentiometer, right pin 5V
UNO pin 9 220 Ω resistor, then the LED's long leg
LED short leg GND

The sketch

const int KNOB_PIN = A0;
const int LED_PIN = 9;    // a PWM pin, marked with ~ on the board

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  int knob = analogRead(KNOB_PIN);              // 0 ... 1023
  int brightness = map(knob, 0, 1023, 0, 255);  // 0 ... 255
  analogWrite(LED_PIN, brightness);

  Serial.print("knob:");
  Serial.print(knob);
  Serial.print(" brightness:");
  Serial.println(brightness);
  delay(50);
}

Upload the sketch and turn the knob. Then open Tools → Serial Plotter to see both numbers as moving lines.

How it works

Inside the potentiometer, a strip of resistive material runs between the two outer pins, and the middle pin is a wiper that slides along it as you turn. With 5 V on one end and GND on the other, the wiper picks off any voltage in between. analogRead() measures that voltage and turns it into a number: 0 for 0 V, 1023 for 5 V.

The UNO can't produce a real in-between voltage on its output pins. Instead, PWM switches the pin on and off about 500 times per second, and analogWrite() sets how much of each cycle the pin is on, from 0 (never) to 255 (always). Your eye averages the fast flashing into a steady brightness. Because the input goes up to 1023 and the output only to 255, map() converts one range into the other.

Try this next

  • Use the knob to set the blink speed of the LED instead of its brightness.
  • Our eyes see small brightness changes better in the dark. Try brightness = brightness * brightness / 255; for a smoother feel.
  • Control a servo's angle with the knob, combining this lesson with the servo lesson.

Troubleshooting

  • The value jumps between 0 and 1023 only: the middle pin isn't on A0, or the outer pins aren't on 5V and GND.
  • The LED only switches on and off: it must be on a PWM pin (3, 5, 6, 9, 10 or 11).
  • Turning right makes it dimmer: swap the wires on the two outer pins.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit