L298N: motor control module for Arduino


There are many modules for Arduino or for use in DIY projects by makers. In the case of the L298N it is a module for motor control. With them you can use simple codes to program our Arduino board and be able to control DC motors in a simple and controlled way. Generally, this type of module is used more in robotics or in actuators using motors, although it can be used for many applications.

We have already introduced everything necessary about the ESP module, with ESP8266 chip, a module that allows to extend the capacities of the Arduino boards and other projects to have WiFi connectivity. These modules can not only be used alone, the good thing is that they can be combined. For example, we can use an ESP8266 for our prototype and the L298N, which would give us an engine that can be controlled over the Internet or wireless.

Introduction to L298N and datasheets:

l298n pinout

Although with Arduino you can also work with stepper motors, which are quite well known in robotics, in this case it is usually more common to use the controller or driver for DC motors. You can get information about the L298 chip and the modules from the manufacturers’ datasheets, like the one at STMicroelectronics from this link. If you want to see a datasheet of the specific module, and not only of the chip, you can download this other PDF of the Handsontec L298N.

But broadly speaking, an L298N is an H-bridge type driver that allows speed and direction of rotation control of DC motors. It can also be used with stepper motors in a simple way thanks to the 2 H-bridge it implements. That is to say, an H-bridge, which means that it is made up of 4 transistors that will allow the direction of the current to be reversed so that the motor’s rotor can rotate in one direction or the other as we wish. This is an advantage over controllers that only allow you to control the speed of rotation (RPM) by controlling only the value of the supply voltage.

The L298N can work with several voltages, from 3v to 35v/strong and at a current of 2A. This is what will really determine the performance or speed of the motor. It is necessary to consider that the electronics that the module consumes usually consume around 3v, reason why the motor is always going to receive 3v less of the feeding to which we are feeding it. It is a somewhat high consumption, in fact it has a high power element that needs a heatsink as you can see in the image.

To control the speed, you can do something opposite to what we did with the LM35, in this case, instead of getting a certain voltage at the output and having to convert it into degrees, here it will be the opposite. We feed the driver with a lower or higher voltage to obtain a faster or slower turn. In addition, the L298N module also allows you to feed the Arduino board at 5v as long as you are feeding the driver with at least 12v.

Integration with Arduino

l298n circuit diagram with Arduino

There are multiple projects you can use this L298N module with. In fact, you can simply imagine all that you could do with it and get on with it. For example, a simple example would be the control of two DC motors as you can see in the diagram above made with Fritzing.

Before working with the L298N we must take into account that the input of the module or Vin admits voltages between 3v and 35v and that we must also connect it to ground or GND, as shown in the image with the red and black wires respectively. Once connected to the power supply, the next thing is to connect the motor or the two motors that it accepts to control simultaneously. This is imple, you only have to connect the two terminals of the motor to the connection card that has the module at each side.

And now comes perhaps the most complicated part, and that is to connect the module connections or pins to the Arduino ones in a proper way. Remember that if the jumper of the module is closed, the voltage regulator of the module is activated and there is a 5v output that you can use to power the Arduino board. On the other hand, if you remove the jumper, you deactivate the regulator and you need to feed Arduino independently. Watch out! Because the jumper can only be set up to 12v, for more than that you must remove it to avoid damaging the module…

You can see that there are 3 connections for each motor. The ones marked IN1 to IN4 are the ones that control motors A and B. If you don’t have one of the motors connected because you only need one, then you don’t have to put them all. The jumpers on either side of these connections for each motor are ENA and ENB, that is to say, to activate motor A and B which must be there if both motors are to work.

For the motor A (it would be the same for B), we must have IN1 and IN2 connected which will control the direction of rotation. If IN1 is HIGH and IN2 is LOW, the motor turns in one direction, and if they are LOW and HIGH, it turns in the other. To control the rotation speed you must remove the INA or INB jumpers and use the pins that appear to connect it to Arduino’s PWM, so that if we give a value of 0 to 255 we obtain a low or higher speed respectively.

As for programming, it is also simple in Arduino IDE. For example, a code would be

{pos(192,210)}Engine A
int ENA = 10;
int IN1 = 9;
int IN2 = 8;

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

void setup ()
 // We declare all pins as outlets
 pinMode (ENA, OUTPUT);
 pinMode (ENB, OUTPUT);
 pinMode (IN1, OUTPUT);
 pinMode (IN2, OUTPUT);
 pinMode (IN3, OUTPUT);
 pinMode (IN4, OUTPUT);
//Move the motors to full capacity (255), if you want to lower the speed you can reduce the value to the minimum which is 0 (stopped)
{pos(192,210)}To move the motors in the opposite direction, change IN1 to LOW and IN2 to HIGH

void Forward ()
 // Engine steering A
 digitalWrite (IN1, HIGH);
 digitalWrite (IN2, LOW);
 analogWrite (ENA, 255); //Motor speed A
 // Engine steering B
 digitalWrite (IN3, HIGH);
 digitalWrite (IN4, LOW);
 analogWrite (ENB, 255); //Motor speed B
}</pre ;

Leave a Comment