
L298N Motor Driver Explained: Wiring and Code
The L298N motor driver is one of the most popular motor control modules used in Arduino robotics and DIY electronics projects. It allows an Arduino to control the speed and direction of DC motors without connecting the motors directly to the microcontroller.
If you are building a robot car, obstacle-avoiding robot, Bluetooth-controlled car, or line-following robot, the L298N is a useful module to understand.
In this beginner-friendly guide, you will learn how the L298N motor driver works, understand its pins, connect it to an Arduino UNO, and upload code to control two DC motors in forward, reverse, left, and right directions.
1. What Is an L298N Motor Driver?
The L298N is a dual H-bridge motor driver module based on the L298 dual full-bridge motor driver IC. It allows a low-power controller such as an Arduino UNO to control the direction and speed of two brushed DC motors.
An Arduino output pin cannotsafely supply the current required by most DC motors. The L298N acts as an interface between the Arduino and the motors, using separate motor power connections.
Main functions:
-
Controls two brushed DC motors independently.
-
Changes motor direction using input signals.
-
Controls motor speed using PWM signals.
-
Supports forward, reverse, and turning movements.
-
Can also control one bipolar stepper motor in suitable configurations.
Common applications:
-
Arduino robot cars
-
Obstacle-avoiding robots
-
Bluetooth-controlled vehicles
-
Line-following robots
-
Small automation projects
-
Educational robotics experiments
The L298N uses bipolar transistor output stages, so it has a noticeable voltage drop and generates heat. It is not as efficient as many modern MOSFET-based motor drivers.
GitHub+1
2. L298N Motor Driver Pinout Explained
Before connecting the module, it is important to understand its input, output, and power pins.
Control Pins
| L298N Pin | Function |
|---|---|
| ENA | Enables Motor A; PWM speed control |
| IN1 | Motor A direction input 1 |
| IN2 | Motor A direction input 2 |
| IN3 | Motor B direction input 1 |
| IN4 | Motor B direction input 2 |
| ENB | Enables Motor B; PWM speed control |
Motor Output Pins
| L298N Pin | Connection |
|---|---|
| OUT1 | Motor A terminal 1 |
| OUT2 | Motor A terminal 2 |
| OUT3 | Motor B terminal 1 |
| OUT4 | Motor B terminal 2 |
Power Pins
| Pin | Function |
|---|---|
| VIN / 12V | Motor supply input (within module and IC ratings) |
| GND | Common ground |
| 5V | Logic supply or regulated output, depending on jumper configuration |
Important: L298N modules vary in their onboard regulator and jumper arrangements. Do not assume the 5V terminal is always an output. Check the module’s markings and regulator configuration before connecting power.
3. Components Required
For this tutorial, we will build a simple two-wheel robot car using the following components:
| Component | Quantity | Purpose |
|---|---|---|
| Arduino UNO | 1 | Main controller |
| L298N Motor Driver | 1 | Controls DC motors |
| DC Gear Motors | 2 | Drives the robot |
| Robot Car Chassis | 1 | Holds the components |
| Wheels | 2 | Robot movement |
| Battery Pack | 1 | Motor power |
| Jumper Wires | As required | Electrical connections |
| USB Cable | 1 | Uploads Arduino code |
You can also use a breadboard for organizing low-current signal connections, but motor power wiring should use suitable wire and secure terminals.
4. How to Connect L298N with Arduino UNO
We will connect Motor A to the left side and Motor B to the right side of the robot.
Arduino-to-L298N pin connections
| L298N Pin | Arduino UNO Pin |
|---|---|
| ENA | D5 |
| IN1 | D8 |
| IN2 | D7 |
| IN3 | D4 |
| IN4 | D3 |
| ENB | D6 |
| GND | GND |
Pins D5 and D6 support PWM on the Arduino UNO, allowing speed control through analogWrite().
Motor connections
| L298N Output | Motor Connection |
|---|---|
| OUT1 and OUT2 | Left DC motor |
| OUT3 and OUT4 | Right DC motor |
If a motor rotates in the opposite direction from what you expect, switch its two output wires or adjust the direction logic in the code.
Motor power connections
Connect the motor power supply as follows:
| L298N Power Terminal | Connection |
|---|---|
| 12V / VS | Battery positive, within module ratings |
| GND | Battery negative |
| 5V | Logic supply only, according to jumper configuration |
Important power-supply instructions:
-
Use a battery pack whose voltage is suitable for your motors and L298N module.
-
Connect battery negative, L298N GND, and Arduino GND together.
-
Do not power the motors from the Arduino 5V pin.
-
Check the module’s
5V-ENregulator jumper before using its 5V terminal. -
On modules with a compatible onboard 5V regulator, the 5V-EN jumper may enable the regulator when the motor supply is within its specified range.
-
If the jumper is removed, the module may require a separate regulated 5V logic supply at its 5V terminal. Follow the exact module documentation.
-
Never connect an external 5V source to a terminal that is already being driven by the onboard regulator.
For initial testing, keep the wheels off the ground so the robot cannot unexpectedly drive away.
5. Understanding ENA and ENB Jumpers
The L298N module usually includes two small jumpers:
-
ENA: Enables Motor A.
-
ENB: Enables Motor B.
These jumpers are useful when you want the motors to run at full enabled speed without PWM control.
For Arduino speed control:
-
Remove the ENA jumper.
-
Remove the ENB jumper.
-
Connect ENA to Arduino PWM pin D5.
-
Connect ENB to Arduino PWM pin D6.
-
Use
analogWrite()to control motor speed.
If the jumpers remain installed, the enable pins may be held HIGH, preventing the Arduino from controlling their speed through those pins.
6. How to Control Motor Direction Using L298N
The L298N uses two direction inputs for each motor.
| IN1 | IN2 | Motor A Action |
|---|---|---|
| HIGH | LOW | One direction |
| LOW | HIGH | Opposite direction |
| LOW | LOW | Coast / free-running stop |
| HIGH | HIGH | Brake |
The same principle applies to Motor B using IN3 and IN4.
The actual forward direction depends on how the motor wires are connected and how the motor is mounted on the robot.
Dual full-bridge driver+1
7. Complete Arduino Code for L298N Motor Driver
Now that the wiring is complete, upload this code to your Arduino UNO.
This program will:
-
Move both motors forward.
-
Stop the motors.
-
Move both motors backward.
-
Turn left.
-
Turn right.
-
Repeat the movement sequence.
Complete Arduino Code:
// L298N Motor Driver with Arduino UNO
// Controls two DC motors
// Motor A - Left Motor
const int ENA = 5;
const int IN1 = 8;
const int IN2 = 7;
// Motor B - Right Motor
const int ENB = 6;
const int IN3 = 4;
const int IN4 = 3;
int motorSpeed = 180; // PWM range: 0 to 255
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
stopMotors();
}
void loop() {
// Forward
forward();
delay(2000);
// Stop
stopMotors();
delay(1000);
// Backward
backward();
delay(2000);
// Stop
stopMotors();
delay(1000);
// Turn Left
turnLeft();
delay(1000);
// Stop
stopMotors();
delay(1000);
// Turn Right
turnRight();
delay(1000);
// Stop
stopMotors();
delay(2000);
}
void forward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, motorSpeed);
analogWrite(ENB, motorSpeed);
}
void backward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, motorSpeed);
analogWrite(ENB, motorSpeed);
}
void turnLeft() {
// Left motor backward, right motor forward
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, motorSpeed);
analogWrite(ENB, motorSpeed);
}
void turnRight() {
// Left motor forward, right motor backward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, motorSpeed);
analogWrite(ENB, motorSpeed);
}
void stopMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
Understanding the Code
-
pinMode()configures the Arduino pins as outputs. -
digitalWrite()controls motor direction. -
analogWrite()controls the PWM speed command. -
forward()runs both motors in the forward direction. -
backward()reverses both motors. -
turnLeft()andturnRight()rotate the robot by driving its motors in opposite directions. -
stopMotors()disables the motor outputs using zero PWM.
Speed adjustment: Change motorSpeed to control the PWM command.
| PWM Value | Approximate Command |
|---|---|
| 80 | Low |
| 120 | Medium-low |
| 180 | Medium-high |
| 220 | High |
| 255 | Maximum |
The actual motor speed depends on battery voltage, motor load, driver voltage drop, and motor characteristics. A low PWM setting may not provide enough torque to start a motor.
8. How to Control Two DC Motors at Different Speeds
One useful feature of the L298N is independent PWM control for Motor A and Motor B.
For example:
// Different speeds for two DC motors
analogWrite(ENA, 150); // Left motor
analogWrite(ENB, 220); // Right motor
This is useful for:
-
Adjusting robot steering.
-
Correcting differences between motors.
-
Building line-following robots.
-
Making smoother turns.
-
Experimenting with differential-drive movement.
You can calibrate the two PWM values so the robot travels more accurately in a straight line.
9. Using L298N with Four DC Motors
Typical arrangement:
-
Left-front motor and left-rear motor connected to OUT1 and OUT2 in parallel.
-
Right-front motor and right-rear motor connected to OUT3 and OUT4 in parallel.
However, there is an important limitation.
The two motors connected to one channel share its available current. Their combined starting and stall current must stay within the driver’s electrical and thermal limits. Check the stall current of each motor, not just its no-load current.
For four motors with high current demand, use a suitable higher-capacity motor driver or two separate motor drivers.
10. Common L298N Motor Driver Problems and Solutions
| Problem | Possible Cause | Solution |
|---|---|---|
| Motors do not run | Incorrect wiring or no motor power | Check battery and motor terminals |
| Only one motor runs | Incorrect channel wiring | Check OUT1–OUT4 connections |
| Motor runs in reverse | Motor wires reversed | Swap its output wires |
| Arduino resets when motors start | Supply voltage drops | Use a suitable motor supply |
| Motors run slowly | Driver voltage drop or weak battery | Check supply and motor requirements |
| Driver becomes very hot | Excessive current or load | Reduce load and check motor stall current |
| PWM speed control does not work | ENA/ENB jumpers still installed | Remove enable jumpers |
| Motors make noise but do not rotate | Insufficient starting torque or mechanical obstruction | Check supply, load, and connections |
11. L298N Motor Driver Specifications and Limitations
The original L298 IC is specified for up to 46V motor supply and 2A DC per bridge under specified operating conditions. However, these figures should not be treated as guaranteed continuous ratings for every inexpensive L298N module. Cooling, PCB construction, motor load, and temperature all affect practical performance.
Dual full-bridge driver+2
| Feature | Specification |
|---|---|
| Driver Type | Dual H-Bridge |
| Number of DC Motors | 2 |
| Direction Control | Forward and Reverse |
| Speed Control | PWM |
| IC Motor Supply Limit | Up to 46V |
| IC Current Rating | Up to 2A per bridge under specified conditions |
| Logic Supply | Typically 5V |
| Protection | Thermal protection in the IC |
Important limitation: The L298N uses bipolar transistors and loses some voltage inside the driver. This means your motor receives less voltage than the battery supplies, and the driver can become hot during operation.
For projects requiring better efficiency, consider modern drivers such as the TB6612FNG or DRV8833, provided their voltage and current ratings match your motors.
12. Important Safety Tips
-
Never connect the motor supply directly to Arduino GPIO pins.
-
Use a battery voltage suitable for your DC motors and driver module.
-
Always connect Arduino GND and L298N GND together.
-
Check the 5V regulator jumper before powering the module.
-
Never connect a battery directly to the L298N 5V logic terminal.
-
Check motor stall current before connecting multiple motors to one channel.
-
Avoid short circuits at the motor output terminals.
-
Do not change motor wiring while the circuit is powered.
-
Use suitable wires and secure battery connections.
-
Stop testing if the driver becomes excessively hot.
13. Where to Buy L298N Motor Drivers and Arduino Components in Pakistan?
If you are building a robot car, line-following robot, or Bluetooth-controlled vehicle, choosing compatible motor drivers and electronic components is essential.
At Electroway.org, students, hobbyists, engineers, and robotics enthusiasts can explore components for:
Explore the Arduino boards and accessories at Electroway.org.
You can also use the Electroway app as part of your electronics shopping experience. Always check motor voltage, current requirements, and driver compatibility before purchasing.
14. Frequently Asked Questions (FAQs)
Q1. What is an L298N motor driver used for?
The L298N is used to control the speed and direction of two brushed DC motors or one compatible bipolar stepper motor.
Q2. Can I connect an L298N directly to Arduino UNO?
Yes. Connect its direction and enable pins to suitable Arduino output pins, provide an appropriate motor supply, and connect the grounds together.
Q3. Can an L298N control two motors independently?
Yes. Motor A and Motor B have separate direction inputs and enable pins, allowing independent direction and PWM speed control.
Q4. Can I use four DC motors with one L298N?
Yes, in some applications, by connecting two motors in parallel to each channel. However, their combined current must remain within the driver’s practical electrical and thermal limits.
Q5. Why does my L298N motor driver become hot?
The L298N has an internal voltage drop and dissipates power as heat. High motor current, mechanical load, and insufficient cooling can increase its temperature.
Q6. What is the function of the ENA and ENB pins?
ENA enables Motor A and ENB enables Motor B. They can receive PWM signals to control motor speed.
Q7. Can I power Arduino from the L298N 5V terminal?
Some L298N modules provide regulated 5V through an onboard regulator. Whether it can power an Arduino depends on the module design, regulator jumper, input voltage, and available current. Check the module documentation before using it.
Q8. Can I use a 3.7V Li-ion battery with an L298N?
It depends on the particular module and motor requirements. A single 3.7V nominal cell may not provide enough voltage for reliable operation because the L298N itself loses voltage. Check the motor’s minimum operating voltage and the module’s regulator requirements.
Conclusion
The L298N motor driver is a useful component for learning Arduino motor control and building beginner robotics projects. It allows you to control two DC motors, change their direction, and adjust their speed using PWM.
With the correct wiring and Arduino code, you can build a robot car that moves forward, backward, left, and right. You can also expand the project by adding ultrasonic sensors, Bluetooth modules, IR sensors, and other electronics.
Start with the two-motor example in this guide, test each direction carefully, and then use the L298N in your own robotics projects.
Explore Arduino, motor drivers, sensors, and robotics components at Electroway.org and bring your DIY electronics ideas to life.
