bclose

L298N motor driver board

Objectives

 

 

    • Present the L298N H-bridge motor driver board.
    • Explain how it is connected and used.
    • Use it to control two DC motors.

 
 
 

Bill of materials

Imagen de Arduino UNO

Arduino Uno.

 

Vista principal

L298N motor driver board.

 Paraja motriz

 Two dc motors.

Img_3_6

Protoboard wires.

Intro

 

The L298N H-bridge driver board module allows us to control the speed and direction of two DC motors or a stepper motor in a very simple way, thanks to the 2 H-bridge it is mounted on.

We have talked about them before, but basically an H-bridge is a component formed by 4 transistors that allows us to reverse the direction of the current, and in this way we can reverse the direction of rotation of the motor.

The voltage range in which this module works ranges from 3V to 35V, and an intensity of up to 2A. When it is time to feed it, it is necessary to take into account that the electronics of the module consume about 3V, so the motors receive 3V less than the voltage with which we feed the module.

In addition, the L298N includes a voltage regulator that allows us to obtain a voltage of 5V from the module, perfect for powering our Arduino. Of course, this regulator only works if we feed the module with a maximum voltage of 12V.

It is a module that is widely used in robotics projects, for its ease of use and its low price.

motor river board

CONNECTION AND OPERATION

 

The Vin (voltage input) admits voltages between 3V and 35V, and just to the right of the image we have the pin that we must connect to GND.

The third connection , V logic, can work in two ways:

 
  • If the regulator jumper is closed we will activate the voltage regulator of the L298N, and in V logic we will have a 5V output, which we can use for what we want, for example to power an Arduino board.
  • If we remove the jumper we will deactivate the regulator, we will need to feed the logical part of the module, so we will have to put a voltage of 5V through the V logic connection for the module to work.
  • Watch out! If we introduce current by logic V with the regulation jumper set, we could damage the module.
  • In addition the regulator only works with voltages up to 12V in Vin, above this value we will have to remove the jumper and feed the logical part of the module from another source. 

The other connections are used in one way or another depending on whether we are going to drive two DC motors or a stepper motor. In this session we are going to focus on the control of DC motors.

The outputs for engines A and B will give us the energy to move the engines. Keep in mind the polarity when connecting them, so that when later we make them move forward, they work as they should. If it were not so, we would have to reverse the connections.

The pins IN1 and IN2 are used to control the direction of rotation of the motor A, and the pins IN3 and IN4 of the motor B. They work so that if IN1 is at HIGH and IN2 at LOW, motor A turns in one direction, and if it is IN1 to LOW and IN2 to HIGH it does to the other. And the same with pins IN3 and IN4 and engine B.

To control the speed of the motors we have to remove the jumpers and use the ENA and ENB pins. We will connect them to two Arduino PWM outputs so that we send a value between 0 and 255 that controls the speed of rotation. If we have the jumpers in place, the motors will rotate at the same speed.

The assembly scheme that we are going to use is going to be the following one, although you can use the pins that you want whenever we respect that those that we connect to ENA and ENB are PWM.

arduino motors

PROGRAMMING

 

Now we are in a position to start programming. We are going to make a little program that moves the motors in both directions, forward and backward, and in opposite directions of each other, also varying the speed of movement.

We start by assigning the pins we are going to use and declaring them as output:

// Motor A
int ENA = 10;
int IN1 = 9;
int IN2 = 8;

// Motor B
int ENB = 5;
int IN3 = 7;
int IN4 = 6;

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

And now we are going to create the functions to move the motors. First to move them forward at full power:

void Forward ()
{
 //Motor A direction
 digitalWrite (IN1, HIGH);
 digitalWrite (IN2, LOW);
 analogWrite (ENA, 255); //Motor A Speed
 //Motor B direction
 digitalWrite (IN3, HIGH);
 digitalWrite (IN4, LOW);
 analogWrite (ENB, 255); //Motor B Speed
}

And now to move in the opposite direction to half power:

void Backward ()
{
 //Motor A direction
 digitalWrite (IN1, LOW);
 digitalWrite (IN2, HIGH);
 analogWrite (ENA, 128); //Motor A Speed
 //Motor B direction
 digitalWrite (IN3, LOW);
 digitalWrite (IN4, HIGH);
 analogWrite (ENB, 128); //Motor B Speed
}

And now we’re going to turn each engine in one direction, each with a speed in addition:

void Right ()
{
 //Motor A direction
 digitalWrite (IN1, HIGH);
 digitalWrite (IN2, LOW);
 analogWrite (ENA, 200); //Motor A Speed
 //Motor B direction
 digitalWrite (IN3, LOW);
 digitalWrite (IN4, HIGH);
 analogWrite (ENB, 100); //Motor B Speed
}

void Left ()
{
 //Motor A direction
 digitalWrite (IN1, LOW);
 digitalWrite (IN2, HIGH);
 analogWrite (ENA, 50); //Motor A Speed
 //Motor B direction
 digitalWrite (IN3, HIGH);
 digitalWrite (IN4, LOW);
 analogWrite (ENB, 150); //Motor B Speed
}

And one more function to stop them:

void Stop ()
{
 //Motor A direction
 digitalWrite (IN1, LOW);
 digitalWrite (IN2, LOW);
 analogWrite (ENA, 0); //Motor A Speed
 //Motor B direction
 digitalWrite (IN3, LOW);
 digitalWrite (IN4, LOW);
 analogWrite (ENB, 0); //Motor B Speed
}

We will combine these functions in the loop, making each one run for a period of time:

void loop ()
{
 Forward ();
 delay (5000);
 Backward ();
 delay (3000);
 Right ();
 delay (2000);
 Left ();
 delay (2000);
 Stop ();
 delay (4000);
}

If, when executing it, the motors move in directions that are not the expected ones, you can either exchange the motor connections on the terminals of the L298N or invert the state of the pins IN1 to IN4.

Summary

 

 

    • We have introduced the L298N module that allows us to control motors of a certain power in a very simple way.
    • We know how to use it and how to connect it to our Arduino.
    • We have learned to use the L298N to control two DC motors.
    • We have created a program with functions to move the motors in both directions and with different speed.

 

No Comments

Give a Reply