Skip to content

Interfacing L298N Motor Driver With Arduino & DC Motor

    Definitely, you need to know the easy way to control Dc motors, right?

    You are in a right place in this tutorial you will learn how to Interface the L298N motor driver with Arduino which is the easiest way to control 2 DC motors at the same time.

    In addition, you will learn about the L298N motor driver from the pinout, circuit diagram, datasheet, and of course the code at the end of this tutorial. 

    So complete this tutorial to the end to learn everything you need to know.

    but before that let’s learn how to control a DC motor

    Controlling a DC Motor

    To control a DC motor you need to control the speed and rotation direction, so there are two methods to do that:

    • PWM: this method is used to control the speed of the dc motor. 
    • H Bridge: this method is used to control the rotation direction of the DC motor.

    DC Motor Speed Control Using PWM

    PWM (Pulse Width Modulation) is a method that allows us to control the DC motor speed by changing the average input voltage value by adjusting the on-off pulses series of the Duty Cycle.

    The Duty Cycle is a series of on-off pulses, so to increase the motor speed you need to apply a higher average input voltage as result higher duty cycle percentage, and to decrease the motor speed you need to apply a lower average input voltage as a result lower duty cycle percentage. 

    So let’s just say you need to rotate the DC motor at the maximum speed so you need to apply 12V average input voltage to do that you need to set the Duty Cycle at 100%  percentage.

    In the picture down below we apply this method in different Duty Cycle so you can see the difference of average input voltage with each Duty Cycle percentage.

    Dc Motor Speed Control Using PWM - ROBOSANS
    Dc Motor Speed Control Using PWM

    DC Motor Direction Control Using H Bridge

    H-Bridge is a method that allows us to control the DC motor rotation direction by changing the polarity of the input voltage so the current that flows through the motor will be reversed by using 4 switches.

    H-bridge consists of 4 switches in the shape of the character ‘H’ and the motor is placed at the center, so we can control the rotation direction by closing the (S1, S4), and opening the (S2, S3) switches and vice versa, so the polarity will be reversed and rotation direction will be changed.

    In the picture down below you can see how in detail how the direction will be changed.

    Dc Motor Direction Control Using H Bridge - ROBOSANS
    Dc Motor Direction Control Using H Bridge

    L298N Motor Driver

    The L298N Motor Driver is a controller chip with dual H-Bridge which allows controlling the direction and speed of up to 2 DC motors easily at the same time. L298N module is capable of driving motors with voltages between (5-35)V and up to 2A maximum current which is ideal for DC motors.  

    L298n Motor Driver - ROBOSANS
    L298n Motor Driver

    L298N Motor Driver Specifications

    To know the technical specifications for the L298N motor driver take a look at the table down below.

    Drive voltage(5 – 35)V 
    Logic voltage5V
    Drive current2A  for each channel
    Maximum power25W  
    L298N Motor Driver Specifications

    L298N Motor Driver Pinout

    Now let’s take a look at the L298N motor driver pinout in this picture down below and talk about it in detail.    

    L298N Motor Driver Pinout - ROBOSANS
    L298N Motor Driver Pinout

    VCC: This is a power pin that accepts to apply between (5-35)V on it but we recommend between (5-12)V input voltage, but keep in mind that you need to supply 2V extra than the actual voltage requirement of the motor to be rotated at the maximum speed if the jumper is in place.

    5V: This pin is used to power the logic circuit by 5V which is inside the  L298N IC.

    GND: this pin is common ground.

    OUT1 & OUT2: These are the output pins for motor A which we can connect with Dc motor (5-12)V.

    OUT3 & OUT4: These are the output pins for motor B which we can connect with Dc motor (5-12)V.

    IN1 & IN2: These are the pins that control the rotation direction of motor A whether it is run forward or backward, these pins control the switches inside the L298N chip of the H-Bridge.

    You can control the rotation direction by setting these inputs at logic 1(5V) or 0(GND), you can take a look at the possibilities in the table down below.

    Rotation DirectionIN1IN2
    Motor OFF0(GND)0(GND)
    Forward1(5V)0(GND)
    Backward0(GND)1(5V)
    Motor OFF1(5V)1(5V)
    IN1 & IN2 possibilities

    IN3 & IN4: These are the pins that control the rotation direction of motor B the same as IN1 & IN2 pins, take a look at the possibilities in the table down below.

    Rotation DirectionIN3IN4
    Motor OFF0(GND)0(GND)
    Forward1(5V)0(GND)
    Backward0(GND)1(5V)
    Motor OFF1(5V)1(5V)
    IN3 & IN4 possibilities

    ENA & ENB: These are the pins that control the speed of motor A and B, so if we apply logic 1 the motor will be rotated at the maximum speed, and by logic 0 the motor will be stopped.

    But if we connect these pins with the Arduino pins that support PWM and remove the jumper we can control the speed by programming (by Arduino code in this case).

    L298N Jumper & 5V Regulator

    This module has a 5V regulator on-board the L298N which can be disabled or enabled by using the jumper, so if the motor is supplied by 12V the 5V regulator can be enabled and use a 5V pin as an output.

    But be aware, if the motor is supplied by more than 12V the jumper must be disconnected to avoid damaging the 5V regulator, so we can use a 5V pin as an input and connect it to the power supply(5V) to work correctly.

    L298N Jumper & 5v Regulator - ROBOSANS
    L298N Jumper & 5v Regulator

    L298N Voltage Drop

    As you can see in the picture down below there is a 2V voltage drop, so if connect a DC motor with a 12V power supply the DC motor will receive 10V which means you can’t rotate a 12V DC motor at the maximum speed.

    So keep in mind if you need to rotate the DC motor at the maximum speed and you have a 12V DC motor the power supply should be (14V).

    L298N Voltage Drop - ROBOSANS
    L298N Voltage Drop

    L298N Motor Driver Schematic

    This picture down below is the L298N motor driver schematic, you can look at it.

    L298N Motor Driver Schematic
    L298N Motor Driver Schematic. Source: Handsontec.

    L298N Motor Driver Datasheet

    If you need to know more technical details you can read the L298N motor driver datasheet.

    L298N Motor Driver Datasheet PDF

    This file down below is the L298N motor driver IC datasheet PDF you can download it.

    L298N Motor Driver With Arduino

    Now let’s connect the L298N motor driver with Arduino and 2 Dc motors, but you might be asking why I need an L298N if I have Ardunio because the Ardunio doesn’t have enough voltage and current to be able to drive a motor, we need the H-Bridge that is integrated into the L298 to control the rotation direction, and PWM to control the speed.

    Parts Requirement:

    • Arduino Uno
    • L298N Motor Driver
    • (12V) battery
    • Two DC motors
    • Hook-up wires

    Interfacing L298N Motor Driver With Arduino 

    To interface L298N motor diver with Arduino we need to connect the first motor with (OUT1, OUT2)pins and the second motor with (OUT3, OUT4)pins for the L298N, as well as connect the 12V battery (+V) with (VCC) for the L298N and (-V) with (Gnd) for both Arduino and L298N, and finally, we need to connect the digital pins for the Arduino (3, 4, 5, 7, 8, 9) with the (ENB, IN4, IN3, IN2, IN1, ENA)pins for the L298N. 

    Interfacing L298N Motor Driver With Arduino - ROBOSANS
    Interfacing L298N Motor Driver With Arduino

    For the DC motor, you can use the (TT) DC Gearbox Motor that we use, the rated voltage for it is between (3-12)V, so keep in mind the motor will not rotate at the maximum speed because of 2V drop voltage but that’s fine.

    To supply the 5V for the logic circuit we need to keep the jumper in place to use a 5V regulator on-board the L298N.

    L298N Motor Driver Fritzing

    This is the L298N motor driver fritzing file you can download it.

    L298N Motor Driver Arduino Code

    finally, this is the L298n motor driver Arduino code so you can accelerate, decelerate and change the rotation direction of the Dc motor.

    //The connection of the motor A 
    #define EN_A 9
    #define IN1 8
    #define IN2 7
    //The connection of the motor B 
    #define EN_B 3
    #define IN3 5
    #define IN4 4
    
    void setup() {
      //To Set the control pins of the motor as an outputs
      pinMode(EN_A, OUTPUT);
      pinMode(EN_B, OUTPUT);
      pinMode(IN1, OUTPUT);
      pinMode(IN2, OUTPUT);
      pinMode(IN3, OUTPUT);
      pinMode(IN4, OUTPUT);
    
      //To turn off the motors 
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);
    
      //Initialize serial port
      Serial.begin(9600);
    }
    
    void loop() {
    
      if (Serial.available() ){
        if (Serial.read() == 'A'){
          //rotate motors Forward
          motorForward();      
          motorRun();
        }
        else if (Serial.read() == 'B'){
          //rotate Counter Backward
          motorBackward();
          motorRun();
        }
        else if (Serial.read() == 'C'){
          //turn off the motor
          motorOff();
        }    
      }
      
    }
    
    void motorForward(){
      // Motor A Forward
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      // Motor B Forward
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);  
    }
    
    void motorBackward(){
      // Motor A backward
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      // Motor B backward
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
    }
    
    void motorOff(){
      // Motor A Off
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      // Motor A Off
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);
    }
    
    
    //Run the motor by accelerating from 0 to top speed
    // then decelerating to 0
    void motorRun() { 
      //To accelerate the motor from 0 to the maximum speed
      for (int i = 0; i < 256; i++) {
        analogWrite(EN_A, i);
        analogWrite(EN_B, i);
        delay(20);
      }
      
      //To Decelerate the motor from the maximum speed to 0
      for (int i = 255; i >= 0; --i) {
        analogWrite(EN_A, i);
        analogWrite(EN_B, i);
        delay(20);
      }
      
      //Finally to turn off the motors
      motorOff();
    }

    Frequently Asked Questions

    What Is L298N Motor Driver

    The L298N Motor Driver is a controller chip with an integrated dual H-Bridge to control the direction and speed of up to 2 DC motors easily at the same time. L298N module is capable of driving motors with voltages between (5-35)V and up to 2A maximum current which is ideal for DC motors.  

    How To Use L298N Motor Driver

    To use the L298N motor diver with Arduino we need to connect the first motor with (OUT1, OUT2)pins and the second motor with (OUT3, OUT4)pins for the L298N, as well as connect the 12V battery (+V) with (VCC) for the L298N and (-V) with (Gnd) for both Arduino and L298N, and finally, we need to connect the digital pins for the Arduino (3, 4, 5, 7, 8, 9) with the (ENB, IN4, IN3, IN2, IN1, ENA)pins for the L298N.

    How Many Motors Can L298N Control

    The L298N Motor Driver can control the direction and speed of up to 2 motors easily at the same time.

    Thanks for being with us until the end of this tutorial, we really appreciate it and we hope you find everything you need about interfacing L298N motor diver with Arduino and Dc motor.

    ROBOSANS is the best place that provides you with a brief Arduino tutorial to start your learning journey. 

    if you are passionate about Arduino and want to learn more, you can read these articles down below.

    How to control 28BYJ-48 stepper motor with Arduino by ULN2003 Driver

    Interfacing L298N Motor Driver With Arduino & DC Motor

    Sources:

    arduino.cc

    wikipedia.org