Small DC motors are in every robot or car project. The idea is simple: connect a voltage, and it spins. Reverse the polarity, and it spins the other way. But to control one with a microcontroller, you need to understand a few things.
Speed and current
- Speed rises roughly with voltage.
- Current depends on the load: a motor running with no load draws a small current, while the same motor stalled or at the moment of starting draws much more, possibly 5–10 times as much.
Design the driver and the power supply for the stall current, not the normal current.
How do you find the stall current?
The easiest way: measure the motor's resistance with a multimeter while it's disconnected (turn the shaft a little and take the lowest reading). The stall current is roughly the voltage ÷ the resistance. A motor of 2 Ω at 6 V → about 3 A at the moment of starting, even if it draws 200 mA while running. That's exactly why the Arduino resets or a small driver gets hot.
Speed control: PWM
Instead of changing the voltage, you switch the motor on and off rapidly with PWM. The motor responds to the average. And if it only moves in one direction, a MOSFET and a protection diode are enough; see the logic-level MOSFET for the circuit.
Watch out: below a certain ratio (for example 30–40%) the motor doesn't turn at all; it just hums. The average voltage isn't enough to overcome friction. So start the speed from that threshold, not from zero.
Reversing direction: the H-bridge
To reverse the direction you must reverse the polarity of the voltage on the motor. An H-bridge is 4 switches around the motor in the shape of the letter H:
- S1 + S4: current flows one way, and the motor turns forward.
- S2 + S3: current flows the other way, and the motor turns backward.
- Never S1 + S3 or S2 + S4 together, because that's a direct short across the supply. Ready-made chips prevent this by themselves.
The four states
| IN1 | IN2 | Motor |
|---|---|---|
| HIGH | LOW | Forward |
| LOW | HIGH | Reverse |
| LOW | LOW | Coast (stops gently) |
| HIGH | HIGH | Brake (stops immediately) |
Braking connects the motor's two terminals together, so the spinning motor acts as a generator and brakes itself. Useful for a robot that must stop in exactly the right place. (The last two states may be swapped depending on the chip, so check the datasheet.)
Wiring the L298N
| Module | Connection |
|---|---|
| 12V | The motor supply (up to about 12 V if the 5V jumper is fitted) |
| GND | The battery's ground and the Arduino's ground |
| ENA | A PWM pin (remove the jumper) |
| IN1 and IN2 | Two digital pins |
| OUT1 and OUT2 | The motor |
const int ENA = 9, IN1 = 7, IN2 = 8;
void motor(int speed) { // from -255 to 255
digitalWrite(IN1, speed > 0);
digitalWrite(IN2, speed < 0);
analogWrite(ENA, abs(speed));
}
void setup() {
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
}
void loop() {
motor(200); delay(2000);
motor(0); delay(500);
motor(-200); delay(2000);
}
The 5V jumper on the L298N: when fitted, the module's regulator produces 5 V from the motor supply (you can power the Arduino from it if the supply is 12 V or less). If the supply is above 12 V, remove it and feed the module 5 V externally, or the regulator will burn out.
Ready-made drivers
| Chip | Notes |
|---|---|
| L298N | Popular and cheap, but loses about 2 V and runs hot, because it uses old transistors |
| TB6612FNG | MOSFETs, so much lower losses, and suited to small motors (about 1.2 A per channel) |
| DRV8833 | Small and efficient for batteries and toy motors |
| BTS7960 | For large motors and high currents |
If you're using an L298N on a 6 V battery, the motor gets only about 4, which is why the robot is "slow".
And the TB6612 has a pin called STBY: it must be HIGH, or the chip is asleep and no motor will move. That's the most common reason "the module doesn't work".
The yellow gear motors
The yellow motors popular in robot cars (TT motors) are a small motor with a gearbox: low speed and higher torque. They run on 3–6 V, and their starting current can reach an amp. Two of them with an L298N on 4 AA cells → you'll find the car feels weak because of the 2 V lost. A TB6612 or DRV8833 suits them better.
Protection from noise
Motors produce a lot of electrical noise, which can reset the Arduino:
- A 100 nF ceramic capacitor soldered across the motor's own terminals.
- A large capacitor (100–470 µF) on the driver's supply. See decoupling capacitors.
- A separate supply for the motors, with a common ground with the microcontroller.
- Protection diodes, which most drivers have built in.
Common faults
| Fault | Usual cause |
|---|---|
| The motor hums but doesn't turn | PWM too low, or the supply is weak |
| The Arduino resets | The starting current drags the supply down, plus noise |
| One motor turns opposite to the other | Swap the motor's two wires or invert it in code |
| No movement at all | No common ground, or the ENA or STBY jumper |
| The driver is very hot | Current beyond its capacity, or an L298N with high current |
When should you use a servo or stepper instead of DC?
If you need a specific angle or a precise number of turns, an ordinary DC motor alone isn't suitable. See servos and steppers. Or add an encoder to count the turns; see the rotary encoder.
FAQ
Can I run the motor directly from an Arduino pin? Never. The pin safely supplies 20 mA, the motor draws hundreds of milliamps, and the noise will damage the chip.
Can I run a 6 V motor on 12? It'll spin faster and get hot, and its life will be shorter. If you must, keep the PWM to a maximum of about 50%, but a supply at the right voltage is better.