← Back to notes

Pulse Width Modulation & analogWrite()

A digital pin can only be 0 V or 5 V. PWM switches it on and off very fast; the fraction of time it is ON (the duty cycle) sets the average voltage the LED "sees".

Set the output

Duty cycle = on time / period × 100
On time / off time (period ≈ 2.04 ms @ 490 Hz)
Average output voltage

Pin voltage vs time

Red: the actual pin voltage. Dashed blue: the average voltage. Your eye (and a motor's inertia) responds to the average, so the LED looks dimmer rather than flickering.

Which pins can do PWM? (Arduino Uno R3)

Click a digital pin. PWM pins are marked with a ~ on the board.