Lab

PWM: speed is a trick

A digital pin has two states, 5 V and 0 V. There is no “a bit”. So how does the car’s speed pin make the motors go slowly? It switches on and off about 980 times a second and changes how long it stays on each time. That is PWM, and the share of the time it is on is the duty cycle.

Tap the car’s speed digits and watch only the width of the pulses change. Then drag below 150, where the 2021 car’s ladder stops, and see why it starts there.

Then move the speed wire to pin 13, or leave the jumper cap on ENA: two different faults with the same symptom, every speed the same.

SEND A SPEED DIGIT (THE CAR'S LADDER)
or drag: analogWrite(5, 150)
The phone’s ‘0’ and ‘1’ both set 150. Yes, 0 and 1 give the same speed: to stop, the car uses S.
THE SPEED WIRE GOES TO
A PWM pin (marked ~). It can switch on and off about 980 times a second.
ENA: ON 59% OF EVERY CYCLE
5 V0 Vaverageone cycle ≈ 1 ms
duty cycle
59%
on each cycle
602 µs
of ~1024
cycles a second
≈ 980
The pin is only ever fully on (5 V) or fully off. Only how long it stays on changes.
WHAT THE MOTORS FEEL: THE AVERAGE
about 3.2 V
59% of about 5.4 V (the battery’s 7.4 V less about 2 V in the motor driver, Chapter 16). The motor can’t follow a thousand pushes a second, so it feels the average. The TT motors are rated 3–6 V.
3 V
6 V
hatched: your car’s floor is somewhere around here (not measured)
Around the bottom of the rating. Whether the car moves here depends on the surface and the cells: a rug, a climb or tired cells push the floor up. Find yours with the Chapter 17 floor test.
NOW YOU TRY
Speed 1 crawls and speed 9 runs. What is the pin doing differently? (Hint: it is not “giving more volts”.)
Say your answer out loud, then open this
It stays on for longer in each cycle: 150 out of 255 (59%) for speed 1, 250 out of 255 (98%) for speed 9. When it is on, it is 5 V both times. The motor averages the pulses, so a bigger share of on-time feels like a bigger push.

What this shows

A digital pin is only ever fully on or fully off. PWM, pulse-width modulation, makes a motor go slowly by switching the pin on and off very fast and changing how long it stays on in each cycle. The share of time it is on is the duty cycle.

On an Arduino Uno's pins 5 and 6, analogWrite runs at about 980 cycles a second (16 MHz divided by 64 and by 256 gives 976.6 Hz). analogWrite(5, 255) is always on, 128 is about half on, and 0 is off.

A motor cannot follow a thousand pushes a second, because the car's momentum and the motor coil's resistance to sudden changes in current smooth them out. So it feels the average: at 59% duty, about 0.59 times the voltage it gets when fully on.

The Troniction car's speed ladder starts at 150 (59%), not 0, because below some duty cycle a motor can't overcome the gears and the car's weight: it hums and stays still. Where that floor sits depends on the car, the surface and the cells, and is found by testing.

On a pin without PWM, analogWrite turns the pin fully on for any value from 128 up and fully off below it. A speed wire on such a pin, or a jumper cap left on the L298N's ENA, makes every speed the same.

Common questions

How does PWM control motor speed?
The pin switches fully on and fully off many times a second, and the share of each cycle it stays on sets the average push. The motor is too slow to follow each pulse, so it runs at a speed set by that average.
What frequency is Arduino PWM on pins 5 and 6?
About 980 Hz on an Uno: 16 MHz divided by 64 and by 256 is 976.6 Hz. That is far faster than a motor can follow, which is why it feels only the average.
Why does my motor hum but not turn at low PWM?
Below some duty cycle the average push can't overcome the gearbox, the load and friction. The motor draws current, hums and warms up without moving. Raise the speed or stop it; don't leave it stalled.