Embedded Systems

PWM and Duty Cycle

A microcontroller's digital pin outputs either 0 V or 3.3 V. Switching rapidly lets a load respond to the average drive. The fraction of each cycle spent high is the duty cycle.

A simulator linking PWM duty cycle, waveform, average voltage, and LED brightness

1. Set duty cycle and frequency

30%
PWM frequency

2. Choose the viewing mode

Move the sliders to change the high interval and average-voltage line. Slow motion reveals the LED switching fully on and off.

Average voltage = duty cycle × supply voltage

A 3.3 V output high for 30% of a cycle averages 0.3 × 3.3 V = 0.99 V. Rapid LED switching appears as a steady brightness; a motor's electrical and mechanical behavior smooths the pulsed drive. Motor PWM often uses frequencies above 20 kHz to reduce audible noise.

Generating PWM with an STM32 timer

The counter repeatedly counts from 0 through ARR, with the output high while the count is below CCR. ARR sets the period and CCR the duty cycle. PSC divides the timer clock so the period fits the available counter range.

PWM frequency = timer clock / ((PSC + 1) × (ARR + 1)); Duty cycle = CCR / (ARR + 1)