Home / Learn Embedded C / PWM for LED Dimming

PWM for LED Dimming

Using PWM duty cycle to control how bright an LED appears.

In shortUsing PWM duty cycle to control how bright an LED appears.

What's actually going on

Definition

Driving an LED with PWM instead of a plain on/off signal lets its apparent brightness be set anywhere between fully off and fully on, by varying the duty cycle.

Why you need this

A plain GPIO pin can only make an LED fully on or fully off; graded brightness (for a dimmer, a breathing effect, or brightness matched to ambient light) needs PWM.

How it works

The LED physically still switches fully on and off very rapidly (fast enough the human eye cannot perceive the flicker) — perceived brightness is the eye averaging the fraction of time it was lit.

Where you'll see this

  • Brightness control
  • "Breathing" LED effects
  • Backlight dimming

Try it

A simple "breathing" brightness ramp (simulated)C: edit and run

A real breathing effect would ramp much more finely and continuously, with small delays between steps.

Try it: stepping brightness down instead of up

A dimming-down ramp from full brightness to offC: edit and run

Running the same ramp in reverse fades the LED down instead of up — the duty cycle is the only thing that changes.

Test what you just read

1. Why does an LED appear dimmer at a lower PWM duty cycle, even though it is either fully on or fully off at any instant?