The speed of a DC motor depends on its average voltage. A microcontroller cannot output an adjustable voltage, so it switches the supply on and off very fast (PWM), and an H-bridge lets the motor turn in both directions. This is the electronic side of DC Motor Speed Control.
Pulse Width Modulation
How it works:
- a square wave with a fixed frequency
- the duty cycle is the fraction of each period that the output is on
- the motor's inductance and inertia smooth the pulses, so the motor responds to the average voltage
How a microcontroller makes it:
- a counter counts from 0 up to a wrap value (TOP), then starts again
- the output is high while the count is below a compare level
- with no clock divider, the PWM frequency and duty are
- example: a 125 MHz clock with TOP gives kHz
Choosing the frequency:
- above about 20 kHz the motor makes no audible whine
- too high a frequency increases switching losses in the transistors
H-Bridge
How it works:
- four switches around the motor, in the shape of the letter H
- top-left and bottom-right closed: current flows one way, the motor turns forward
- top-right and bottom-left closed: current flows the other way, the motor turns backward
- both bottom switches closed: the motor terminals are shorted, which brakes the motor
What must never happen:
- both switches on the same side closed at once, which shorts the supply (shoot-through)
- drivers add a short dead time between turning one switch off and the other on
Common driver interface:
- one direction pin and one PWM pin per motor (sign and magnitude)
- the software sets the direction pin from the sign of the command and the PWM duty from its size
Bipolar Versus MOSFET Drivers
- older bipolar drivers such as the L298N lose about 2 V across their transistors, which turns into heat, and the motor gets less voltage
- MOSFET drivers have a very small on-resistance, so the voltage drop and the heat are much lower
Practical Points
- deadband: below a minimum duty the motor cannot overcome static friction and does not move, so controllers often raise any small non-zero command to that minimum
- the minimum can be found with a test that steps the duty down until the motor stops
- flyback: when a switch opens, the motor's inductance keeps the current flowing, so diodes (or the MOSFET body diodes) must give it a path
- two "identical" motors are rarely identical, so open-loop commands drift, and closed-loop speed control with encoders fixes it