Lesson 14: Motor Control: H-Bridges, PWM and SpeedNew-age
A motor is what turns a microcontroller’s decisions into movement, but a microcontroller pin cannot drive a motor directly: it supplies about 40 mA, and even a small motor wants ten times that. Between the pin and the motor sits a motor driver — usually an H-bridge — which lets a small control signal switch a large motor current, reverse the motor, and control its speed. This lesson explains the H-bridge, shows how PWM controls speed, and builds a motor that can go forwards, backwards, fast and slow.
Speed control matters as much as direction. A robot that can only go full speed or stop will overshoot every turn and crash into every wall. A robot that can go slowly, speed up and slow down can follow a line, approach an obstacle gently, and stop exactly where it should.
- H-bridge — a circuit of four switches that lets a motor run forwards or backwards.
- Motor driver — a chip or board, such as the L298N, that contains H-bridges for driving motors.
- PWM — pulse-width modulation: switching a signal on and off rapidly to control average power.
- Duty cycle — the fraction of time a PWM signal is on, such as 50% for half speed.
- Stall current — the current a motor draws when it is stopped but powered — its highest current.
- Back EMF — the voltage a spinning motor generates, which opposes the supply and limits speed.
- Flyback diode — a diode that protects a circuit from the voltage spike when a motor is switched off.
- Torque — the turning force a motor produces, which falls as speed rises.
Why a motor needs a driver
A microcontroller pin can supply about 40 mA at 5 V — 0.2 watts. A small DC motor draws 200–500 mA running and over 1 A when it starts or stalls. Connecting a motor directly to a pin would burn the pin out, and probably the board. A motor driver solves this: it takes a small control signal from the microcontroller and switches a separate, larger supply to the motor. The microcontroller decides; the driver delivers the power.
The driver also protects the board from the motor’s electrical noise. When a motor is switched off, its magnetic field collapses and produces a voltage spike that can damage electronics; a good driver includes flyback diodes that absorb the spike. Never drive a motor from a bare transistor without a flyback diode, and never skip the driver to “keep it simple” — the simple version is the one that breaks boards.
The H-bridge: forwards, backwards, stop
An H-bridge is four switches arranged like the letter H, with the motor as the crossbar. Close the top-left and bottom-right switches and current flows one way through the motor: it turns forwards. Close the top-right and bottom-left and current flows the other way: it turns backwards. Close both switches on one side and the motor brakes; open all four and it coasts to a stop. The L298N driver board contains two H-bridges, so it can drive two motors independently — one per side of a robot.
Two combinations must never be used: top-left with bottom-left, or top-right with bottom-right, because they short the supply directly to ground. A driver chip prevents this internally, but if you ever build an H-bridge from separate transistors, the rule is the first thing to check. Wire the driver’s logic supply from the microcontroller’s 5 V, its motor supply from a separate battery, and join the grounds.
PWM and speed
Speed is controlled by PWM — pulse-width modulation. The driver switches the motor on and off hundreds of times a second; the fraction of time it is on is the duty cycle, and the motor’s average voltage follows it. A 50% duty cycle gives about half speed, 25% gives about a quarter. The motor’s inertia smooths the pulses into steady motion, just as your eye smooths a flickering light.
Two motors, one robot
A two-wheeled robot steers by driving its wheels at different speeds: both forward for straight, left slower than right to turn right, one forward and one backward to spin on the spot. This is differential drive, and it is controlled by two PWM values — one per motor. The skill is calibration: two “identical” motors rarely run at the same speed for the same PWM value, so a robot that should go straight will curve. Measure the curve, then correct it in code by adding a small offset to one motor.
Worked example — driving a motor with the L298N
Wire the L298N’s ENA pin to Arduino pin 9 (a PWM pin), IN1 to pin 8 and IN2 to pin 7, and the motor to OUT1 and OUT2. The code below runs the motor forwards at about half speed for two seconds, then stops it.
void setup() {
pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(8, HIGH); digitalWrite(7, LOW); // forwards
analogWrite(9, 128); // ~half speed
delay(2000);
analogWrite(9, 0); // stop
delay(1000);
}
Common mistakes
- Driving a motor directly from a microcontroller pin. The pin cannot supply the current and will be damaged.
- Forgetting to join the grounds of the logic supply and the motor supply.
- Using delay() for motor timing, so the robot cannot react while the motor runs.
- Assuming two identical motors run at the same speed. Calibrate, and correct in code.
Discussion questions
- A motor hums but does not turn at 30% duty cycle. Is the motor broken, or is something else happening? What would you test?
- Why does a robot need to go slowly as well as fast? Give two situations where full speed would fail.
- An H-bridge can brake a motor by shorting it. Why would you brake instead of letting the motor coast?
Practice questions
- Why can’t a microcontroller pin drive a motor directly? Answer: A pin supplies about 40 mA; even a small motor draws 200–500 mA running and over 1 A at start-up. The pin would burn out. Explanation: Pins are signals, not power.
- What does an H-bridge do that a single transistor cannot? Answer: It reverses the current through the motor, so the motor can run backwards as well as forwards. Explanation: Four switches, two directions.
- What is duty cycle, and how does it control speed? Answer: The fraction of time a PWM signal is on. A higher duty cycle gives a higher average voltage and a faster motor. Explanation: On-time fraction = speed.
- Your robot curves left when it should go straight. What is the likely cause, and how do you fix it? Answer: The two motors run at different speeds for the same PWM value. Add a small offset to one motor’s speed in code until it runs straight. Explanation: Calibrate the motors.
- Why should the motor supply and the logic supply be separate? Answer: Motors draw large, spiky currents that would reset the microcontroller if they shared a supply. Separate supplies, joined only at ground, keep the logic stable. Explanation: Separate power, shared ground.
✅ Check your understanding
Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.