One-sentence summary
We learn to control a robot's two wheels independently using DC motors and a motor driver, creating forward, reverse and turning movements.
Why it matters
So far we have practised the first two steps of a robot's "sense, decide, act" loop. Sensors read the surroundings and the microcontroller makes a decision. But if the robot never moves, our work is no different from a program running on a desktop.
Movement is what makes a robot a robot. A line-following car and an obstacle-avoiding rover both rely on the same basic skill: driving two wheels separately. If the left wheel turns faster than the right, the robot curves to the right. If the two wheels turn in opposite directions, the robot spins on the spot. All steering grows from this simple idea.
In this lesson we will see why we cannot connect a motor straight to the Arduino, what a motor driver does, and how the forward/reverse/turn logic looks in code.
Why not connect a motor straight to the Arduino?
In the previous lesson we discussed choosing a power source. Now we connect that knowledge to motors.
A DC motor needs current
A DC motor is a simple part that spins when current flows through it. The more load it carries (the robot's weight, friction with the floor) the more current it draws. Even a small robot motor can ask for hundreds of milliamps at start-up, and sometimes more than a full amp.
An Arduino pin, meanwhile, can safely supply only about 20 milliamps. If we connect a motor straight to a pin, the pin can burn out and the delicate parts on the board can be damaged.
Short rule: An Arduino pin cannot spin a motor. The pin only gives the "order"; a separate circuit supplies the real power.
The motor driver is the bridge
A motor driver is a circuit that takes the small signal from the Arduino and switches the large current going to the motor on and off. Think of it as a controlled switch: the Arduino says "on," and the driver lets the power from a thick wire reach the motor.
A commonly used driver chip is the L298N. Inside it there is an arrangement called an H-bridge. The H-bridge can change the direction of the current going to the motor, which means it can spin the motor both forward and backward. That direction change is what lets the robot reverse and turn.
A separate power source and common ground
Motors create short spikes while they run. If we power the Arduino from the same battery, these spikes can reset or damage the board. So we use two separate supplies:
- The Arduino runs from its own USB or a small battery.
- The motors run from a separate battery pack (for example 4–6 AA cells) connected to the motor driver.
There is one requirement: the ground (GND) lines of the two circuits must be connected together. This is called a common ground. Without it, the driver cannot correctly "read" the signal the Arduino sends. To speak the same language, both sides need to share the same zero point.
Driving two wheels independently
Our robot has one motor on the left and one on the right. Each motor spins one wheel. On the driver chip, each motor usually has these signals:
- Two direction pins (they choose whether the motor turns forward or backward).
- One speed pin (it sets how fast the motor turns, using PWM).
PWM (Pulse Width Modulation) is a way of producing in-between speeds like "half power" or "quarter power" by switching a pin on and off very quickly. It is the same idea we used for LED brightness in the micro:bit and Arduino lessons; here we adjust speed instead of brightness.
Forward, reverse and turning logic
All steering can be explained by comparing the speed and direction of the two wheels:
| Movement | Left wheel | Right wheel |
|---|---|---|
| Forward | forward | forward |
| Reverse | backward | backward |
| Turn right | forward | stop or backward |
| Turn left | stop or backward | forward |
| Spin right in place | forward | backward |
In pseudocode:
FORWARD:
left motor = forward, speed 200
right motor = forward, speed 200
TURN RIGHT:
left motor = forward, speed 200
right motor = forward, speed 90
In the second example the right wheel is slower, so the robot curves toward the slower side, that is, to the right. It is just like a rowing boat: pull one oar more slowly and you turn that way.
Example 1: Gentle turn or sharp turn?
- Gentle turn: Both wheels turn forward, but one is slower. The robot draws a wide arc. This is useful for line-following cars.
- Sharp (in-place) turn: One wheel turns forward, the other backward. The robot spins almost on the spot. This is ideal for changing direction in a tight space.
With the same two motors, just by changing the speed and direction values, we get very different movements.
Example 2: Why won't the robot go straight?
You gave both motors the same speed value (say 200), but the robot drifts slightly to the left. This is completely normal: no two motors are exactly alike, and one may be a little stronger. The fix is to add a small correction in software, for example giving the right motor 200 and the left motor 210. This is a common fine-tuning step on real robots.
Mini practice
The Arduino code below drives two motors connected to an L298N driver: first forward, then turning right. The code is under 25 lines, and the logic matches the pseudocode exactly.
// Left motor pins
int leftDir = 7; // direction
int leftSpeed = 5; // PWM speed
// Right motor pins
int rightDir = 8;
int rightSpeed = 6;
void setup() {
pinMode(leftDir, OUTPUT);
pinMode(rightDir, OUTPUT);
}
void forward() {
digitalWrite(leftDir, HIGH);
digitalWrite(rightDir, HIGH);
analogWrite(leftSpeed, 200); // equal speed
analogWrite(rightSpeed, 200);
}
void turnRight() {
digitalWrite(leftDir, HIGH);
digitalWrite(rightDir, HIGH);
analogWrite(leftSpeed, 200); // left fast
analogWrite(rightSpeed, 90); // right slow
}
void loop() {
forward();
delay(1000);
turnRight();
delay(600);
}
Before you put the robot on the floor, lift its wheels into the air (for example, rest it on a box) and watch that the wheels spin the right way. If a wheel turns the wrong way, swap that motor's two wires, or set its direction pin to LOW in the code.
Common mistakes
Connecting a motor straight to an Arduino pin
This can burn out the pin. There must always be a motor driver in between.
Forgetting the common ground
The Arduino and motor battery are powered separately, but their GND lines must be connected. If they are not, the motors will not spin at all, or they behave randomly.
Powering the motors from one battery
If you power the motors from the same battery as the Arduino, the board may keep resetting. Give the motors a separate battery pack.
Starting at full speed right away
Starting at 255 (full speed) can send the robot flying off the table. Use a low speed on the first try (for example 120), then increase it once you see the behaviour.
Placing it on the floor before checking wheel direction
If a wheel is wired backward, the robot shoots off in an unexpected direction. Test in the air first.
Safety note
A moving robot can pinch a finger, fall off a table or run into something. So:
- Clear the test area. Try the robot on a flat, open surface with visible edges. Keep it away from the table edge; do your first tests on the floor.
- Keep fingers, hair and cables away. Spinning wheels and gears can catch hair or a loose cable.
- Start at low speed. Do not increase speed until you understand the behaviour.
- Use a motor driver and a separate, low-voltage battery. Never connect motors to mains (wall) electricity; use a battery pack only. Connect the Arduino's ground to the motor battery's ground.
- Work with an adult present. Ask an adult for help with motors, wiring and, if needed, soldering or tools.
- Know your emergency stop. Set up a way to cut the motor battery quickly if something goes wrong (for example, a switch on the battery box).
Lesson summary
- A DC motor draws a lot of current; an Arduino pin cannot supply it, so we use a motor driver.
- The H-bridge inside a motor driver (such as the L298N) changes the current's direction, letting the motor spin forward and backward.
- Motors run from a separate battery pack; the Arduino's ground and the motor battery's ground must be connected.
- By comparing the speed and direction of the two wheels, we get forward, reverse, gentle turns and in-place spins.
- We set speed with PWM, and we always test the robot in the air first, then at low speed.
Check questions
- Why can't we connect a DC motor straight to an Arduino pin?
- What is the key ability that a motor driver chip (the H-bridge) provides?
- What does "common ground" mean, and why is it needed?
- How should the left and right wheels be driven for a gentle turn to the right?
- What safety check should we do before putting the robot on the floor, and why?
Answers
- Because a DC motor draws far more current than an Arduino pin can safely supply (about 20 mA); connecting it directly can burn out the pin and the board.
- The H-bridge can change the direction of the current going to the motor, so the motor can spin both forward and backward. It also switches the large current on and off using the Arduino's small signal.
- Even though the Arduino and motor battery are powered separately, their GND (ground) lines are connected together. A shared zero point is needed so the driver reads the signal correctly.
- Both wheels turn forward, but the right wheel is driven more slowly than the left; the robot curves toward the slower side, to the right.
- We should lift the wheels into the air and check that they spin the right way. A backward-wired motor can throw the robot off in an unexpected direction, and testing in the air lets us see this safely.
Source and verification note
For “Motors and Drivers”, verification focuses on whether the relationship between Why not connect a motor straight to the Arduino? and The motor driver is the bridge remains consistent across examples. Robot behaviour cannot be explained by code alone; mechanical structure, power system, sensor placement and surface conditions must be evaluated together. Test results should be recorded over several runs on the same course.
Next lesson
Obstacle Detection: The robot uses a distance sensor to notice an obstacle ahead and stops or changes direction.