PWM: How a Computer Whispers to a Motor
A chip's pin knows only on and off — yet motors need anything in between. The trick is to blink faster than the motor can feel and let the average do the talking.
Builds on: 0.1 Sense, Think, Act: What Makes a Robot
Same idea, other course:Spark: PWM from the electronics side
Digital pins, analog world
A microcontroller pin is a light switch: fully on (say 5 V) or fully off (0 V), nothing between. But the world the loop acts on is smooth — a motor wants 40% effort, a lamp wants half brightness. Buying a true in-between voltage costs precious hardware. Robotics instead uses a beautiful cheat: pulse-width modulation, PWM.
Switch the pin on and off very fast, and control what fraction of each cycle is spent on. That fraction is the duty cycle:
The load never notices the blinking if the blinking is fast enough. A motor’s spinning mass smooths thousands of pulses per second into steady torque, the way a ceiling fan keeps turning between pushes. An LED does flicker — but above a few hundred hertz your eye averages just like the motor does.
Frequency and duty are different knobs
Two numbers describe a PWM signal, and beginners blur them at their peril. The duty cycle is the message — how much effort. The frequency is how often the message repeats — chosen once, high enough that the load can’t feel individual pulses, then left alone. Change duty constantly; change frequency almost never.
Too low a frequency and the cheat collapses: the motor growls and jerks, the LED strobes. Too high and the switching electronics waste power warming up. Motor drivers typically sit near 20 kHz — just above human hearing, so the motor doesn’t sing its duty cycle out loud.
Why robotics runs on it
PWM is how the think-step’s decision physically reaches the act-step: the control loop computes an effort, sets a duty cycle, and the motor feels an average voltage. One digital pin, zero extra parts, effort in a thousand shades. Every motor command in the rest of this course — line follower, robot arm, all of it — is at the bottom a duty cycle being nudged up or down.
PWM delivers a clean average voltage — but what the motor does with it depends on the fight against friction and load, which is Unit 1’s story. The signal is honest; the mechanics negotiate.
⚗️ Lab — The Duty Dial
One PWM channel, three victims: an oscilloscope trace, an LED and a motor. Turn the two knobs and watch who cares about which.
- Sweep the duty cycle and watch the average voltage track it exactly.
- Drop the frequency below ~50 Hz: the LED starts strobing and the motor speed gets lumpy — same duty, broken illusion.
- Find the lowest frequency where the motor runs smooth. Notice the LED needs more than the motor.
Work it by hand — 0 / 2
No multiple choice here: compute the value and type it. Suffixes like 2.5k or 20m are understood; answers within ±2% count.