A digital pin can only do two things: on, or off. Yet the same pin dims an LED, sets a motor's speed, aims a servo, and even sculpts a sine wave. The trick is timing — and it's called PWM. Six live panels take you from the first square wave to the algorithm inside a motor drive. Drag every slider.
Microcontrollers speak in absolutes. A pin is either at the supply voltage or at ground — 5 volts or 0 volts, high or low, nothing in between. That is wonderful for logic and useless for the real world, where we want a lamp at half brightness or a motor at a third of full speed.
Pulse width modulation is the bridge. Instead of finding a voltage between the two, we flip the pin on and off very fast and control how much of the time it spends on. Blink quickly enough and nothing downstream — an LED, a coil, your eye — can keep up with the individual pulses. What they feel is the average. Change the ratio of on-time to off-time and you change that average smoothly, from nothing to everything, using a pin that never left its two states.
The fraction of each cycle the signal is high. This is the knob that sets the average.
How many cycles happen per second. Fast enough and the pulses disappear into a steady effect.
What the load actually experiences: duty cycle times the supply voltage.
Every cycle is split into an ON part and an OFF part. The duty cycle is the ON fraction, from 0% to 100%. Slide it and watch the average voltage — the dashed line — rise and fall with it. The LED tracks that average exactly.
Because the load responds to the average, the whole of basic PWM collapses into a single relationship. If the supply is V and the signal is high for a fraction D of every cycle, the average voltage delivered is simply their product.
At 40% duty on a 5 volt supply the load sees 2 volts on average. Nothing about the pin changed — it still swings the full 0-to-5. We only changed the schedule.
Duty sets the average; frequency sets how fast we deliver it. At a few hertz the LED visibly blinks. Raise the frequency and the blink speeds up, blurs, and — past roughly 50 Hz — fuses into a steady glow your eye reads as brightness. The average never changed; only your ability to see the seams.
Frequency is not a free choice. Too low and everything flickers — lights strobe, motors cog, servos buzz. Too high and switching losses climb and the driving transistor heats up. So every application has a home range: LEDs live happily from a few hundred hertz up, DC motors often run in the tens of kilohertz to stay silent, and hobby servos famously expect exactly 50 Hz. The period, T = 1/f, is just the length of one cycle in time.
An LED and a motor average the pulses for us because they're slow. To get a genuinely steady voltage — to drive an amplifier or a sensor — we add a resistor and capacitor: a low-pass filter. It literally computes the running average. Heavier smoothing flattens the ripple but reacts more slowly to change.
No processor sits in a loop toggling a pin and counting microseconds — it would have time for nothing else. Instead a dedicated hardware timer does it, free of the CPU. The idea is beautifully simple: a counter ramps up, rolls over, and repeats. Alongside it sits a compare value you set. While the counter is below the compare value the pin is high; once it passes, the pin goes low. That's a whole PWM channel, in silicon.
The blue ramp is the timer counting from 0 up to its TOP value, then wrapping. Where your compare level crosses it decides the duty cycle. TOP is set by the resolution: an 8-bit timer has 256 steps, a 16-bit timer over 65,000. More bits means finer duty control — but the same fixed set of achievable values.
This is why datasheets talk about PWM in bits. Resolution is the number of distinct duty cycles you can command, and at low bit-depth you can feel the gaps — a dimming LED steps instead of glides. The same counter, incidentally, is what makes precise timing possible: because the hardware measures pulse width in fixed clock ticks, it can hit a servo's microsecond target far more reliably than any software loop.
A hobby servo bends the rules: here the pulse width is a command, not an average. Every 20 ms it wants one pulse. A 1.0 ms pulse means full one way, 1.5 ms is centre, 2.0 ms is full the other way. The servo's own electronics turn that width into an angle and hold it. Duty cycle here is almost irrelevant — the message is entirely in the timing.
If PWM only ever made a steady average, it would stop here. But the average can change over time. Vary the duty cycle smoothly — high, then low, then high again, following a sine curve — and the filtered output becomes an actual alternating voltage. This is sinusoidal PWM, and it's how a battery drives a three-phase motor, how a solar inverter makes grid AC, and how a class-D amplifier reproduces music.
The mechanism is a comparator race. A slow reference sine is compared against a fast triangle carrier. Whenever the sine is above the triangle, the output is high; otherwise low. Because the sine spends more time near its peaks, the pulses fatten there and thin near the troughs — and the average, once filtered, is the sine itself.
Top: a reference sine (blue) racing a triangle carrier (grey). Middle: the comparator output — a PWM train whose width breathes with the sine. Bottom: run that through a filter and the sine reappears. Modulation index scales the sine's amplitude; the carrier ratio sets how many switching pulses fit into one output cycle.
From one square wave to a whole waveform. That is the arc of PWM. A pin that can only choose between two voltages, switched on a schedule, gives you a dimmer, a throttle, a positioner, a digital-to-analog converter, and finally an AC source. The hardware never got more capable. We just got smarter about when to flip the switch.
Two servos, real PWM, and a guidebook that derives duty cycle, timer resolution, and pulse timing from first principles — then has you drive them yourself.
View the Kit