Find a course

Module 3: Analog Input and PWM OutputLesson 6 of 1213 min

PWM: Dimming LEDs and Controlling Power

Understand pulse-width modulation, use analogWrite with values from 0 to 255 to dim LEDs, and connect a knob to brightness.

Step 1 of 13Learn

Faking a dimmer with a fast switch

Digital pins can only be fully on (5 V) or fully off (0 V). So how do you dim an LED to half brightness?

You switch it on and off very fast. If the pin is on for half of each cycle and off for the other half, the LED gets power half the time, and your eye, which can't follow flicker faster than roughly 60 to 100 times per second, sees a steady half-brightness glow. This trick is called pulse-width modulation (PWM).

The key number is the duty cycle: the percentage of each cycle the signal is HIGH.

  • 0% duty: always off
  • 25% duty: on a quarter of the time
  • 100% duty: always on

The average voltage equals the duty cycle times the supply: a 25% duty cycle on a 5 V pin averages 1.25 V.

On the Uno, PWM runs at about 490 Hz on most PWM pins (about 980 Hz on pins 5 and 6). That's far too fast to see, but fast enough to smoothly control LEDs and the speed of motors through a driver. The same idea runs LED dimmers, phone screen brightness and electric car motor controllers.

PWM signals at 25, 50 and 75 percent duty cycle25%50%75%time

Step 2 of 13Learn

analogWrite and the 0 to 255 scale

You produce PWM with analogWrite, which only works on the pins marked ~ (3, 5, 6, 9, 10 and 11 on the Uno):

analogWrite(9, 128);   // about 50% duty cycle

The value runs from 0 to 255 because the timer behind it is 8-bit (28 = 256 levels). So:

  • duty cycle (%) = value / 255 × 100
  • value = duty (%) × 255 / 100

Notice the mismatch with analogRead, which returns 0 to 1023. To make a knob set the brightness, divide the reading by 4, or use map():

void loop() {
  int raw = analogRead(A0);              // 0..1023
  int level = map(raw, 0, 1023, 0, 255); // 0..255
  analogWrite(9, level);
}

Common mistakes: using analogWrite on a pin without a ~ (it ends up fully on above 127 and off below), passing the raw 0 to 1023 value straight in (values above 255 wrap around and behave strangely), and expecting an actual steady voltage on a multimeter. A meter shows the average, but the pin is really flicking between 0 and 5 V.

Despite its name, analogWrite has nothing to do with the analog input pins A0 to A5.

Step 3 of 13Try the calculator

PWM duty cycle calculator

Enter an analogWrite value to see the duty cycle and average voltage.

Duty cycle
25.1 %
Average output voltage
1.25 V

Step 4 of 13Practice · Work it out

What analogWrite value gives a duty cycle of about 40%? Give the nearest whole number.

Step 5 of 13Note

Step 6 of 13Practice · Chart

PWM value vs. how bright it looks

Perceived lightness rises steeply at low duty cycles and flattens near the top.

Tap the analogWrite value that already looks about half as bright as full, even though it gives only 13% of the light.

Tap the point that answers the question, then press Check.

PWM value vs. how bright it looksLine chart with 9 categories: 0: Light output (% of full) 0%, Perceived lightness (%) 0%; 32: Light output (% of full) 13%, Perceived lightness (%) 42%; 64: Light output (% of full) 25%, Perceived lightness (%) 57%; 96: Light output (% of full) 38%, Perceived lightness (%) 68%; 128: Light output (% of full) 50%, Perceived lightness (%) 76%; 160: Light output (% of full) 63%, Perceived lightness (%) 83%; 192: Light output (% of full) 75%, Perceived lightness (%) 90%; 224: Light output (% of full) 88%, Perceived lightness (%) 95%; 255: Light output (% of full) 100%, Perceived lightness (%) 100%.0%20%40%60%80%100%0326496128160192224255analogWrite value
Perceived lightness approximated with the CIE L* formula.
PWM value vs. how bright it looks
analogWrite valueLight output (% of full)Perceived lightness (%)
00%0%
3213%42%
6425%57%
9638%68%
12850%76%
16063%83%
19275%90%
22488%95%
255100%100%

Step 7 of 13Note

Step 8 of 13Practice · Choose one

Which code dims an LED on pin 6 to roughly one quarter brightness?

Step 9 of 13Learn

Fading with a for loop

Here's a sketch that fades an LED up and down, a classic first PWM project:

const int led = 9;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  for (int level = 0; level <= 255; level++) {
    analogWrite(led, level);
    delay(4);
  }
  for (int level = 255; level >= 0; level--) {
    analogWrite(led, level);
    delay(4);
  }
}

Each for loop takes 256 steps, and each step waits 4 ms, so each fade takes about 256 × 4 ≈ 1 second, and one full breath (up and down) takes about 2 seconds.

The same pattern controls anything that responds to average power: the speed of a small fan through a transistor, the brightness of an LED strip through a MOSFET, or the color of an RGB LED, which is really three LEDs (red, green, blue) on three PWM pins. Mixing analogWrite values gives you any color: red 255 and green 128 with blue 0 produces orange.

Step 10 of 13Diagram

The fade loop

The fade loopCycle diagram: Brightness 0 → 255 → Full brightness → Brightness 255 → 0 → Off.loop()repeatsBrightness 0 →255Full brightnessBrightness 255→ 0Off

Tap a shape with to see more.

Brightness 0 → 255

The first for loop raises the value in small steps with a short delay.

Full brightness

The value reaches 255: 100% duty cycle.

Brightness 255 → 0

The second for loop counts back down.

Off

At 0 the LED is dark, then loop() starts again.

Step 11 of 13Note

Step 12 of 13Practice · Match the pairs

Match each analogWrite value to its approximate duty cycle.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. 0
  2. 64
  3. 128
  4. 191
  5. 255

Step 13 of 13Practice · What would you do?

What would you do?

Omar connects a potentiometer to A0 and writes analogWrite(10, analogRead(A0)); The LED brightens as he turns the knob, then suddenly goes dark, brightens again, and repeats four times across the full turn. Why?