🔍🔩 Parts bench
19 lessons

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.

lesson 2 of 2 in this unit

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:

V_avg = D · V_supplyduty cycle D from 0 to 1 — 30% on-time delivers 30% of the supply, on average

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.

Half voltage is not half speed under load

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.
40 %
1 kHz
Average voltage
2.40 V of 6 V
LED
steady to the eye
Motor
smooth — rides the average

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.

1. A 12 V supply is PWM-switched at 25% duty cycle. What is the average voltage, in volts?
2. A PWM signal runs at 20 kHz. How long is one full cycle, in microseconds? (T = 1/f)

Check your understanding

1. What does a PWM signal's duty cycle control?

2. Why doesn't a motor jerk forward and stop thousands of times a second under PWM?

3. You lower a motor's PWM frequency from 20 kHz to 30 Hz at the same 50% duty. What changes?

4. Why do motor drivers often switch at about 20 kHz, specifically?