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.
59%
602 µs
of ~1024
≈ 980
Say your answer out loud, then open this
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.
What the bridge does with those pulses: the H-bridge lab →
The ENA and ENB jumper caps, on the real module →
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.