Home · Academy · Robotics & Coding · Arduino · The Motor Driver

The Motor Driver

Learn to control a DC motor's direction and speed safely with a motor driver and separate power.

LESSON COMPASS

What will you use this page for?

Core idea

A motor driver is the go-between that takes Arduino's small signal and powers a DC motor from a separate supply, so we can safely control the motor's direction and speed (with PWM) without damaging our board.

Evidence to produce

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

Control trap

Wiring the motor straight to a pin This is the most common and most dangerous mistake. Connecting the motor directly to an Arduino pin can destroy the pin and the board. Always put a motor driver in between. Forgetting the common GND If you do not connect the GNDs of the Arduino and the battery pack, the driver…

Next connection

Libraries: We will meet ready-made packages of code that make controlling components like motors, sensors and displays easier, and learn how to add them to our own code.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteThe Ultrasonic Distance Sensor
ContentStandard lesson · 1,766 words
Last updated

One-sentence summary

A motor driver is the go-between that takes Arduino's small signal and powers a DC motor from a separate supply, so we can safely control the motor's direction and speed (with PWM) without damaging our board.

Why it matters

In the previous lesson we used an ultrasonic sensor to let a robot "see" an obstacle in front of it. But once the robot sees something and wants to stop or turn, what actually spins the wheels? That is where the DC motor comes in.

There is a small problem, though. To light an LED, we wired it straight to a pin. You might want to wire a motor the same way. Unfortunately, that is one of the easiest ways to break your board. A DC motor draws much more current than an LED, and an Arduino pin cannot supply that much.

The solution is to put a motor driver in between. This lesson introduces the component at the heart of every moving robot. In fact, you already see this idea in everyday life:

Behind both of these behaviours (changing direction and changing speed) is a circuit that drives a motor safely.

Why can't we drive a motor straight from a pin?

The current problem

Remember from the electronics lesson: current is the amount of electricity flowing through a circuit. An LED glows on a tiny current, which is why we could connect it directly to a pin. A DC motor needs far more current to turn its shaft, and it draws the most at the very moment it starts (at startup).

An Arduino pin is designed to supply only a few milliamps. A motor wants much more than that. One of two things happens: either the motor barely turns, or the pin is overloaded and the board is damaged.

Simple rule: An Arduino pin is there to give an "order," not to spin a motor. The power has to come from somewhere else.

Think of a tap

Think of a motor driver like a tap. Your finger nudges a small lever with very little effort (Arduino's signal). But the water that flows comes from the pressure in the pipe (a separate power source). With a small movement, you control a much stronger flow.

What does a motor driver do?

A motor driver is a circuit that takes Arduino's small signal and powers the motor from a separate power source. The most common examples in schools are the L298N and L293D boards.

A motor driver does two jobs at once:

  1. It uses Arduino's signal to turn the motor on/off and choose its direction.
  2. It pulls the current the motor needs from a separate power source (a battery pack), which protects the Arduino.

We connect three things

When driving a single motor with an L298N, there are three groups of connections:

How is direction chosen?

Direction depends on the states of IN1 and IN2. When they are opposite, the motor turns; when they are the same, it stops.

How is direction chosen? table
IN1IN2Motor
HIGHLOWTurns forwards
LOWHIGHTurns backwards
LOWLOWStops (coasts)

How is speed set? (PWM)

To set speed we use PWM (Pulse Width Modulation). PWM means switching the power on and off very quickly. The longer it stays "on," the faster the motor turns. On Arduino we do this with analogWrite(pin, value), where value is between 0 and 255. 0 means stop, 255 means full speed.

Two everyday examples

Direction and speed control: the full sketch

The sketch below drives a single motor forwards, then backwards, with a stop in between, through an L298N. The pin numbers are examples; change them to match your wiring.

const int IN1 = 8;   // direction pin 1
const int IN2 = 9;   // direction pin 2
const int ENA = 5;   // speed pin (PWM ~)

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
}

After setup, the main loop:

