Interface L298N DC Motor Driver Module with Arduino

In this user guide, we will learn about L298N Motor Driver and how to use it with Arduino. This is an in-depth guide about the L298N motor driver including its specifications, pinout, interfacing with Arduino board and eventually learning how to control DC motors with it.

Interface L298N DC Motor Driver Module with Arduino

We have a similar guide for ESP32 and ESP8266 using MicroPython and Arduino IDE:

L298N Motor Driver Module

The L298N motor driver module is very easy to use with Arduino and relatively inexpensive as well. It is widely used in controlling robots as we can connect up to four motors at once but if we want to control the speed and direction as well then it allows two motors to be connected. Thus, it is perfect for two-wheeled robots. This module is mainly used in robotics and in controlling dc and stepping motors.

The L298N motor driver module consists of an L298N motor driver IC, 78M05 5V regulator, 5V jumper enable, power LED, heat sink, resistors, and capacitors all combined in an integrated circuit. The diagram below shows all the components consisting inside the module.

L298N Motor Driver pic1
L298N Motor Driver Module

The L298N Motor driver IC is powerfully built with a big heat sink. It is a dual-channel H bridge motor driver which can be easily used to drive two motors.

The module also has a 78M05 5V regulator which is enabled through a jumper. Keeping the jumper intact, means the 5V regulator is enabled. If the motor power supply is less than 12V then we will power the module through the voltage regulator. The 5V pin in this case acts as an output to power the microcontroller. If the power supply is more than 12V, make sure the jumper is not intact and supply 5V power through the pin separately.

Note: If the jumper is connected, do not supply power to both the motor power supply input and the 5V power supply input.

Specifications

The table shows some specifications of the L298N motor driver module:

Driver ModelL298N
Driver ChipDouble H-bridge L298N
Maximum Power25W
Maximum Motor Supply Voltage46V
Maximum Motor Supply Current2A
Driver Voltage5-35V
Driver Current2A
Size43x43x26mm
L298N Module Specifications

PinOut

Let us now look at the pinout of the module.

L298N Motor Driver pic2
Pin NameDescription
VCCThis is the pin which supplies power to the motor. It is imprinted with +12V on board but can be powered between 6-12V.
GroundThis is the common ground pin.
5VThis pin supplies the power (5V) for the internal circuit (L298N IC). Will be used only if the 5V enable jumper is not intact. If jumper is intact, then it acts as an output pin.
ENAThis pin controls the speed of the motor A by enabling the PWM signal.
IN1 & IN2These are the input pins for motor A. They control the spinning direction for that particular motor.
IN3 & IN4These are the input pins for motor B. They control the spinning direction for that particular motor.
ENBThis pin controls the speed of the motor B by enabling the PWM signal.
OUT1 & OUT2OUT1: Positive terminal.
OUT2: Negative terminal

These are the output pins for motor A. Motor A having voltage between 5-35V, will be connected through these two terminals.
OUT3 & OUT4OUT3: Positive terminal
OUT4: Negative terminal

These are the output pins for motor B.

Controlling DC motors through L298N Driver Module

Let us now see the details behind controlling the dc motor through the L298N module.

Control Pins

There are two types of control pins found at the bottom right side of the module. One type controls the speed and the other type controls the direction of the motor.

Speed Control (ENABLE) Pins

The speed control pins labeled ENA and ENB on the module, control the speed of the dc motor and turn it ON and OFF.

L298N Motor Driver pic4
Speed Control Pins

ENA controls the speed of motor A and ENB controls the speed of motor B. If both of the pins are in a logic HIGH (5V) state, then both the motors are ON and spinning at maximum speed. If both of the pins are in a logic LOW (ground) state, then both the motors are OFF. Through the PWM functionality, we can also control the speed of the motor. By default, there is a jumper connected to these pins which keep these pins in a HIGH state. In order to control the speed, we need to remove the jumper and connect these terminals with the PWM pins of Arduino and program them in code. The table below demonstrates the logic signals required for controlling Motor A.

ENA Pin StateMotor Action
1 (HIGH)ON
0 (LOW)OFF

If ENA is in a HIGH state, the motor is enabled and if it is in a LOW state then the motor is off.

Direction Control (INPUT) Pins

The direction control pins are the four input pins (IN1, IN2, IN3, IN4) on the module.

L298N Motor Driver pic3
Direction Control Pins

Through these input pins we can determine whether to move the dc motor forward or backwards. IN1 and IN2 control motor A’s spinning direction whereas IN3 and IN4 control motor B’s spinning direction. The table below shows the logic signals required for the appropriate spinning action for motor A.

IN1IN2Motor Action
1 (HIGH)1OFF
10 (LOW)Backward
01Forward
00OFF

As seen from the table, whenever one of the inputs is in a HIGH state (5V) then the motor will spin. Otherwise, when both the inputs are LOW (ground) state or both are in HIGH state then the motor stops. In order for motor A to spin forward, IN1 should be LOW and IN2 should be HIGH. For backwards motion, IN1 should be HIGH and IN2 should be LOW. Motor B is also controlled in a similar way.

Interface L298N DC Motor Driver with Arduino UNO

Now, as we have seen how to control the dc motor through the motor driver, let us do a demonstration by showing you how to control two DC motors using this driver.

Required Equipment

  1. Arduino UNO
  2. L289N Motor driver Module
  3. External 3-12 V power supply
  4. 2 DC Motors
  5. Connecting Wires

Assemble the circuit as shown in the connection diagram below.

Arduino UNO with L298N Motor driver with DC motors connection diagram
Arduino UNO with L298N Motor Driver and DC Motors

