PWM (Pulse-Width Modulation)

Pulse-width modulation is the trick that lets a strictly digital device produce what looks like an analog output. A digital pin can only be fully on or fully off, yet a motor needs to run at half speed and an LED needs to glow at a third of full brightness. PWM bridges that gap by switching the pin on and off very rapidly and adjusting how long it stays on within each repeating cycle. If the pin is high for half of every cycle, the load receives, on average, half the power; the device responds to that average and behaves as if it were given a smooth analog level.

The controlling quantity is the duty cycle, the percentage of each period during which the signal is high. A 25 percent duty cycle means the pin is on for a quarter of each cycle and off for the rest; a 75 percent duty cycle means on for three quarters. The repetition rate, or frequency, is chosen high enough that the load cannot react to the individual pulses and instead integrates them. A motor’s inertia and an LED’s interaction with the human eye both perform that averaging naturally, which is why the switching is invisible and the result appears continuous.

On real hardware, PWM is generated by a timer/counter peripheral rather than by software toggling a pin in a loop, which would be imprecise and waste the processor. The Raspberry Pi’s BCM2835 ARM Peripherals datasheet documents a dedicated PWM block whose registers set the range, defining the length of a cycle, and the data value, defining how much of that cycle is high. Once configured, the hardware emits the pulse train on its own and the processor is free to do other work, only updating a register when it wants to change the level.

The three canonical uses each rely on a different reading of the same pulse train. For LED dimming, duty cycle maps almost directly to perceived brightness. For DC motor speed, the average voltage delivered to the motor scales with duty cycle, so a higher duty cycle spins the motor faster. For hobby servos, the meaning is different and stricter: the servo reads the width of a single high pulse, typically between one and two milliseconds repeated about fifty times a second, and moves its shaft to a position determined by that pulse width.

PWM is one of the most quietly important ideas in embedded computing because it turns the simplest possible output, a pin that is on or off, into precise control over physical quantities, and it does so without any expensive digital-to-analog converter. That efficiency is why PWM outputs are standard on microcontrollers like the AVR family behind the Arduino, and why a single inexpensive chip can dim lights, drive motors, control power supplies, and command servos all at once.