Master DC Motor Control: ESP8266 NodeMCU and L298N Driver Tutorial

by AKP in Circuits > Arduino

69 Views, 0 Favorites, 0 Comments

Master DC Motor Control: ESP8266 NodeMCU and L298N Driver Tutorial

esp8266-nodemcu-dc-motor-l298n-motor-driver-control-speed-direction.png

Discover how to manage both the direction and velocity of a DC motor utilizing an ESP8266 NodeMCU board alongside the L298N Motor Driver. Initially, we’ll elucidate the functionality of the L298N motor driver. Subsequently, we’ll guide you through the process of regulating the speed and direction of a DC motor employing the L298N motor driver with the ESP8266 programmed through Arduino IDE.

Introducing the L298N Motor Driver

298n-motor-driver.jpg

There are numerous methods to regulate a DC motor. The approach we’re adopting here is suitable for most hobbyist motors, requiring either 6V or 12V to function.

We’ll utilize the L298N motor driver, capable of handling up to 3A at 35V. Moreover, it facilitates the simultaneous operation of two DC motors, making it ideal for constructing robots. If you’re employing a different motor driver, fret not, as most modules function similarly with identical input and output pins.

The pinout of the L298N motor driver is depicted below:

L298N Motor Driver Pinout

L298N-label.jpg

Let’s delve into the pinout of the L298N motor driver and comprehend its operation.

For each motor, the motor driver possesses a two-terminal block on both sides: OUT1 and OUT2 on the left, and OUT3 and OUT4 on the right.

  • OUT1: DC motor A’s positive terminal
  • OUT2: DC motor A’s negative terminal
  • OUT3: DC motor B’s positive terminal
  • OUT4: DC motor B’s negative terminal

At the bottom, a three-terminal block is present with +12V, GND, and +5V. The +12V terminal block is utilized to power the motors, while the +5V terminal powers the L298N chip. However, if the jumper is connected, the chip draws power from the motor’s power supply, obviating the need to supply 5V through the +5V terminal.

Crucially, notwithstanding the name +12V, any voltage between 5V and 35V (preferably within 6V to 12V) can be supplied.

Note: If supplying above 12V, remove the jumper and provide 5V to the +5V terminal.

In this tutorial, we’re employing 4 AA 1.5V batteries yielding roughly 6V combined output, though any suitable power supply may be used. For instance, a bench power supply is suitable for testing this tutorial.

To recap:

  • +12V: Connect the motor’s power supply here.
  • GND: Power supply ground.
  • +5V: Supply 5V if the jumper is removed; otherwise, it acts as a 5V output.
  • Jumper: With the jumper in place, the chip is powered by the motor power supply. Remove the jumper if supplying more than 12V; provide 5V to the +5V terminal.

At the bottom right, four input pins and two enable terminals are present. The input pins regulate the direction of your DC motors, while the enable pins manage their speed.

  • IN1: Input 1 for Motor A
  • IN2: Input 2 for Motor A
  • IN3: Input 1 for Motor B
  • IN4: Input 2 for Motor B
  • EN1: Enable pin for Motor A
  • EN2: Enable pin for Motor B

By default, jumper caps are attached to the enable pins. Remove these caps to control your motors’ speed; otherwise, they’ll either halt or operate at maximum speed.


Controlling a DC Motor With ESP8266 NodeMCU – Managing Speed and Direction

ESP8266-Circuit-DC-Motor-L298N.jpg
4-AA-batteries.jpg
DC-Motor-with-capacitor.jpg

Now that you’ve grasped how the L298N motor driver operates and how to regulate a DC motor, let’s construct a straightforward example sketch to oversee the speed and direction of a single DC motor using the ESP8266.

Wiring a DC Motor to the ESP8266 (L298N Motor Driver)

The motor under our control is linked to the motor A output pins, necessitating the connection of the ENABLEA, INPUT1, and INPUT2 pins of the motor driver to the ESP8266. Refer to the schematic diagram below to wire the DC motor and the L298N motor driver to the ESP8266 NodeMCU board.

Powering the L298N Motor Driver

Due to the substantial surge in current required to set the DC motor in motion, it’s imperative to power the motors via an external power source. For instance, we’re employing 4AA batteries, yet any other suitable power supply suffices. In this setup, a power supply ranging from 6V to 12V is viable.

On the schematic diagram, the inclusion of a switch between the battery holder and the motor driver is discretionary but practical for power control. This obviates the need for constant wire connections and disconnections to conserve power. Additionally, battery holders equipped with a button are available.

We advise soldering a 0.1uF ceramic capacitor to the positive and negative terminals of the DC motor, as delineated in the diagram, to aid in mitigating voltage spikes. (Note: the motors are operational even without the capacitor.)


Code: ESP8266 With a DC Motor – Speed and Direction Control

The following code orchestrates the speed and direction of the DC motor. While this code may not find direct application in practical scenarios, it serves as an excellent illustration for comprehending how to manage the speed and direction of a DC motor using the ESP8266.

// Motor A
int motor1Pin1 = 12;
int motor1Pin2 = 14;
int enable1Pin = 13;

// Setting minimum duty cycle
int dutyCycle = 60;

void setup() {
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);

Serial.begin(115200);

// testing
Serial.print("Testing DC Motor...");
}

void loop() {

//Apply power to spin at maximum speed
digitalWrite(enable1Pin, HIGH);

// Move the DC motor forward at maximum speed
Serial.println("Moving Forward");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
delay(2000);

// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);

// Move DC motor backwards at maximum speed
Serial.println("Moving Backwards");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
delay(2000);

// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);

// Move DC motor forward with increasing speed
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
while (dutyCycle <= 255){
analogWrite(enable1Pin, dutyCycle);
Serial.print("Forward with duty cycle: ");
Serial.println(dutyCycle);
dutyCycle = dutyCycle + 5;
delay(500);
}
dutyCycle = 60;
}



See Full Article Here