Learn how to control the direction and speed of a DC motor using an ESP8266 NodeMCU board and the L298N Motor Driver. We’ll start by explaining how the L298N motor driver works. Then, we’ll show you how to control the speed and direction of a DC motor with the L298N motor driver using the ESP8266 programmed with Arduino IDE.
Note: there are many ways to control a DC motor. We’ll be using the L298N motor driver. This tutorial is also compatible with similar motor driver modules.
Table of Contents
This tutorial covers the following topics:
- Introducing the L298N Motor Driver
- L298N Motor Driver Pinout
- Control DC Motors with the L298N
- Control DC Motor with ESP8266 – Speed and Direction
Are you using an ESP32? Follow this tutorial instead: ESP32 with DC Motor and L298N Motor Driver – Control Speed and Direction.
Prerequisites
Before proceeding, make sure you check the following prerequisites:
ESP8266 with Arduino IDE
We’ll program the ESP8266 using Arduino IDE. So, make sure you have the ESP8266 board installed. Follow the next tutorial if you haven’t already:
Alternatively, you may also want to program the ESP8266 using VS Code and the platformIO extension:
Parts Required
To complete this tutorial you need the following parts:
- ESP8266 Board – read ESP8266 Boards Comparison
- Mini DC motor
- L298N motor driver
- Power source: 4x 1.5 AA batteries + battery holder, or Bench power supply
- 0.1uF ceramic capacitor (optional)
- 1x SPDT slide switch (optional)
- Jumper wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Introducing the L298N Motor Driver
There are several ways to control a DC motor. The method we’ll use here is suitable for most hobbyist motors, that require 6V or 12V to operate.
We’re going to use the L298N motor driver that can handle up to 3A at 35V. Additionally, it allows us to drive two DC motors simultaneously, which is perfect for building a robot. If you’re using a different motor driver, don’t worry, most of those modules work similarly and have the same input and output pins.
The L298N motor driver is shown in the following figure:
L298N Motor Driver pinout
Let’s take a look at the L298N motor driver pinout and see how it works.
The motor driver has a two-terminal block on each side for each motor. OUT1 and OUT2 at the left and OUT3 and OUT4 at the right.
- OUT1: DC motor A + terminal
- OUT2: DC motor A – terminal
- OUT3: DC motor B + terminal
- OUT4: DC motor B – terminal
At the bottom, you have a three-terminal block with +12V, GND, and +5V. The +12V terminal block is used to power up the motors. The +5V terminal is used to power up the L298N chip. However, if the jumper is in place, the chip is powered using the motor’s power supply and you don’t need to supply 5V through the +5V terminal.
Important: despite the +12V terminal name, you can supply any voltage between 5V and 35V (but 6V to 12V is the recommended range).
Note: if you supply more than 12V, you need to remove the jumper and supply 5V to the +5V terminal.
In this tutorial, we’ll be using 4 AA 1.5V batteries that combined output approximately 6V, but you can use any other suitable power supply. For example, you can use a bench power supply to test this tutorial.
In summary:
- +12V: The +12V terminal is where you should connect the motor’s power supply
- GND: power supply GND
- +5V: provide 5V if the jumper is removed. Acts as a 5V output if the jumper is in place
- Jumper: jumper in place – uses the motor power supply to power up the chip. Jumper removed: you need to provide 5V to the +5V terminal. If you supply more than 12V, you should remove the jumper
At the bottom right you have four input pins and two enable terminals. The input pins are used to control the direction of your DC motors, and the enable pins are used to control the speed of each motor.
- 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
There are jumper caps on the enable pins by default. You need to remove those jumper caps to control the speed of your motors. Otherwise, they will either be stopped or spinning at the maximum speed.
Control DC motors with the L298N Motor Driver
Now that you understand how the motor driver works, let’s see how to use it to control a DC motor.
Enable pins
The enable pins are like an ON and OFF switch for the motors. For example:
- If you send a HIGH signal to the enable 1 pin, motor A is ready to be controlled and at the maximum speed;
- If you send a LOW signal to the enable 1 pin, motor A turns off;
- If you send a PWM signal, you can control the speed of the motor. The motor speed is proportional to the duty cycle. However, note that for small duty cycles, the motors might not spin, and make a continuous buzz sound.
SIGNAL ON THE ENABLE PIN | MOTOR STATE |
---|---|
HIGH | Motor enabled |
LOW | Motor not enabled |
PWM | Motor enabled: speed proportional to the duty cycle |
Input pins
The input pins control the direction the motors are spinning. Input 1 and input 2 control motor A, and input 3 and 4 control motor B.
- If you apply LOW to input1 and HIGH to input 2, the motor will spin forward;
- If you apply power the other way around: HIGH to input 1 and LOW to input 2, the motor will rotate backwards. Motor B can be controlled using the same method but applying HIGH or LOW to input 3 and input 4.
For example, for motor A, this is the logic:
Direction | Input 1 | Input 2 | Enable 1 |
Forward | 0 | 1 | 1 |
Backwards | 1 | 0 | 1 |
Stop | 0 | 0 | 0 |
Controlling 2 DC Motors – ideal to build a robot
If you want to build a robot car using 2 DC motors, these should be rotating in specific directions to make the robot go left, right, forward, or backward.
For example, if you want your robot to move forward, both motors should be rotating forward. To make it go backward, both should be rotating backward.
To turn the robot in one direction, you need to spin the opposite motor faster. For example, to make the robot turn right, enable the motor at the left, and disable the motor at the right. The following table shows the input pins’ state combinations for the robot directions.
DIRECTION | INPUT 1 | INPUT 2 | INPUT 3 | INPUT 4 |
---|---|---|---|---|
Forward | 0 | 1 | 0 | 1 |
Backward | 1 | 0 | 1 | 0 |
Right | 0 | 1 | 0 | 0 |
Left | 0 | 0 | 0 | 1 |
Stop | 0 | 0 | 0 | 0 |
Recommended reading: Build Robot Car Chassis Kit for ESP32, ESP8266, Arduino, etc…
Control DC Motor with ESP8266 NodeMCU – Speed and Direction
Now that you know how an L298N motor driver works and how to control a DC motor, let’s build a simple example sketch to control the speed and direction of one DC motor with the ESP8266.
Wiring a DC Motor to the ESP8266 (L298N Motor Driver)
The motor we’ll control is connected to the motor A output pins, so we need to wire the ENABLEA, INPUT1, and INPUT2 pins of the motor driver to the ESP8266. Follow the next schematic diagram to wire the DC motor and the L298N motor driver to the ESP8266 NodeMCU board.
We’re using the GPIOs in the following table to connect to the motor driver. You can use any other suitable GPIOs as long as you modify the code accordingly.
LN298N Motor Driver | Input 1 | Input 2 | Enable | GND |
ESP8266 | GPIO 12 (D6) | GPIO 14 (D5) | GPIO 13 (D7) | GND |
Recommended reading: ESP8266 Pinout Reference: Which GPIO pins should you use?
Powering the LN298N Motor Driver
The DC motor requires a big jump in current to move, so the motors should be powered using an external power source. As an example, we’re using 4AA batteries, but you can use any other suitable power supply. In this configuration, you can use a power supply in the range of 6V to 12V.
On the schematic diagram, the switch between the battery holder and the motor driver is optional, but it’s handy to cut and apply power. This way you don’t need to constantly connect and then disconnect the wires to save power. There are also battery holders that already come with a button
We recommend soldering a 0.1uF ceramic capacitor to the positive and negative terminals of the DC motor, as shown in the diagram to help smooth out any voltage spikes. (Note: the motors also work without the capacitor.)
Code: ESP8266 with a DC Motor – Control Speed and Direction
The following code controls the speed and direction of the DC motor. This code might not have a practical application in the real world but is a great example to understand how to control the speed and direction of a DC motor with the ESP8266.
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-dc-motor-l298n-motor-driver-control-speed-direction/
*********/
// 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;
}
Upload the code to your ESP8266. Make sure you have the right board and COM port selected.
Continue reading to learn how the code works or skip to the demonstration section.
Declaring motor pins
First, you define the GPIOs connected to the motor driver. In this case, Input 1 for motor A is connected to GPIO 12, Input 2 to GPIO 14, and the Enable pin to GPIO 13.
int motor1Pin1 = 12;
int motor1Pin2 = 14;
int enable1Pin = 13;
Setting minimum duty cycle
We’ll control the speed of the motor by adjusting the duty cycle of the PWM signal. We’ll set the minimum duty cycle to 60 because for smaller values the motor won’t move and will make a weird buzz sound.
int dutyCycle = 60;
setup()
In the setup(), you start by setting the motor pins as outputs.
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
We initialize the Serial Monitor for debugging purposes:
Serial.begin(115200);
Moving the DC motor forward
In the loop() is where the motor moves. The code is well commented on what each part of the code does.
To supply power to the motors, and move them at the maximum speed, we need to set the enable pin to HIGH.
digitalWrite(enable1Pin, HIGH);
To move the motor forward, you set input 1 pin to LOW and input 2 pint to HIGH. In this example, the motor speeds forward for 2 seconds (2000 milliseconds).
// Move the DC motor forward at maximum speed
Serial.println("Moving Forward");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
delay(2000);
Moving the DC motor backwards
To move the DC motor backwards you apply power to the motor input pins the other way around. HIGH to input 1 and LOW to input 2.
// Move DC motor backwards at maximum speed
Serial.println("Moving Backwards");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
delay(2000);
Stop the DC motor
To make the DC motor stop, you can either set the enable pin to LOW, or set both input 1 and input 2 pins to LOW. In this example, we’re setting both input pins to LOW.
// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);
Controlling the DC motor speed
To control the DC motor speed, we need to change the PWM signal duty cycle. For that, you use the analogWrite() function that accepts as arguments the GPIO pin and the duty cycle, as follows.
analogWrite(enable1Pin, dutyCycle);
Recommended reading: ESP8266 NodeMCU PWM with Arduino IDE (Analog Output)
In our example, we have a while loop that increases the duty cycle by 5 in every loop.
// 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);
}
When the while condition is no longer true, we set the duty cycle to 60 again.
dutyCycle = 60;
Demonstration
After uploading the code to the ESP8266, apply power to the motor driver.
The motor will start spinning in different directions and speeds.
Wrapping Up
In this tutorial,l we’ve shown you how to control the direction and speed of a DC motor using an ESP8266 and the L298N motor driver. In summary:
- To control the direction the DC motor is spinning you use the input 1 and input 2 pins;
- Apply LOW to input 1 and HIGH to input 2 to spin the motor forward. Apply power the other way around to make it spin backward;
- To control the speed of the DC motor, you send a PWM signal to the enable pin. The speed of the DC motor is proportional to the duty cycle.
We hope you’ve found this tutorial useful.
Learn more about the ESP8266 with our resources:
- Home Automation Using ESP8266
- Build Web Servers with ESP32 and ESP8266 (eBook)
- Firebase Web App with ESP32 and ESP8266 (eBook)
- SMART HOME with Raspberry Pi, ESP32, and ESP8266
- Free ESP32 Projects and Tutorials
Thanks for reading.
Allow me just a general commengt here that is not specifically related to this article: You two have really helped me to advance in a couple of areas that I had little or no experience in. I had dabbled in for instance firebase before, but I think that without you I woud not have brought that forward. Same can be said for integrating more JavaScript in my webserverprojects and without you I would never have used highcharts or fontawesome. All extremely interesting articles. Lately your articles are more in basic projects and maybe not as interesting for me, but that doesnt change the appreciation that I have for the work you have been doing and I am sure they are interesting for a large group of beginners. Ofcourse I will keep following your site, just in case something pops up again that is more up my alley. Thanks for yr work
Thanks for your feedback.
We try to integrate both beginner and more advanced projects.
Stay tuned.
Regards,
Sara