get-started-witharduino-lesson-14-l298n
|

How to Use the L298N Motor Driver Shield: Beginner’s Guide

What is the L298N Motor Driver Shield?

The L298N motor driver shield is a versatile module used to control the speed and direction of DC motors and stepper motors in Arduino projects. It’s based on the L298N dual H-Bridge motor driver IC, which can drive up to two DC motors simultaneously.

While the L293D motor driver is another popular motor driver IC, the L298N stands out for its ability to handle higher currents (up to 2A per motor) and higher voltage (up to 46V), which makes it more suitable for larger motors. Additionally, the L298N has built-in diodes for back EMF protection, a feature that the L293D lacks.

L298N-motor-driver-shield
L298N motor driver shield

How the L298N Motor Driver Works

The L298N motor driver is built around an H-Bridge circuit. An H-bridge is a configuration that allows current to flow in either direction across a motor, enabling the motor to spin both forward and backward. In the L298N, there are two H-Bridge circuits, which means it can control two DC motors independently.

Each motor can be controlled by two inputs (IN1 and IN2 for motor 1, IN3 and IN4 for motor 2). By setting these inputs to HIGH or LOW, you can control the motor’s direction. Additionally, the ENA and ENB pins control the speed of the motors using PWM signals.

Controlling Two DC Motors

Wiring Diagram

To connect the L298N motor driver shield to your Arduino and two DC motors, follow these steps:

  1. Connect motor 1 to the OUT1 and OUT2 terminals on the L298N.
  2. Connect motor 2 to the OUT3 and OUT4 terminals on the L298N.
  3. Connect the IN1, IN2, IN3, and IN4 pins of the L298N to any four digital pins on your Arduino.
  4. Connect the ENA and ENB pins to PWM-enabled pins on the Arduino.
  5. Provide a separate power source for the motors if they require more power than the Arduino can provide.

Important: Always power the motors with a separate power supply when using high-current motors to avoid damaging the Arduino.

Arduino Code

// Pin definitions for Motor 1
int motor1Pin1 = 9; // IN1 on the L298N
int motor1Pin2 = 8; // IN2 on the L298N
int enable1Pin = 10; // ENA on the L298N (PWM control)

// Pin definitions for Motor 2
int motor2Pin1 = 7; // IN3 on the L298N
int motor2Pin2 = 6; // IN4 on the L298N
int enable2Pin = 5; // ENB on the L298N (PWM control)

void setup() {
  // Set all motor control pins as output
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);

  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enable2Pin, OUTPUT);
}

void loop() {
  // Move motor 1 forward at full speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  analogWrite(enable1Pin, 255); // Full speed

  // Move motor 2 backward at half speed
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
  analogWrite(enable2Pin, 128); // Half speed

  delay(2000); // Run motors for 2 seconds

  // Stop both motors
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);

  delay(2000); // Pause for 2 seconds before repeating
}

Explanation of Code:

  • The motor1Pin1 and motor1Pin2 control the direction of motor 1, while enable1Pin controls its speed using PWM.
  • The motor2Pin1 and motor2Pin2 control the direction of motor 2, and enable2Pin controls its speed using PWM.
  • The digitalWrite function sets the direction, and the analogWrite() function sets the speed of the motors. Setting the PWM value to 255 moves the motor at full speed, while a value of 128 moves it at half speed.

Conclusion

The L298N motor driver shield is a powerful and flexible solution for controlling two DC motors in your Arduino projects. It provides bi-directional control and speed regulation using the H-Bridge design and PWM signals. This motor driver is ideal for beginners due to its ease of use and support for higher-current motors.

For future projects, you could expand the application by controlling stepper motors or experimenting with autonomous robots.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *