L298N Motor Driver: Basics, Direction & Speed Control
Meet Torque — your friendly guide to the L298N, the most popular motor driver for beginners! In three easy parts, you'll learn how to wire it up, spin a motor forward and backward, and control exactly how fast it spins using PWM.
⚙️ Say hi to Torque, your motor driver guide!
What Is an L298N Motor Driver, Anyway?
An Arduino pin can only supply a tiny trickle of current — nowhere near enough to spin a real DC motor safely. The L298N solves this problem: it's a small board that takes a little signal from the Arduino and uses it to switch a much bigger current from a separate battery to the motor. It can also flip that current's direction, which is how a motor spins forward or backward.
This tutorial covers three essential skills in order: Part 1 wiring the L298N and spinning a motor, Part 2 controlling forward and reverse direction, and Part 3 controlling motor speed with PWM.
What You'll Need
The same simple setup is used for all three parts of this tutorial.
Arduino Uno
Sends direction and speed signals to the L298N.
L298N Motor Driver Module
The star of this tutorial — controls the motor safely.
Small DC Motor
The motor you'll spin forward, backward, and at different speeds.
Potentiometer (10kΩ)
Used in Part 3 to manually dial in motor speed.
4x AA Battery Pack (or 9–12V supply)
Powers the motor — separate from the Arduino's own power.
Breadboard
For connecting everything without soldering.
Jumper Wires
Male-to-male and male-to-female.
The Circuit Diagram
This same wiring is used for all three parts — only the code changes!
Part 1: L298N Basics — Spinning the Motor
Let's start simple: wire everything up and get the motor spinning in one direction.
Wire the Arduino to the L298N
Connect Arduino pin 8 to IN1, pin 9 to IN2, and pin 10 to ENA. Connect Arduino GND to the L298N's GND.
Wire the motor and battery pack
Connect your DC motor to OUT1 and OUT2. Connect the battery pack's positive and negative wires to the L298N's 12V and GND screw terminals.
💡 Tip: Never power the motor from the Arduino's 5V pin directly — motors need their own separate power source!// 🔧 Part 1: L298N Basics — spin a motor forward at full speed const int IN1 = 8; const int IN2 = 9; const int ENA = 10; void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENA, OUTPUT); } void loop() { digitalWrite(IN1, HIGH); // current flows one way through the motor digitalWrite(IN2, LOW); digitalWrite(ENA, HIGH); // ENA high = motor runs at full speed }
Part 2: Forward & Reverse Control
Now let's make the motor spin one way, then the other — the wiring stays exactly the same as Part 1!
Understand the IN1/IN2 pattern
The L298N flips the motor's direction based on which of IN1 and IN2 is HIGH. Swapping which one is HIGH reverses the current, which reverses the spin.
// 🔄 Part 2: Forward and Reverse — alternates direction every few seconds const int IN1 = 8; const int IN2 = 9; const int ENA = 10; void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENA, OUTPUT); digitalWrite(ENA, HIGH); // keep speed at full for this part } void loop() { spinForward(); delay(3000); stopMotor(); delay(500); spinReverse(); delay(3000); stopMotor(); delay(500); } void spinForward() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); } void spinReverse() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); } // Setting both IN pins LOW lets the motor coast to a stop void stopMotor() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); }
Part 3: PWM Speed Control
Instead of just full-speed on or off, let's control exactly how fast the motor spins using a potentiometer knob!
Add the potentiometer
Connect the potentiometer's two outer pins to 5V and GND, and its middle pin (the wiper) to Arduino pin A0.
💡 Tip: ENA must be connected to a PWM-capable pin (marked with a ~ symbol on your Arduino) for speed control to work!// 🎚️ Part 3: PWM Speed Control — turn the knob to change motor speed const int IN1 = 8; const int IN2 = 9; const int ENA = 10; const int potPin = A0; void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENA, OUTPUT); digitalWrite(IN1, HIGH); // direction fixed as forward for this demo digitalWrite(IN2, LOW); } void loop() { int potValue = analogRead(potPin); // reads 0–1023 int motorSpeed = map(potValue, 0, 1023, 0, 255); // scale to PWM range analogWrite(ENA, motorSpeed); // smoothly changes the motor's speed }
How Does the L298N Actually Work?
Here are the big ideas hiding inside this tutorial:
The H-Bridge
Inside the L298N is a circuit called an H-bridge, which can route current through the motor in either direction — that's the real trick that makes forward and reverse possible.
Separating Logic from Power
The Arduino's small signals (IN1, IN2, ENA) only tell the L298N what to do — the actual motor power comes from the separate battery pack, keeping the Arduino safe.
PWM: Fast On/Off Switching
analogWrite() doesn't create a "half voltage" — it rapidly switches the pin on and off. The more time it spends on versus off, the faster the motor spins, on average.
Reading an Analog Knob
The potentiometer gives a changing voltage as you turn it, which analogRead() converts into a number from 0–1023 — map() then rescales that into the 0–255 range PWM understands.
🧑🔬 Safety First!
- Never power a motor directly from the Arduino's 5V pin — always use the L298N with a separate battery pack.
- Double-check your battery pack's voltage matches your L298N and motor's ratings before connecting.
- Keep fingers and loose wires clear of the spinning motor shaft.
- Build with an adult, especially the first time you wire a motor driver.
- Disconnect power before changing any wiring.
Frequently Asked Questions
My motor doesn't spin at all — what's wrong?
Check that your battery pack is actually connected and has power, that GND is shared between the Arduino and the L298N, and that ENA is set HIGH or receiving a PWM value above 0.
Can I control two motors with one L298N?
Yes! The L298N has a second channel — IN3, IN4, ENB, and OUT3/OUT4 — that works exactly the same way as IN1/IN2/ENA/OUT1/OUT2, letting you control a second motor independently.
What's the difference between ENA being HIGH and using analogWrite()?
digitalWrite(ENA, HIGH) runs the motor at full, constant speed. analogWrite(ENA, value) uses PWM to run it at any speed between fully off (0) and full speed (255).
What age group is this tutorial good for?
This is a great beginner-friendly tutorial for kids around age 9 and up, especially as an introduction to motors before building bigger robots.

Comments
Post a Comment