PWM and H-Bridges
A microcontroller pin cannot power a motor, and it can only be fully on or fully off. An H-bridge uses four switches to connect the motor to the battery either way around, and PWM flicks the power on and off so fast that the motor only feels the average.
Average voltage
PWM, pulse width modulation, switches the supply \(V_s\) on for a fraction \(D\) of each period. The motor's coils smooth the current, so it responds to the average voltage:
The four states
Forward turns on the top-left and bottom-right switches, so current runs left to right through the motor. Reverse uses the other pair. Brake turns on both bottom switches, shorting the motor so its own back voltage stops it quickly. Coast turns everything off and the motor spins down slowly.
What to notice
The PWM voltage jumps between zero and full, but the current and speed stay smooth, because the motor coil and the rotor's inertia act as filters. Lower the PWM frequency and the current ripple grows, which you can hear as a whine. Most robot drivers run above 16 kHz to stay above human hearing.
Never turn on both switches on the same side. That shorts the battery through the bridge, called shoot-through, and it can destroy the driver in an instant. Driver chips add a short dead time between switching to prevent it.