EmbeddedRelated.com

PWM

Category: Peripherals | Also known as: pulse-width modulation

Pulse-width modulation (PWM) is a technique for encoding a variable analog-equivalent level as a fixed-frequency digital signal by varying the fraction of each period during which the output is high (the duty cycle). The ratio of on-time to total period determines the effective average voltage or power delivered to a load.

In practice

Most MCUs include dedicated PWM peripherals or timer outputs that function as PWM sources, typically implemented as a timer configured to count to a period register and toggle an output pin when the count matches a compare register. Common peripheral names include TIM (STM32), TPM/FTM (Kinetis), TCB (AVR-DA/DB), and MCPWM (ESP32). Resolution varies from 8-bit on simpler parts to 16-bit or more on higher-end devices; some specialized motor-control SoCs provide higher effective resolution through timer clocking arrangements, though true high-resolution PWM depends heavily on peripheral clock rate, timer mode, and topology.

PWM is used in a wide range of applications: DC motor speed control, brushless motor commutation (using complementary outputs with dead-time insertion to prevent shoot-through), LED brightness dimming, switching power supply control, servo positioning (typically 50 Hz, 1-2 ms pulse width), and audio generation. For LED dimming, PWM avoids the color-shift that occurs with analog current reduction. For driving WS2812 addressable RGB LEDs, the protocol uses a tightly timed one-wire waveform that is not PWM in the usual sense; DMA-assisted bitstream generation or bit-banging is often used to meet its strict timing requirements, as covered in "Introduction to Microcontrollers - Driving WS2812 RGB LEDs."

A key parameter is the PWM frequency. Higher frequencies reduce visible flicker in LEDs and audible noise in motors and inductors, but increase switching losses in FETs. For motor control, frequencies in the low-to-mid tens of kHz are common, with the specific choice depending on motor type, acoustics, and efficiency goals; for servo control, 50 Hz is standard; for switching regulators, 100 kHz to several MHz is typical depending on topology. The choice of frequency also determines the available resolution for a given peripheral clock: for a simple edge-aligned timer with no prescaler, resolution (bits) ≈ log2(f_clk / f_pwm), though actual resolution also depends on timer mode, prescalers, period register width, and whether center-aligned operation is used.

A common pitfall is confusing software (bit-banged) PWM with hardware PWM. Software PWM ties up the CPU and is sensitive to interrupt latency, causing jitter. Hardware PWM runs autonomously after configuration and produces output that is far less susceptible to jitter than software approaches, though update timing, register buffering, and misconfiguration can still introduce artifacts. Another pitfall is failing to configure dead-time on complementary outputs in H-bridge or half-bridge motor drivers, which can cause shoot-through and destroy switching transistors. "What is Pulse Width Modulation and How Does It Work?" provides a thorough grounding in these fundamentals.

Discussed on EmbeddedRelated

Frequently asked

What is duty cycle and how does it relate to output voltage?
Duty cycle is the ratio of the on-time to the total period, expressed as a percentage. For a 3.3 V logic output, a 50% duty cycle produces an average of 1.65 V across a purely resistive or heavily filtered load. This relationship is linear: average output voltage = duty cycle x supply voltage. Note that this average only approximates the load behavior if the load acts as a low-pass filter (e.g., an RC network, inductor, or motor winding); a bare LED driven at 50% duty cycle sees full pulses, not half voltage.
How do I choose the right PWM frequency?
The right frequency depends on the application. For LED dimming, 200 Hz to 1 kHz is usually sufficient to avoid visible flicker, though some high-speed camera applications require 1 kHz or higher. For RC servos, 50 Hz is standard. For DC motor control, 10 kHz to 50 kHz is a common range that pushes noise above the audible band while keeping switching losses manageable. For switching regulators, frequency is often 100 kHz to several MHz. Higher frequency also reduces available bit-depth for a given peripheral clock, so there is always a resolution/frequency trade-off.
Can I generate PWM in software instead of using a hardware peripheral?
Yes, but with significant trade-offs. Software (bit-banged) PWM, discussed in 'Introduction to Microcontrollers - More On GPIO', works by toggling a GPIO in a timed loop or from a timer ISR. It consumes CPU cycles proportional to the update rate and is susceptible to jitter whenever interrupts or other tasks delay the toggle. Hardware PWM runs autonomously once configured, freeing the CPU and producing clean edges. For applications sensitive to timing -- motor drives, switching regulators, servo control -- hardware PWM is strongly preferred.
What is complementary PWM and why does dead-time matter?
Complementary PWM drives two outputs 180 degrees out of phase, used to control the high-side and low-side switches of a half-bridge or H-bridge. If both switches turn on simultaneously even for nanoseconds during a transition, a low-impedance path forms from supply to ground (shoot-through), which can destroy the FETs. Dead-time insertion adds a brief period where both outputs are off during the transition. Many motor-control timer peripherals (e.g., STM32 advanced timers TIM1/TIM8, Kinetis FTM) include hardware dead-time generation with configurable delay.
How does PWM differ from other modulation schemes used in embedded systems?
PWM varies on-time while keeping frequency constant. Related alternatives include pulse-frequency modulation (PFM), which typically varies frequency while on-time may also change depending on the implementation, and pulse-density modulation (PDM), which varies the density of pulses over time and is used in class-D audio amplifiers and MEMS microphone interfaces. Sigma-delta modulation is a form of PDM used in high-resolution ADCs and DACs. 'Modulation Alternatives for the Software Engineer' covers several of these approaches and their trade-offs.

Differentiators vs similar concepts

PWM is sometimes conflated with PDM (pulse-density modulation) and PFM (pulse-frequency modulation). PWM holds frequency constant and varies pulse width; PFM generally varies frequency, though on-time behavior varies by scheme and implementation; PDM varies the number of pulses per unit time at fixed width. In practice, switching regulators often switch between PWM mode at heavy loads and PFM mode at light loads for efficiency, so both terms appear in the same datasheet. PWM is also distinct from PAM (pulse-amplitude modulation), which varies signal amplitude rather than timing.