There are many modules for Arduino or for use in DIY projects by makers. In the case of L298N is a module to control motors. 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 motorized actuators, although it can be used for a multitude of applications.
We have already entered everything you need about the ESP module, with ESP8266 chip, module that allows to extend the capacities Arduino boards and other projects so that they have WiFi connectivity. These modules can not only be used in isolation, the good thing is that they can be combined. For example, an ESP8266 can be used for our prototype and the L298N, with which we would get a controllable motor through the Internet or wireless.
Introduction to the L298N and datasheets:
Although with Arduino you can also work with stepper motors, which are 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 in the manufacturers' datasheets, such as STMicroelectronics from this link. If you want to see a datasheet of the specific module, and not just the chip, you can download this other PDF of the Handsontec L298N.
But broadly speaking, an L298N is a H-bridge type driver that allows the speed and direction of rotation of DC motors to be controlled. It can also be used with stepper motors easily thanks to the 2 H bridge that implements. That is to say, a bridge in H, which means that it is formed by 4 transistors that will allow to reverse the direction of the current so that the rotor of the motor can rotate in one direction or another as we want. This is an advantage over controllers that only allow you to control the rotational speed (RPM) by controlling only the value of the supply voltage.
The L298N can work with various voltages, from 3v to 35v, and at an intensity of 2A. This is what will really determine the performance or rotational speed of the motor. It must be borne in mind that the electronics that the module consumes usually consume around 3v, so the motor will always receive 3v less from the power 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 inverse to what we did with the LM35, in this case, instead of obtaining 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 the Arduino board to be powered at 5v as long as we are powering the driver with at least 12v voltage.
Integration with Arduino
There are multitude of projects with which you can use this module L298N. In fact, you can just imagine everything you could do with it and get to work. For example, a simple example would be the control of two direct current motors as can be seen in the previous diagram made with Fritzing.
Before working with the L298N we must take into account that the input of the module or Vin supports voltages between 3v and 35v and that we must also connect it to ground or GND, as can be seen in the image with the red and black cable respectively. Once connected to the power, the next thing is to connect the motor or the two motors that it accepts to control simultaneously. This is simple, you only have to connect the two terminals of the motor to the connection tab that has the module on each side.
And now comes perhaps the most complicated, and is to connect the module connections or pins to Arduino's properly. Remember that if the module's regulator jumper or bridge is closed, that is, on, the module's voltage regulator 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 power Arduino independently. eye! Because the jumper can only be set up to 12v voltages, for more than that you must remove it so as not to damage the module ...
You can appreciate that there are 3 connections for each motor. Those marked as 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 won't have to put them all. The jumpers on each side of these connections for each motor are ENA and ENB, that is, to activate motor A and B, which must be present if we want both motors to work.
FOR motor A (It would be the same for B), we must have IN1 and IN2 connected that 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 the other. To control the speed of rotation you must remove the INA or INB jumpers and use the pins that appear to connect it to the Arduino PWM, so that if we give it a value from 0 to 255 we obtain a low or higher speed respectively.
As for the programming is also easy in the Arduino IDE. For example, a code would be:
<pre>// Motor A int ENA = 10; int IN1 = 9; int IN2 = 8; // Motor B int ENB = 5; int IN3 = 7; int IN4 = 6; void setup () { // Declaramos todos los pines como salidas pinMode (ENA, OUTPUT); pinMode (ENB, OUTPUT); pinMode (IN1, OUTPUT); pinMode (IN2, OUTPUT); pinMode (IN3, OUTPUT); pinMode (IN4, OUTPUT); } //Mover los motores a pleno rendimiento (255), si quieres bajar la velocidad puedes reducir el valor hasta la mínima que son 0 (parados)</pre> <pre>//Para mover los motores en sentido de giro contrario, cambia IN1 a LOW e IN2 a HIGH void Adelante () { //Direccion motor A digitalWrite (IN1, HIGH); digitalWrite (IN2, LOW); analogWrite (ENA, 255); //Velocidad motor A //Direccion motor B digitalWrite (IN3, HIGH); digitalWrite (IN4, LOW); analogWrite (ENB, 255); //Velocidad motor B }</pre>