Unit content
Pulse-width modulation
Pulse-width modulation (PWM) represents a controllable level using a periodic digital signal whose fraction of time in the active state can be varied.
The duty cycle is
$$D=\frac{t_{on}}{T},$$
where $T$ is the period. A $25%$ duty cycle keeps the output active for one quarter of each period.
A hardware timer can generate PWM by resetting at the period boundary and changing an output when the counter reaches a compare value. Hardware then maintains the waveform without software toggling the signal at every edge.
Loads that average energy over many cycles, such as LEDs or some motors, can respond roughly to the duty cycle rather than to each individual pulse. An analog filter can similarly recover a smoother average voltage from a sufficiently fast PWM waveform.
PWM frequency and duty-cycle resolution trade against each other for a fixed timer clock and counter width.
PWM is therefore a digital timing technique for controlling average delivered energy or approximating an analog level.