TL;DR:
A microcontroller pin can supply only a small current, so a motor driver sits between them and switches the motor's own supply.
An H-bridge uses four switches to send current through the motor in either direction, and PWM on those switches sets the speed.
Never close both switches on the same side at once, and give inductive loads a path (flyback diodes) when the current is cut.
Wire a small motor to a microcontroller pin and set the pin high. Nothing happens, or the board resets, or, if you are unlucky, a pin stops working for good. The microcontroller is a good brain and a bad muscle. A motor driver is the part that translates between the two.
Why a pin cannot drive a motor
A typical microcontroller output pin can source or sink only a small current, often a few tens of milliamps at most. The exact limit is on your chip's datasheet. A small hobby motor, like the 6 V example in how DC motors turn electricity into motion, can draw 1 A running and 2.4 A when stalled. That is around a hundred times more than a pin can give.
So the pin should only send a signal, a small current that says what to do, while a driver circuit switches the large current from the battery. The driver also keeps the motor's electrical noise away from the delicate logic.
Four switches
Imagine a motor in the middle of the letter H, with a switch at each of the four ends. Call them top-left (S1), bottom-left (S2), top-right (S3) and bottom-right (S4). The top ones connect to the battery's positive side, the bottom ones to ground.
Forward: close S1 and S4. Current flows from the battery through S1, across the motor left to right, and down through S4 to ground.
Reverse: close S3 and S2. Now the current crosses the motor right to left, and the motor spins the other way.
Coast: open all four. The motor is disconnected and spins down on its own.
Brake: close S2 and S4 (both bottom). The motor terminals are shorted to ground, so its own back-EMF drives a current that resists the motion and slows the shaft quickly.
The one forbidden combination is S1 with S2 (or S3 with S4) closed together. That connects the battery straight to ground through two switches, a short circuit called shoot-through. It can destroy the switches and the supply. Driver chips prevent it with built-in logic and a short dead time while switching.
Worked example: reversing a DC motor
A robot has a 12 V supply and a motor behind an H-bridge driver chip that has two inputs: a direction pin and a PWM pin.
Full speed forward. Direction = high, PWM = always on. S1 and S4 stay closed and the motor sees the full 12 V.
Three-quarter speed forward. Hold direction high and send PWM with a duty cycle of 0.75. The bridge switches on and off thousands of times per second, so the motor sees an average voltage of
0.75 × 12 = 9 V. The motor's inertia and inductance smooth the pulses.Reverse at half speed. Set direction low, so S3 and S2 are the pair that closes, and use duty 0.5. The average voltage is
0.5 × 12 = 6 Vacross the motor, in the opposite direction.
For any driver, work out the current too. If the motor draws 1.5 A at that load, the driver must be rated above that continuously, plus margin for the stall current. A chip rated for 1 A would overheat or shut itself off.
A last check on direction: reversing a motor is just flipping the sign of the voltage across it. That is exactly what swapping which diagonal pair of switches closes does.
Flyback
A motor winding is an inductor. When the switch that was feeding it opens, the current cannot stop instantly. The inductor pushes the current to continue, and since it has nowhere to go, the voltage across the open switch spikes far above the supply. This is the flyback (or inductive kick) voltage, and it can kill transistors.
The fix is a flyback diode, wired so it gives the current a path to circulate when the switch opens. In a proper H-bridge each of the four switches has such a diode across it, often built in as the body diode of the transistor. The motor current then loops back safely into the supply or through the bridge. If you build a driver from your own transistors, add the diodes.
Where it goes wrong
Shared ground missing. The microcontroller's ground and the driver's ground must be connected, or the logic signals mean nothing.
Heat. Switches have resistance, so they dissipate power. A driver running near its limit needs a heat sink and airflow.
Voltage dips. A motor starting up pulls a big current and can drag the battery voltage down. A large capacitor across the motor supply helps, and so does a separate regulator for the logic.
Wrong rating. Check both the voltage and the current. A driver for 5 V motors will not survive 12 V.
Wrong default on boot. During start-up, pins can float. Add a pull-down resistor on the driver's enable pin so the motor is off until your code says otherwise.
Safety: Motors can start the moment power is applied, so put a master switch or e-stop on the battery line that cuts power to the drivers, and test with the wheels off the ground. Disconnect the battery before you touch wiring. Check polarity before connecting a battery, because reversed power can destroy the driver and the battery can overheat; fuse the battery lead.
How real robots use drivers
Every wheeled robot with brushed motors uses H-bridges, either as driver chips, small breakout boards or built into a motor controller board. Drivers often add a current sense pin, so the controller can notice a stall and shut the motor off. For the power side, see batteries for robots, since the driver's supply and the battery have to match.
Try it yourself
Before using a driver, draw the four switches and mark which pair is closed for forward, reverse, coast and brake. Then wire a driver board to a small motor and a safe battery, write a program that runs forward for two seconds, stops for one, reverses for two, and runs at duty 0.5 and 0.25. Listen: you should hear the pitch change and feel the speed halve.
Frequently asked questions
What is the difference between brake and coast?
In coast all four switches are open and the motor spins down slowly. In brake the motor terminals are shorted, which makes the motor resist rotation and stop faster.
Why do I need PWM and not just a lower voltage?
A driver cannot easily make a smooth variable voltage efficiently. Switching fully on and off wastes little heat in the switches, and the motor averages the pulses.
