Introduction: Interface L298N Using NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @makers_bee & Motivate me on YouTube @MakersBee

Hi Makers,

In this Instructable we go through how to interface an L298N Dual H-Bridge Motor Driver module with an NodeMCU.

So, let's get started.

Step 1: Materials You Need

Hardware Requirements

  • L298N Dual H-Bridge Motor Driver module (various models will work)
  • NodeMCU
  • Jumper wires
  • DC Power Supply (7-35v)
  • DC Motors

Software Requirements

  • Arduino IDE

Step 2: Descritions

Getting to Know Your L298N Dual H-Bridge Motor Controller Module

An H-Bridge is a circuit that can drive a current in either polarity and be controlled by Pulse Width Modulation (PWM).

What is Pulse Width Modulation?

Pulse Width Modulation is a means in controlling the duration of an electronic pulse. Motors will last much longer and be more reliable if controlled through PWM.

Specifications:

  • Double H bridge Driver Chip: L298N
  • Logical voltage: 5V
  • Drive voltage: 5V-35V
  • Logical current: 0-36mA
  • Drive current: 2A (MAX single bridge)
  • Max power: 25W

Note: Built-in 5v power supply, when the driving voltage is 7v-35v. But i recommend you not to power up your microcontroller with on-board 5v power supply.

Step 3: Circuit Diagram

Supply Connections :

+12v - Positive terminal of the battery to be connected.

GND - Ground terminal of the battery to be connected.

+5v - +5v input (unnecessary if your power source is less than +7v to +9v, if the power source is greater than +9v to +35v then it can act as a 5v out)

Input Connections :

(Motor A)

ENA - Enables PWM signal for Motor A

IN1 - Enable Motor A

IN2 - Enable Motor A

(Motor B)

ENB - Enables PWM signal for Motor B

IN3: Enable Motor B

IN4: Enable Motor B

Note :

  • Make sure you have all of your grounds connected together. (NodeMCU, Power Source, and Motor Driver)
  • The PWM Pins are unnecessary if you do not want to control PWM features.

Step 4: Wiring Connections

Check the Circuit Diagram for any confusions.

Step 5: Coding Time

int ENA = 4;

int IN1 = 0;

int IN2 = 2;
void setup() {

// set all the motor control pins to outputs

pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);

}
// this function will run the motors in both directions at a fixed speed

void testOne() {

// turn on motor

digitalWrite(ENA, HIGH); // set speed to 200 out of possible range 0~255
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);

delay(2000); // now change motor directions

digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);

delay(2000); // now turn off motors

digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
// 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 fractions of the maximum speed possible by your hardware

void testTwo() {

// turn on motors

digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);

// accelerate from zero to maximum speed

for (int i = 0; i < 256; i++)

{
    analogWrite(ENA, i);
    delay(50);
}

// decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i)
{
      analogWrite(ENA, i);
      delay(50);
}

// now turn off motors

digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
void loop() {

	testOne();   
	delay(1000);   
  	testTwo();   
  	delay(1000);
}

Download the "L298N_NodeMCU.ino" file and open it up in the Arduino IDE.

Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload. You can tinker with it if you like based on the application, or just use it as it is.

Step 6: OUTPUT

That's all Makers.

You have successfully completed another instructable, It takes pretty less time to create this instructable, and its fun too.

Thank you. Stay tuned for another interesting Instructables.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017