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:
- A toy car drives backwards when you flip the battery around; the motor's direction has reversed.
- A fan's speed dial makes the motor spin faster or slower.
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:
- It uses Arduino's signal to turn the motor on/off and choose its direction.
- 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:
- Direction pins (IN1, IN2): These go to two Arduino digital pins. Their combined state decides whether the motor turns forwards or backwards.
- Speed pin (ENA): This goes to a PWM pin on the Arduino (one of the pins marked
~on the board). This pin sets the motor's speed. - Power and common GND: The motor's power comes from a separate battery pack. A very important rule: the battery pack's GND and the Arduino's GND must be connected together. This is called common GND (a shared ground); both circuits need to share the same "zero point," otherwise their signals cannot agree.
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.
| IN1 | IN2 | Motor |
|---|---|---|
| HIGH | LOW | Turns forwards |
| LOW | HIGH | Turns backwards |
| LOW | LOW | Stops (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
- Toy car: The DC motor inside spins the wheels. Flipping the direction pins is like flipping the battery around: the car drives backwards.
- Washing machine drum: It turns one way to wash and the other way to spin, and its speed changes. Same idea: a driver manages both the motor's direction and its speed.
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:
- What speed did you start the motor at? Why is a low speed safer?
- Did you stop the motor before changing direction?
- Is the power coming from a separate battery pack, or accidentally from the Arduino?
- Are the battery pack's GND and the Arduino's GND connected?
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:
- Keep your fingers, hair and clothing away from the spinning shaft, gears or fan blade. Tie back long hair.
- Always connect the motor through a motor driver; never power it directly from an Arduino pin.
- Take the motor's power from a separate, low-voltage source (a battery pack or USB). Never use mains (wall socket) electricity.
- Connect the battery pack's GND to the Arduino's GND (common GND).
- Start the motor at low speed, then increase it slowly.
- If wires get hot or you notice a strange smell or sound, cut the power immediately.
- Do all wiring and testing under adult supervision.
Lesson summary
- A DC motor draws far more current than an LED, so we cannot drive it directly from an Arduino pin.
- A motor driver (L298N/L293D) takes Arduino's small signal and powers the motor from a separate source, protecting the board.
- Direction is chosen by the states of the IN1 and IN2 pins; the motor turns when they are opposite.
- Speed is set on a PWM pin with
analogWrite(pin, 0–255); always start at a low speed. - A separate power source, a common GND and adult supervision are the basics of safe motor control.
Check questions
- Why can't we drive a DC motor directly from an Arduino pin?
- What two jobs does a motor driver do at the same time?
- What does the line
analogWrite(ENA, 90)do, and what range is 90 in? - With IN1 HIGH and IN2 LOW, what does the motor do? What happens if we swap them?
- What does "common GND" mean, and why is it needed?
Answers
- 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.
- 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.
- 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).
- 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).
- 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.