hardware//electronics//PWM

Pulse-width modulation (PWM) is a way of encoding a value in a digital signal by the width of its pulses at a fixed repetition rate, and it serves two different jobs in a machine: delivering an analog amount of power from a switch that is only ever fully on or fully off, and carrying a command from a controller to an actuator. A microcontroller timer generates it in hardware, so it costs the CPU nothing once configured.


Pulse-width modulation (PWM) is a way of encoding a value in a digital signal by the width of its pulses at a fixed repetition rate, and it serves two different jobs in a machine: delivering an analog amount of power from a switch that is only ever fully on or fully off, and carrying a command from a controller to an actuator. A microcontroller timer generates it in hardware, so it costs the CPU nothing once configured.

As power modulation, the fraction of each period the switch stays on, the duty cycle, sets the average voltage the load sees. A heater switched on 30 % of every one-second cycle delivers 30 % of its power, because its thermal mass averages the pulses; a motor winding switched at tens of kHz sees a smooth current, because its inductance averages them. The trick works whenever the load is much slower than the switching, and it is efficient because a transistor fully on or fully off dissipates little. It is the continuous version of on-off control: instead of letting a slow process swing in a limit cycle, the switching is made so fast that the process only sees the mean.

As a command signal, PWM is the classic language between a flight controller and its ESCs and servos: a pulse of 1 to 2 ms, repeated at 50 Hz to a few hundred Hz, where 1 ms means zero throttle and 2 ms full. The ESC measures the pulse and its own microcontroller then commutates the brushless motor phases with power PWM at tens of kHz.

Analog pulse timing has limits that matter to control. The receiver must measure a width, so resolution depends on its timer and on calibration of the endpoints, and the update rate caps how fast a new command arrives. Faster analog variants shortened the pulses; digital protocols such as DShot replaced them with a checksummed number of 11 bits (about 2,000 throttle levels) that needs no calibration.

The repetition rate is a sampling rate for the actuator: a command held for one PWM period is a zero-order hold, adding about half a period of delay to the loop.

Switching edges are a source of electrical noise; current-sense and analog readings are often timed to sample in the middle of a PWM period, away from the edges.