L298N Motor Driver (Description, Pinouts, Specification, Interfacing)

L298N Based DC  Motor Driver Module with arduino - Full Tutorial
L298N Based Motor Driver Module – Full Tutorial

If you are thinking of making your new robotic car, then you have to learn about controlling DC Motors. One of the easiest methods is to use a Motor Driver Module. L298N Motor Driver is a very cheap and user-Friendly Motor Driver.

It can control both speed and direction of the DC Motors. It can also control stepper motors like Nema 17.

So let’s start with this tutorial.

L298N Motor Driver

L298N Motor Driver
L298N Motor Driver

This L298N Motor Driver Module is a high power motor driver module for driving DC and Stepper Motors. This module consists of an L298 motor driver IC and a 78M05 5V regulator. L298N Module can control up to 4 DC motors Unidirectional, or 2 DC motors with directional and speed control, or one Stepper Motor.

L298N Motor Driver - Specification

  • Driver Chip: Double H Bridge L298N
  • Motor Supply Voltage (Maximum): 46V
  • Motor Supply Current (Maximum): 2A
  • Logic Voltage: 5V
  • Driver Voltage: 5-35V
  • Driver Current:2A
  • Logical Current:0-36mA
  • Maximum Power (W): 25W
  • Current Sense for each motor
  • Heatsink for better performance
  • Power-On LED indicator

L298N Motor Driver - Pinout

L298N Motor Driver Pinout

VCC/12V Power pin supplies power for the motor. It can be anywhere between 5 to 35V. Remember, if the 5V-EN jumper is in place, you need to supply 2 extra volts than the motor’s actual voltage requirement, in order to get maximum speed out of your motor.

GND is a common ground pin.

5V pin supplies power for the switching logic circuitry inside the L298N IC. If the 5V-EN jumper is in place, this pin acts as output and can be used to power up your Arduino. If the 5V-EN jumper is removed, you need to connect it to the 5V pin on Arduino.

ENA pins are used to control the speed of Motor A. Pulling this pin HIGH(Keeping the jumper in place) will make the Motor A spin, pulling it LOW will make the motor stop. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor A.

IN1 & IN2 pins are used to controls the direction of Motor A. When one of them is HIGH and the other is LOW, Motor A will spin. If both the inputs are either HIGH or LOW the Motor A will stop.

IN3 & IN4 pins are used to control the spinning direction of Motor B. When one of them is HIGH and the other is LOW, Motor B will spin. If both the inputs are either HIGH or LOW the Motor B will stop.

ENB pins are used to control the speed of Motor B. Pulling this pin HIGH(Keeping the jumper in place) will make the Motor B spin, pulling it LOW will make the motor stop. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor B.

OUT1 & OUT2 pins are connected to Motor A.

OUT3 & OUT4 pins are connected to Motor B.

L298N Motor Driver - Schematic

L298N Motor Driver - Schematic

Controlling DC Motor using L298N Motor Driver

Controlling DC Motor using L298N Motor Driver

Now that we have learned all the basics about L298N Motor Driver Module Let's control a DC Motor using L298N Motor Driver Module using Arduino.

Steps for Connecting all the Connection as per the circuit diagram given above.

Connect the Input and Enable Pins to Arduino as follow.

ENA >>> D9 (PWM)

ENB >>> D3 (PWM)

IN1 >>> D7

IN2 >>> D6

IN3 >>> D5

IN4 >>> D4

Connect first motor in OUTA and second motor in OUTB.

Connect the 12V Supply to the L298N Motor Driver and Connect the ground of the 12V Supply and Arduino together.

Arduino Code - Controlling DC Motor

Now open your Arduino IDE to do the programming of Arduino.

The code is pretty simple it does'nt require any additional libraries.

Code for Direction Control of DC Motors

/*
  Code: DC Motor Speed and Dorection Control using L298N Motor using Arduino.
  Circuit Diagram and Working: https://alphaelectronz.com/l298n-motor-driver
  Credit: Alpha Electronz
*/


// Motor A connections
int enA = 9;
int in1 = 7;
int in2 = 6;
int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {
  //set all the input and enable pins as output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  // Turn off all motors initially
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void loop() {
  directionControl();
  delay(1000);

}

// This function lets you control spinning direction of motors
void directionControl() {
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);
  analogWrite(enB, 255);

  // Turn on motor A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);
  
  // Now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);
  
  // Turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Code Explanation

In the beginning we declare the pins of Arduino Uno that are connected to L298N Motor Driver.


int enA = 9;
int in1 = 7;
int in2 = 6;
int enB = 3;
int in3 = 5;
int in4 = 4;

In setup, all the pins are declared as digital OUTPUT and set as LOW to turn the motor OFF initially

void setup() {
  //set all the input and enable pins as output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  // Turn off all motors initially
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

directionControl() – This function spins both motors forward at maximum speed for two seconds. It then reverses the motor’s spinning direction and spins for another two seconds. Finally, it turns the motors off.

void directionControl() {
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);
  analogWrite(enB, 255);

  // Turn on motor A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);
  
  // Now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);
  
  // Turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Code for Speed Control of DC Motors

/*
  Code: DC Motor Speed and Dorection Control using L298N Motor using Arduino.
  Circuit Diagram and Working: https://alphaelectronz.com/l298n-motor-driver
  Credit: Alpha Electronz
*/

int enA = 9;
int in1 = 7;
int in2 = 6;
int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {
  //set all the input and enable pins as output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  // Turn off all motors initially
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void loop() {
  speedControl();
  delay(1000);
}

// This function lets you control speed of the motors
void speedControl() {
  // Turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  
  // Accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  }
  
  // Decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  }
  
  // Now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Code Explanation

speedControl() – This function accelerates both the motors from zero to maximum speed by producing PWM signals using analogWrite() function, then it decelerates them back to zero. Finally, it turns the motors off.

void speedControl() {
  // Turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  
  // Accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  }
  
  // Decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  }
  
  // Now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

The rest of the code is same for both speed and direction control.

Controlling Stepper Motor using L298N Motor Driver

Controlling Stepper Motor using L298N Motor Driver
Controlling Stepper Motor using L298N Motor Driver

The connections are fairly simple. Start by connecting external 12V power supply to the VCC terminal. And keep the 5V-EN jumper in place.

You also need to keep both the ENA & ENB jumpers in place so the the motor is always enabled.

Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L298N module to four Arduino digital output pins(8, 9, 10 and 11).

Finally, connect the A+, A-, B+ and B- wires from the stepper motor to the module as shown in the illustration below.

Code for Controlling Stepper Motor

/*
  Code: Stepper Motor Control using L298N Motor using Arduino.
  Circuit Diagram and Working: https://alphaelectronz.com/l298n-motor-driver
  Credit: Alpha Electronz
*/

// Include the Arduino Stepper Library
#include <Stepper.h>

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);


void setup()
{
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() 
{
  // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

The sketch starts with including Arduino Stepper Library. The stepper library comes packaged with the Arduino IDE and takes care of sequencing the pulses we will be sending to our stepper motor.

Projects using L298N Motor Driver

Leave a Reply

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