void loop() {
  digitalWrite(IN1, HIGH);   // forwards
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 90);      // low speed (0-255)
  delay(2000);

  analogWrite(ENA, 0);       // stop
  delay(1000);

  digitalWrite(IN1, LOW);    // backwards
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 90);
  delay(2000);

  analogWrite(ENA, 0);       // stop
  delay(1000);
}

Notice: the speed starts at 90, not 255. We always test a motor at low speed first, then increase it.

A sketch that ramps the speed up

With a for loop and analogWrite, we can raise the speed gradually. This stops the robot from lurching forward suddenly.

void loop() {
  digitalWrite(IN1, HIGH);         // forwards
  digitalWrite(IN2, LOW);

  for (int speed = 60; speed <= 200; speed += 20) {
    analogWrite(ENA, speed);       // raise speed from 60 to 200
    delay(500);
  }

  analogWrite(ENA, 0);             // stop
  delay(1500);
}

Mini practice

Design a desk-fan behaviour: the motor spins slow, then medium, then fast, then stops, then turns the other way at low speed. Write the pseudocode first, then try it with a driver and battery pack together with an adult.

Start
Direction: forwards
Speed 80,  wait 2 seconds
Speed 150, wait 2 seconds
Speed 220, wait 2 seconds
Stop the motor (speed 0), wait 1 second
Direction: backwards
Speed 100, wait 2 seconds
Stop the motor
End

Check yourself:

Common mistakes

Wiring the motor straight to a pin

This is the most common and most dangerous mistake. Connecting the motor directly to an Arduino pin can destroy the pin and the board. Always put a motor driver in between.

Forgetting the common GND

If you do not connect the GNDs of the Arduino and the battery pack, the driver "cannot understand" Arduino's signal and the motor behaves unexpectedly. A common GND is essential.

Not separating the power source

If you power both the motor and the Arduino from the same weak source, the board may reset or the motor may not turn. Give the motor its own battery pack.

Reversing direction suddenly

Giving a reverse command while the motor is running forwards at full speed strains the driver and the motor. First stop it with analogWrite(ENA, 0), wait briefly, then set the opposite direction.

Putting speed on a non-PWM pin

analogWrite only works on pins marked ~ (PWM pins). If you wire ENA to a non-PWM pin, the speed control will not behave as expected.

Safety note

DC motors are spinning, moving parts. To work safely:

Lesson summary

Check questions

  1. Why can't we drive a DC motor directly from an Arduino pin?
  2. What two jobs does a motor driver do at the same time?
  3. What does the line analogWrite(ENA, 90) do, and what range is 90 in?
  4. With IN1 HIGH and IN2 LOW, what does the motor do? What happens if we swap them?
  5. What does "common GND" mean, and why is it needed?

Answers

  1. A DC motor draws a lot of current, especially at startup; an Arduino pin cannot supply that current, so the pin or board can be damaged.
  2. It uses Arduino's signal to switch the motor on/off and choose its direction, and it powers the motor from a separate source to supply the current it needs.
  3. It sets a speed of 90 on the ENA pin using PWM, so the motor turns at a low-to-medium speed. The value ranges from 0 (stop) to 255 (full speed).
  4. With IN1 HIGH and IN2 LOW the motor turns one way (forwards). Swapping them (IN1 LOW, IN2 HIGH) makes it turn the other way (backwards).
  5. A common GND means connecting the GND terminals of the motor power source and the Arduino together. Both circuits must share the same "zero point," otherwise the driver cannot read Arduino's signal correctly.

Source and verification note

For “The Motor Driver”, verification focuses on whether the relationship between Why can't we drive a motor straight from a pin? and Think of a tap remains consistent across examples. Pin, voltage and current limits can differ between Arduino-compatible boards. Compiling code does not guarantee a safe circuit; loads such as motors and servos require a suitable driver and external power where appropriate.

Next lesson

Libraries: We will meet ready-made packages of code that make controlling components like motors, sensors and displays easier, and learn how to add them to our own code.

Start QuizBack to Arduino
QUESTION POOL

Reinforce this lesson with 10 questions

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