DC motor control using L298N motor driver and Arduino

DC motor control using L298N motor driver and Arduino

Dc motors are used in a number of applications especially robotics. The main challenge with using these motors with microcontrollers is that they cannot be connected directly to the microcontroller I/O pins. This is because motors use high currents while microcontroller circuits work on low current signals. Motor drivers are therefore needed to convert the low-current control signals to a higher-current signal that can drive the motor.

In this tutorial I will show you how the L298N motor driver works and how it can be used to control the speed and direction of rotation of DC motors using Arduino.

L298N motor driver Hardware overview.

The L298N motor driver module has two screw terminal blocks for the connecting two motors A and B. There is also a power supply screw terminal block containing the Ground pin, the VCC for motor and a 5V pin which can either be an input or output. The diagram below shows the pin out of this motor driver.

l298n motor driver pin configuration

The module has an onboard 5V regulator for controlling the voltages supplied and used by the motor connected to the L298N motor driver. The driver can drive motors with voltages from 5V to 35V with a peak current of 2A. This voltage is controlled by either enabling or disabling a jumper.

If the motor supply voltage to the module is up to 12V, the 5V regulator is enabled so that we can use the 5V pin as output for powering a microcontroller like an Arduino board.

However if the supply voltage is greater than 12V the jumper should be disconnected to avoid destroying the onboard 5V regulator. In this case the 5V pin will be used as input as we need to connect it to a 5V power supply in order for the IC to work properly.

Input pins IN1, IN2, IN3 and IN4 are for controlling the direction of rotation of the motors where IN1 and IN2 control the direction of rotation of motor A while IN3 and IN4 control direction of rotation of motor B.

The Enable pins ENA and ENB are speed control pins for motor A and B respectively. These pins usually come covered with jumpers. When the jumper is in place, the motor is enabled and spins at maximum speed. If you want to control the speed of motors, you need to remove the jumpers and connect them to PWM-enabled pins on Arduino.

How does the L298N motor driver work?

The L298N motor driver controls the speed of rotation of a dc motor using PWM signals and the direction of rotation is determined by an H-bridge. These techniques are explained in detail below.

Motor speed control using PWM

PWM enables us to control the voltage applied to the motor in form of square wave pulses with a certain frequency. The voltage applied to the motor determines the speed of rotation of the motor by varying the width of this square wave called a duty cycle. The duty cycle is given in percentage and the higher the duty cycle, the higher the voltage across the motor which also increases the speed of rotation.

When the duty cycle is 100%, the pulse is constantly HIGH and the motor receives full power and spins at its rated output speed while a duty cycle of 0% means the pulse signal is constantly LOW therefore no voltage across the motor and the motor will stop rotating.

I have a post with a more detailed explanation of how the PWM technique is used with Arduino which you can refer to using the link below.

  • Arduino PWM Output and LED brightness control.
  • Direction of Rotation control using H-Bridge.

    The direction of rotation of a motor is determined by the direction of flow of current through the motor. This is achieved using an H-bridge circuit which is used for switching the polarity of a voltage applied to a load like a motor in this case.

    How an H-Bridge works.

    An H-Bridge consists of four MOSFETs or Transistors wired as switches. When two of these switches are activated at the same time in a particular format, the direction of flow of current is changed which then changes the direction of rotation of the motor. You can refer to the diagram below to see how the H-Bridge works.

    working of an H-bridge to control dc motor

    When switches S1 and S4 are closed the current will flow from left to right through the motor which makes the motor rotate in a particular direction, in this case clockwise. Likewise if switches S2 and S3 are closed, current will flow from right to left and the motor rotates in the opposite direction.

    Do not switch on S1 and S2 together or S3 and S4 together. This condition is called shoot-through and can damage the MOSFETs or transistors.

    The L298N motor driver module has two H-Bridge circuits and therefore can control two dc motors simultaneously. The pins IN1, IN2, IN3 and IN4 are actually for controlling the switches of the H-bridge circuit of the L298N module.

    If IN1 is LOW and IN2 is HIGH then motor A will rotate in a particular direction and if IN1 is HIGH and IN2 is LOW then the motor rotates in the opposite direction. If all the inputs have the same signal, say IN1 and IN2 are both LOW and both HIGH then the motor will stop rotating.

    The table below shows how the direction of rotation of motor A and B changes depending on the state of the input pins IN1, IN2, IN3 and IN4.

    motor direction control

    DC motor control using the L298N motor driver and Arduino.

    The speed and direction of rotation of the motors can be achieved easily by connecting the L298N driver to an Arduino board. For example the schematic diagram below shows the setup of a motor to be controlled by the L298N driver and Arduino.

    dc motor control using l298n motor driver and arduino

    Code for running the DC motor using L298N motor driver.

    The code below is a simple sketch showing the basic control of speed and direction of rotation of the dc motor by varying the state of the input and enable pins.

    // Motor Two
    int enB = 3; // enable pin should be connected to Arduino PWM pin
    int IN3 = 5;
    int IN4 = 6;
    void setup () {
    // Set all the motor contol pins as outputs
    pinMode (enB, OUTPUT);
    pinMode (IN3, OUTPUT);
    pinMode (IN4, OUTPUT);
    }
    void demoOne () {
    /* This function will run the motor in both directions
    at a fixed speed
    */
    // Turn ON Motor Two
    digitalWrite (IN3, HIGH);
    digitalWrite (IN4, LOW);
    // Set speed to 200 out of possible range (0 - 255)
    analogWrite (enB, 200);
    delay (2000);
    // Now, change motor directions
    digitalWrite (IN3, LOW);
    digitalWrite (IN4, HIGH);
    delay (2000);
    // Now, turn OFF Motors
    digitalWrite (IN3, LOW);
    digitalWrite (IN4, LOW);
    }
    void demoTwo () {
    /* * This function will run the motors across the range of
    possible speeds
    * Note that maximum speed is determined by the motor itself
    and the operating voltage
    * The PWM values sent by "analogWrite ()" are functions of
    the maximum speed possible by the hardware
    */
    // Turn ON motor
    digitalWrite (IN3, LOW);
    digitalWrite (IN4, HIGH);
    // Decelerate from maximum speed to zero
    for (int i = 200; i >= 0; i --) {
    analogWrite (enB, i);
    delay (20);
    }
    // Now, turn OFF motor
    digitalWrite (IN3, LOW);
    digitalWrite (IN4, LOW);
    }
    void loop () {
    demoOne ();
    delay (2000);
    demoTwo ();
    delay (2000);
    }
    
    

    Code description

    First we define the pins and some variables needed for the program. In the setup section we need to set the pin modes for the input and enable pins.

    The demoOne() function runs the motor in both directions at a fixed speed of 200 which is set using the analogWrite() function to send a PWM signal to the enable pin.

    The demoTwo() function runs the motor across a specified range of speeds.

    When the code is uploaded to the board, the dc motor first rotates anticlockwise, then rotates clockwise and finally the motor rotates from a maximum speed and decreases gradually to zero.

    The L298N motor driver is used in a number of applications and I have demonstrated it’s use in robot cars in one of my posts. You can make reference through this link:

  • Remote controlled Car Using Arduino
  • Back to top