Home · Academy · Robotics & Coding · Robotic Systems · Motors and Drivers

Motors and Drivers

Learn to drive two wheels independently with a motor driver to move a robot forward, back and turning.

LESSON COMPASS

What will you use this page for?

Core idea

We learn to control a robot's two wheels independently using DC motors and a motor driver, creating forward, reverse and turning movements.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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.…

Next connection

Obstacle Detection: The robot uses a distance sensor to notice an obstacle ahead and stops or changes direction.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteChoosing a Power Source
ContentStandard lesson · 1,769 words
Last updated

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:

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:

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:

Forward, reverse and turning logic table
MovementLeft wheelRight wheel
Forwardforwardforward
Reversebackwardbackward
Turn rightforwardstop or backward
Turn leftstop or backwardforward
Spin right in placeforwardbackward

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?

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:

Lesson summary

Check questions

  1. Why can't we connect a DC motor straight to an Arduino pin?
  2. What is the key ability that a motor driver chip (the H-bridge) provides?
  3. What does "common ground" mean, and why is it needed?
  4. How should the left and right wheels be driven for a gentle turn to the right?
  5. What safety check should we do before putting the robot on the floor, and why?

Answers

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Start QuizBack to Robotic Systems
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.