What PWM does
Pulse-width modulation switches a voltage fully on and fully off many times per second. The share of each period spent on is the duty cycle. Because the switch itself wastes almost no power, PWM is the standard way to dim LEDs, control motor speed, drive heaters and build switching power supplies.
Key formulas
- Period T = 1 ÷ frequency
- On-time = duty × T, off-time = (1 − duty) × T
- Average voltage = duty × V
- RMS voltage = V × √duty
- Power in a resistor R = duty × V² ÷ R
The average sets the speed of a motor or the brightness perceived through a filter. The RMS value sets the heating of a resistive load, which is why it is higher than the average.
Example
A 12 V supply at 1 kHz with a 25% duty cycle:
| Quantity | Value |
|---|---|
| Period | 1,000 µs |
| On-time | 250 µs |
| Average voltage | 3 V |
| RMS voltage | 6 V |
| Power into 10 Ω | 3.6 W |
| 8-bit compare value | 64 of 255 |
The power is a quarter of the 14.4 W the resistor would take at full on, while a steady 3 V supply would give only 0.9 W. This difference matters when you size resistors or heaters.
Choosing a frequency
- LEDs: above about 200 Hz to avoid visible flicker; above 1 to 2 kHz for cameras.
- DC motors: often 15 to 25 kHz, above the audible range, to avoid whine.
- Switching regulators: from 100 kHz to several MHz to keep inductors small.
- Servos: a 50 Hz frame with a 1 to 2 ms pulse, which is position coding rather than average power.
Timer resolution
Microcontroller timers count from 0 to 2ⁿ − 1. The compare value sets the duty cycle: 8 bits gives 256 steps, 10 bits 1,024 and 16 bits 65,536. Higher resolution at the same timer clock lowers the maximum PWM frequency: f = f_timer ÷ 2ⁿ.