How to Use Arduino L298N Motor Driver Module to Control DC Motor Direction
How to Use Arduino L298N Motor Driver Module to Control DC Motor Direction
This tutorial covers using the L298N Motor Driver Module with Arduino UNO to drive DC motors and control rotation direction via the Serial Monitor in Arduino IDE. Suitable for beginners studying robotics or automation systems that require motor control.
Required Components
- Arduino UNO R3 + USB cable
- L298N Motor Driver Module
- DC Motor 3-6V (vertical shaft)
- Power Adapter 9V 2A jack 5.5x2.5mm (for L298N power)
- Breadboard MB-102 830 points
- Jumper Wires M-M, M-F, F-F at least 40 pieces
- Motor Propellers (to observe rotation direction)
Wiring Steps
Part 1: Arduino to L298N Connection
| Arduino UNO | L298N Module |
|---|---|
| 5V | 5V (Logic Power) |
| GND | GND |
| Pin 2 | IN4 |
| Pin 3 | IN3 |
| Pin 4 | IN2 |
| Pin 5 | IN1 |
Part 2: L298N to DC Motor Connection
For the first motor (left side):
- OUT1 → Motor wire (red)
- OUT2 → Motor wire (black)
For the second motor (right side):
- OUT3 → Motor wire (black)
- OUT4 → Motor wire (red)
Part 3: Power Supply for L298N
Connect 9V 2A power adapter to L298N’s DC jack terminal at +12V and GND
Arduino Code for Motor Direction Control
// Define pins connected to L298N
const int IN1 = 5; // Direction control pin 1
const int IN2 = 4; // Direction control pin 2
const int IN3 = 3; // Direction control pin 3
const int IN4 = 2; // Direction control pin 4
void setup() {
// Set all pins as OUTPUT
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Start Serial Communication
Serial.begin(9600);
// Display usage instructions
Serial.println("=================================");
Serial.println("L298N Motor Driver Control");
Serial.println("=================================");
Serial.println("Press + to rotate CW (Clockwise)");
Serial.println("Press - to rotate CCW (Counter-Clockwise)");
Serial.println("Press any other key to STOP");
Serial.println("=================================");
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == '+') {
// Rotate Clockwise
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("CW - Motor Rotating Clockwise");
}
else if (command == '-') {
// Rotate Counter-Clockwise
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("CCW - Motor Rotating Counter-Clockwise");
}
else {
// Stop motors
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
Serial.println("Stop motors");
}
}
}
How to Upload and Test
- Open Arduino IDE and paste the code above
- Select Tools → Port and choose the port connected to your board
- Select Tools → Board → Arduino UNO
- Click the Upload button (right arrow)
- Wait for Done uploading, then open Serial Monitor (Tools → Serial Monitor)
- Configure Serial Monitor settings:
- Baud Rate: 9600
- No line ending
Test the Operation
- Type + and click Send → Motor rotates clockwise (CW), Serial Monitor shows “CW”
- Type - and click Send → Motor rotates counter-clockwise (CCW), Serial Monitor shows “CCW”
- Type any other character and click Send → Motor stops, Serial Monitor shows “Stop motors”
How L298N Works
L298N uses the H-Bridge principle to reverse current flow through the motor
| IN1 | IN2 | IN3 | IN4 | Result |
|---|---|---|---|---|
| HIGH | LOW | HIGH | LOW | Motor rotates in one direction |
| LOW | HIGH | LOW | HIGH | Motor rotates in opposite direction |
| LOW | LOW | LOW | LOW | Motor stops |
Swapping motor wires at OUT1/OUT2 or OUT3/OUT4 will also reverse rotation direction.
Important Precautions
- Separate power supplies for Logic (5V from Arduino) and Motor (9V from Adapter) - do not mix them
- Do not exceed 35V on L298N’s motor power input
- If motor doesn’t spin, check:
- Jumper wires are not loose
- Adapter power is sufficient
- Pin connections match the code (IN1-IN4)
Reference Video
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย