Projects / PWM LED dimmer

PWM LED dimmer

Beginner40 minGuided buildReviewed
average 2.51 V5 V0 V
analogRead = 512duty = 128 / 255 (50%)average on D9 ≈ 2.51 V

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 4 done
  1. UNO

    Place the potentiometer

    Put the three pins in three separate rows.

  2. UNO

    Make the divider

    One outer pin to 5V, the other to GND, the middle pin to A0.

  3. UNO12.7 mA

    Add the LED on a PWM pin

    D9 to the 220 Ω resistor, resistor to the LED's long leg, short leg to GND.

  4. UNO

    Upload and turn the knob

    Upload, open the serial monitor at 9600 baud and turn the knob end to end.

In short

How do you build a PWM LED dimmer?

Turn a potentiometer to set an LED's brightness with PWM on an Arduino Uno. You need Arduino Uno R3, Potentiometer 10 kΩ, LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points. It takes 4 steps and about 40 minutes.

Level
Beginner
Time
About 40 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Potentiometer outer pinUno 5VTop of the divider
Potentiometer other outer pinUno GNDBottom of the divider
Potentiometer wiper (middle)Uno A0Analog input
Uno D9 (PWM ~)220 Ω resistor, then LED anodePWM output
LED cathodeUno GNDReturn path

Code

Arduino IDE, board "Arduino Uno". No extra libraries.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int POT_PIN = A0;
const int LED_PIN = 9;   // a PWM pin (~)

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

void loop() {
  int raw = analogRead(POT_PIN);          // 0 to 1023
  int duty = map(raw, 0, 1023, 0, 255);   // 0 to 255
  analogWrite(LED_PIN, duty);
  Serial.print("raw="); Serial.print(raw);
  Serial.print("  duty="); Serial.println(duty);
  delay(100);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Knob fully one wayraw near 0, duty 0, LED off
Knob fully the other wayraw near 1023, duty 255, LED fully on
Knob in the middleraw about 512, duty about 127, LED visibly dimmer
Measure D9 with a multimeter at mid positionAbout 2.5 V average (the meter averages the PWM)

If it does not work

Brightness jumps or does not change

Check the LED is on a PWM pin marked ~ (3, 5, 6, 9, 10, 11).

raw stuck at 0 or 1023

The wiper is not on A0, or an outer pin is not connected.

Brightness changes backwards

Swap the two outer pins of the potentiometer.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the PWM LED dimmer?

Arduino Uno R3, Potentiometer 10 kΩ, LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points.

How long does the PWM LED dimmer take to build?

About 40 minutes for a beginner maker, in 4 steps.

How do I know the PWM LED dimmer works?

Knob fully one way: raw near 0, duty 0, LED off. Knob fully the other way: raw near 1023, duty 255, LED fully on. Knob in the middle: raw about 512, duty about 127, LED visibly dimmer.

Brightness jumps or does not change: what should I check?

Check the LED is on a PWM pin marked ~ (3, 5, 6, 9, 10, 11).

raw stuck at 0 or 1023: what should I check?

The wiper is not on A0, or an outer pin is not connected.

Brightness changes backwards: what should I check?

Swap the two outer pins of the potentiometer.