กลับหน้าหลัก
views
How to Use Arduino L298N Motor Driver Module to Control DC Motor Direction
Last updated on

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.

Circuit diagram showing Arduino UNO connected to L298N Motor Driver Module and DC Motor on a breadboard

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 UNOL298N Module
5V5V (Logic Power)
GNDGND
Pin 2IN4
Pin 3IN3
Pin 4IN2
Pin 5IN1

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

L298N Motor Driver Module diagram labeling IN1-IN4 pins, OUT1-OUT4 terminals, and power input connectors

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

  1. Open Arduino IDE and paste the code above
  2. Select Tools → Port and choose the port connected to your board
  3. Select Tools → Board → Arduino UNO
  4. Click the Upload button (right arrow)
  5. Wait for Done uploading, then open Serial Monitor (Tools → Serial Monitor)
  6. Configure Serial Monitor settings:
    • Baud Rate: 9600
    • No line ending
Serial Monitor window showing + and - commands with CW/CCW/Stop results displayed

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

IN1IN2IN3IN4Result
HIGHLOWHIGHLOWMotor rotates in one direction
LOWHIGHLOWHIGHMotor rotates in opposite direction
LOWLOWLOWLOWMotor 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

https://www.youtube.com/embed/QT-ZeGXQfSk

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ประเมินราคาอัตโนมัติ + Reference Code

ขอให้ AI ประเมินราคาโปรเจคนี้

กรอกข้อมูลให้ครบ ระบบจะสร้างรหัสอ้างอิงและประเมินราคา/ระยะเวลาคร่าว ๆ จากรายละเอียดงาน แล้วให้กด Add LINE พร้อมพิมพ์รหัสนี้เพื่อคุยต่อ

คำถามให้ AI ประเมินแม่นขึ้น

หลังส่งฟอร์ม ระบบจะโชว์ Reference Code ให้ copy แล้วกด Add LINE เพื่อคุยต่อ ข้อมูลส่วนตัวจะไม่ถูกส่งเข้า GA4

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...