Site icon AranaCorp

Using an L298N module with Arduino

3.7
(3)

The L298N module is a development board based on the L298N integrated circuit allowing the driving of a DC motor or a stepper motor. We have already seen the use of the H-bridge. It can be found as a convenient module to use with different performances depending on the components used. We will see in this tutorial the use of the L298N module with an Arduino microcontroller.

Material

Principle of operation

The L298N module is based on the integrated circuit of the same name. The L298N circuit is a double H-bridge allowing to modify the direction and the intensity of the voltage at the terminal of two electric loads.

The characteristics of the L298N module are:

Wiring diagram

The L298N module is powered by an external voltage source. Each half-bridge requires 3 output pins of a microcontroller.

On some models of this module, it is possible to add two jumpers to directly connect the enable pins (ENA and ENB) to 5V. This allows to always activate the H-bridge and to save two outputs of the microcontroller

Module L298N management code

To use the L298N module, you have to activate the H-bridge by applying a 5V voltage on the EN pin. Then, send a PWM signal to the Forward (Input1/4) or Reverse (Input2/3) pin to control the direction and speed of rotation. The forward or reverse direction of rotation depends on the direction of the motor connection.

//Constants
#define nbL298N 2

//Parameters
const int enaL298NPin[2] = {2, 4};
const int fwdL298NPin[2] = {3, 5};
const int bwdL298NPin[2] = {6, 9};

//Variables
int Power = 200;

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init DCmotor
  for (int i = 0; i < nbL298N; i++) {
    pinMode(fwdL298NPin[i], OUTPUT);
    pinMode(bwdL298NPin[i], OUTPUT);
    pinMode(enaL298NPin[i], OUTPUT);
  }
}

void loop() {
  testL298N();
}

void testL298N() { /* function testL298N */
  ////Scenario to test H-Bridge
  for (int i = 0; i < nbL298N; i++) {
    digitalWrite(enaL298NPin[i], HIGH);
    analogWrite(bwdL298NPin[i], 0);
    for (int j = 0; j <= Power; j = j + 10) {
      analogWrite(fwdL298NPin[i], j);
      delay(20);
    }
    for (int j = Power; j >= 0; j = j - 10) {
      analogWrite(fwdL298NPin[i], j);
      delay(20);
    }
    delay(2000);
  }
}

Result

Once the code is uploaded, you should see the motors turning one way and then the other.

Applications

Sources

Retrouvez nos tutoriels et d’autres exemples dans notre générateur automatique de code
La Programmerie

How useful was this post?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

Exit mobile version