How to Use the L298N Motor Driver with Arduino

How to Use the L298N Motor Driver with Arduino

The L298N motor driver is a popular module for controlling DC motors and stepper motors. It allows you to control motor speed and direction using PWM signals from Arduino, making it ideal for robotics and automation projects. This tutorial will guide you through connecting and using the L298N with Arduino.


What You Will Need

  1. L298N Motor Driver Module
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. DC Motors or Stepper Motors
  4. External Power Source (e.g., 9V or 12V battery)
  5. Breadboard and Jumper Wires
  6. A computer with the Arduino IDE installed

Step 1: Understanding the L298N Motor Driver

The L298N module is a dual H-bridge motor driver, meaning it can control the speed and direction of two DC motors independently or one stepper motor.

L298N Pinout

Pin Function
IN1, IN2 Motor A Direction Control Inputs
IN3, IN4 Motor B Direction Control Inputs
ENA Motor A Speed Control (PWM Input)
ENB Motor B Speed Control (PWM Input)
OUT1, OUT2 Motor A Outputs
OUT3, OUT4 Motor B Outputs
12V (VCC) External Power Supply for Motors
5V (Optional) Logic Power Supply (if jumper is removed)
GND Ground

Notes:

  • The module includes an onboard 5V regulator that powers the logic circuitry if the jumper is in place and VCC is 7-12V.
  • Removing the jumper requires you to provide a 5V logic supply separately.

Step 2: Wiring the L298N to Arduino

Here’s how to connect the L298N to an Arduino Uno and two DC motors:

L298N Pin Arduino Pin
ENA Pin 10 (PWM)
IN1 Pin 8
IN2 Pin 9
ENB Pin 11 (PWM)
IN3 Pin 6
IN4 Pin 7
GND Arduino GND
12V (VCC) External Power (9V/12V)
OUT1, OUT2 Motor A terminals
OUT3, OUT4 Motor B terminals

Step 3: Upload the Code

Here’s an example sketch to control two DC motors:

Example Code

// Motor A connections
#define ENA 10
#define IN1 8
#define IN2 9

// Motor B connections
#define ENB 11
#define IN3 6
#define IN4 7

void setup() {
  // Set all pins as outputs
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop() {
  // Move Motor A forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 150); // Set speed (0-255)

  // Move Motor B backward
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENB, 150); // Set speed (0-255)

  delay(2000); // Run for 2 seconds

  // Stop both motors
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);

  delay(1000); // Pause for 1 second

  // Move both motors in reverse
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 200);
  analogWrite(ENB, 200);

  delay(2000); // Run for 2 seconds

  // Stop both motors
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  delay(1000); // Pause for 1 second
}

Step 4: Test the Setup

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code to the Arduino by clicking Upload.
  4. Observe the motors spinning forward, stopping, and reversing based on the programmed sequence.

Optional: Controlling Speed Dynamically

You can adjust the motor speed dynamically by changing the PWM values sent to the ENA and ENB pins using analogWrite(). For example:

analogWrite(ENA, 100); // Slow speed
analogWrite(ENA, 255); // Full speed

Applications of the L298N

  1. Building motorized robots
  2. Controlling conveyor belts
  3. Driving stepper motors
  4. Automating systems with DC motors

Troubleshooting

  • Motors not spinning: Ensure the external power supply is connected and provides enough current.
  • Incorrect motor direction: Check the wiring of the IN pins and reverse connections if needed.
  • Unstable motor behavior: Use a stable power supply and avoid loose connections.

Conclusion

You’ve successfully interfaced the L298N motor driver with Arduino to control DC motors. This versatile module enables you to manage motor speed and direction with ease. Experiment further by integrating sensors, remote controls, or other modules into your motorized projects!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.