Prefer to click through the interactive model?
PWM — study guide
The same fragments the interactive model serves, read in order. One source, two views.
Getting an in-between out of a pin that only knows two
A digital output pin has exactly two settings. It can drive its line up near the supply voltage, a logic HIGH, or down to ground, a logic LOW, and that is the entire menu. There is no dial on it for half-bright or three-quarter speed; the hardware simply does not carry an in-between level.
Yet builds constantly need in-between. You want an indicator glowing softly instead of blazing, a motor turning at a gentle crawl instead of flat out, a control voltage set somewhere between zero and full. Pulse-width modulation (PWM) is the trick that gets a continuous-looking level out of a pin that only knows two, and it does it with time instead of voltage.
The idea is to stop asking the pin to hold a middle value and instead switch it between full-on and full-off very fast, over and over. Spend a lot of each cycle high and only a little low, and whatever the pin drives behaves as though it is getting most of the supply; flip that ratio and it behaves as though it is getting only a little. like a light blinked on and off far faster than the eye can follow: you stop seeing separate flashes and see one steady brightness set by how much of each cycle it stays lit The pin is still only ever fully on or fully off — the average is doing the work.
Duty cycle sets the average
The one number that controls a PWM signal is its duty cycle: the fraction of each cycle the pin spends high, written as a percentage. At 0 percent the pin is never high — it is just off. At 100 percent it is high the whole time — just on. Everything useful lives in between: 25 percent means high for a quarter of each cycle and low for the other three quarters, 75 percent means the reverse.
Duty cycle matters because it sets the average voltage the signal delivers. A pin that sits at its high voltage for a fraction of the time and at 0 V for the rest averages out to that fraction of the high voltage. The relationship is exactly that simple: the average voltage equals the duty cycle times the high-level voltage. like being paid for the fraction of each hour you actually work: work half of every hour and you earn half the hourly rate, no matter how the minutes are split up
Put a number on it. Drive a 5 V pin at 50 percent duty and it delivers an average of 2.5 V; at 20 percent, an average of 1 V; at 80 percent, 4 V. Nothing about the pin's two levels changed — it is still slamming between 5 V and 0 V — but by choosing how long it dwells at each, you choose any average between them. Duty cycle is the knob, and the average is what turns.
Period, frequency, and why they are separate from duty
A PWM signal repeats. One high-then-low stretch is a period, and how long that stretch lasts is fixed by the frequency — the number of periods packed into a second, measured in hertz (Hz). A 1000 Hz PWM signal completes a thousand full on-off cycles every second, so each period lasts a thousandth of a second.
Frequency and duty cycle are two independent settings, and keeping them apart is the whole point. Frequency says how fast the cycles repeat; duty cycle says what fraction of each cycle is high. You can hold the frequency dead steady and slide the duty cycle from 10 percent up to 90 percent — the pulses stay the same distance apart while the high part of each one grows wider. The average follows the duty cycle, not the frequency.
That independence is why the same average can be delivered at wildly different speeds. A 50 percent duty signal averages half the supply whether it cycles a hundred times a second or fifty thousand times a second. The frequency does not change what level you get — but it very much changes whether the thing being driven can smooth that level out, which is the next thing that matters.
A fast switch plus something with inertia
A pulse train is not actually a smooth voltage, so something has to do the smoothing. That something is the load: whatever the pin drives has to be unable to keep up with the individual pulses, and then it responds to their average instead of their edges. Fast switching supplies the pulses; inertia in the load supplies the blur.
The inertia takes different physical forms, but the effect is the same. An LED switched thousands of times a second does flick fully on and off each cycle, but the eye cannot resolve flashes that quick and blends them into one steady brightness set by the on-fraction. A motor has mechanical inertia — a spinning mass cannot lurch to full speed and back every pulse, so it settles at a speed set by the average push. like shoving a heavy wheel in quick on-off pushes: it is too massive to speed up and slow down with each individual shove, so it settles at a speed set by the average push And where you want a genuinely smooth voltage rather than a driven device, a resistor and capacitor wired as a low-pass filter charge and discharge a little each cycle and hold the average as a real, steady DC level.
This is the hinge the whole technique turns on. PWM does not create an in-between voltage at the pin; it creates a rapid two-level signal whose average is in between, and relies on a slow-enough load to feel only that average. Get a load fast enough to follow every pulse and the illusion breaks — it would just see the flicker.
Fast enough to blur, not so fast it costs
Since the load averages the pulses only when it cannot follow them, the frequency has to clear whatever speed the load can react at. Too slow and the inertia is not enough to hide the switching: a dimmed LED visibly flickers, a motor lurches and buzzes instead of turning smoothly. The fix is to switch faster than the load or the observer can track.
The thresholds are set by physiology and hardware. For an LED, the eye stops seeing separate flashes once the switching is fast enough to blend — typically at least a few hundred cycles per second clears perceptible flicker for most people. For a motor, the trap is hearing: human hearing runs from about 20 Hz to about 20 kHz, and a motor switched anywhere inside that band vibrates at the switching rate and whines. Pushing the PWM frequency above roughly 20 kHz moves the switching out of the audible range, which is why DC motors are commonly driven at tens of kilohertz — often 16 kHz to 20 kHz and up — and modern motor driver chips go higher still.
Faster is not free, though. Every switch of the pin costs a little energy in the driving electronics, so cranking the frequency far past what the load needs just wastes power and heats the driver for no benefit. A common hobbyist default lands in the low hundreds of hertz — the Arduino Uno's plain analogWrite, for instance, runs about 490 Hz on most pins and about 980 Hz on two of them — which is fine for dimming an LED and audibly rough for a motor. The craft is picking a frequency high enough that the load feels only the average, and no higher.
Dimming, speed, and a smooth voltage from a switch
The same one-knob trick shows up wherever a digital pin needs to command something continuous. Dimming an LED is the clearest case: the duty cycle is the brightness, 10 percent for a faint glow and 90 percent for nearly full output, with the eye doing the averaging. It is how status lights breathe and how backlights and indicators set their level without any analog parts.
Setting a motor's speed is the workhorse use. Feeding a motor a PWM signal lets its duty cycle stand in for throttle — low duty for a slow crawl, high duty for full speed — with the motor's own inertia smoothing the pulses into steady rotation. The details of how that signal reaches the motor and how the motor is wired belong to the motor-driving concepts downstream; the point here is only that the adjustable command riding into them is a duty cycle.
The third use drops the driven device entirely and keeps the average as a voltage. Run a PWM signal through a resistor-capacitor low-pass filter and the pulse train becomes a genuine steady DC voltage set by the duty cycle — a crude but real digital-to-analog converter built from one pin, one resistor, and one capacitor. When a design needs a tunable reference or bias voltage and has PWM to spare, this is often the cheapest way to make one.
One pin, a whole range of commands
A microcontroller's power is that it does everything in software, but its pins are stubbornly digital — high or low, nothing between. PWM is the bridge across that gap. It lets a program command a continuous quantity, brightness or speed or a bias voltage, using nothing but a pin it already has and a timer counting how long to hold it high. No digital-to-analog hardware, no extra chip: just a duty-cycle number the code writes.
For a build like a small self-driving car, this is the layer that turns decisions into motion. The control code arrives at a number — go this fast, steer this hard, glow this bright — and PWM is how that number becomes an actual level in the physical world. A whole range of commands rides out of a single pin, each one just a fraction of time spent high.
None of this covers how a motor is actually wired to survive that switching or how a servo reads a pulse as a position — those are their own concepts, sitting directly on top of this one. What PWM gives you is the foundation underneath them: the one honest way a two-state pin delivers an in-between level, which is the thing almost every actuator on a robot ultimately listens to.