We are using TT DC gear motors for this project. They require an operating voltage of 3-12V DC where the recommended operating voltage is 3-6V DC. Thus we will use 4xAA batteries (4×1.5V = 6V) to supply power for the DC motors. Each of the motor’s terminal is connected at OUT1, OUT2 for motor A and OUT3, OUT4 for motor B respectively.

We are keeping the 5V Enable jumper in its place as it will power up the L298N motor driver. Then, we have connected the input pins IN1, IN2, IN3 and IN4 with digital pins of the Arduino UNO. We have used GPIO9, GPIO8, GPIO7 and GPIO6 respectively to connect with each of the input pins of the motor driver. The enable pins ENA and ENB are connected with the digital PWM pins of the Arduino UNO board. We have used GPIO10 to connect with ENA and GPIO5 with ENB.

Arduino Sketch: Controlling DC Motors using L298N Motor Driver

Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it.

This basic sketch will show us how to control a DC motor’s speed and direction of rotation using the L298N motor driver.

int ENA = 10;
int IN1 = 9;
int IN2 = 8;
int IN3 = 7;
int IN4 = 6;
int ENB = 5;

void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void loop() {
  setDirection();
  delay(1000);
  changeSpeed();
  delay(1000);
}

void setDirection() {
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delay(5000);
  
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(5000);
  
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void changeSpeed() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  
  for (int i = 0; i < 256; i++) {
    analogWrite(ENA, i);
    analogWrite(ENB, i);
    delay(20);
  }
  
  for (int i = 255; i >= 0; --i) {
    analogWrite(ENA, i);
    analogWrite(ENB, i);
    delay(20);
  }
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

How the Code Works?

Firstly, we will define the L298N motor driver’s control pins connection with the Arduino UNO board. We have used the same pins as shown in the connection diagram above. Make sure the enable pins are connected with PWM enabled pins of the Arduino board. For the input pins you can use any digital pin of the Arduino UNO board.

int ENA = 10;
int IN1 = 9;
int IN1 = 8;
int IN3 = 7;
int IN4 = 6;
int ENB = 5;

setup()

Inside the setup() function, first we will configure all the control pins as output pins. This will be done by using the pinMode() function. The pin will be passed as the first parameter and OUTPUT will be passed as the second parameter.

  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

Then by using the digitalWrite() function we will set up all the input pins to a LOW state so that initially both the motors are off.

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

loop()

Inside the loop() function, we will call the user defined functions setDirection() and changeSpeed() after a delay of 1 second. The first function will be used to control the direction of the motors and the second function will control the speed.

void loop() {
  setDirection();
  delay(1000);
  changeSpeed();
  delay(1000);
}

setDirection()

void setDirection() {
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);


  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delay(5000);
  

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(5000);
  

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

The setDirection() function is responsible to control the direction of the two motors. The motors can either turn on, off or change their directions. This is done by providing the input pins (IN1, IN2, IN3 and IN4) with different logic signals. As we have already discussed before, through these input pins we can determine whether to move the dc motor forward or backwards.

First we will set up both the motors at their maximum speed. This will be done by using the analogWrite() function and passing the enable pin as the first parameter and ‘255’ as the second parameter. Here 255 denotes the highest PWM value.

  analogWrite(ENA, 255);
  analogWrite(ENB, 255);

Then we will first turn both the motors ON, then change their direction and then turn them OFF. These three actions will be done after a delay of 5 seconds each. Use digitalWrite() to change the logic state of the pin by passing the pin as the first parameter and the logic state as the second parameter. Follow the table given in the Direction Control Pins, section to set the input pins to required states appropriately.

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delay(5000);
  

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(5000);
  

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

changeSpeed()

void changeSpeed() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  
  for (int i = 0; i < 256; i++) {
    analogWrite(ENA, i);
    analogWrite(ENB, i);
    delay(20);
  }
  

  for (int i = 255; i >= 0; --i) {
    analogWrite(ENA, i);
    analogWrite(ENB, i);
    delay(20);
  }

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

The changeSpeed() function is responsible to control the speed of the two motors. As we have already discussed before, the speed control pins labeled ENA and ENB on the module, control the speed of the dc motor.

First, by using the digitalWrite() function turn on the motors by setting the relevant logic states to the input pins.

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);

Then we will increase the speed of the motors steadily. By using a for loop we will increment the PWM value from 0 to 255 (maximum speed). This is done through the analogWrite() function which takes in the enable pin as the first parameter and the PWM value as the second parameter.

  for (int i = 0; i < 256; i++) {
    analogWrite(ENA, i);
    analogWrite(ENB, i);
    delay(20);
  }

Likewise, we will decrease the speed of the motors from a maximum (255) to zero.

  for (int i = 255; i >= 0; --i) {
    analogWrite(ENA, i);
    analogWrite(ENB, i);
    delay(20);
  }

Lastly, we will turn the motors off by providing a LOW signal to all the input pins.

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

Demonstration

To see the demonstration of the above code, upload the code to Arduino. Before uploading the code, make sure to select Arduino UNO from Tools > Board.

select Arduino uno

Also, select the correct COM port to which the Arduino board is connected from Tools > Port.

Once the code is uploaded to your board, the motors will start rotating.

First, the motors will start rotating backwards for 5 seconds then they will start rotating forwards for 5 seconds. Then the motors stop. After a delay of 1 second, the motors start speeding up and reach maximum speed then they start slowing down and finally stop. Then the loop starts again.

We also have a similar guide to control a stepper motor with L298N and Arduino:

For more L298N Motor Driver articles:

Leave a Comment