IoT/ESP32 B.Tech/Diploma Beginner

ESP32 with DC Motor and L298N Motor Driver – Control Speed and Direction

Learn how to control the direction and speed of a DC motor using an ESP32 and the L298N Motor Driver. Learn how the L298N motor driver works, and how to use it to control the direction and speed of a DC motor using the ESP32 and Arduino IDE.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

This tutorial shows how to control the direction and speed of a DC motor using an ESP32 and the L298N Motor Driver. First, we’ll take a quick look at how the L298N motor driver works. Then, we’ll show you an example of how to control the speed and direction of a DC motor with the L298N motor driver using the ESP32 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 ESP32 – Speed and Direction
Wiring a DC Motor to the ESP32 (L298N Motor Driver)
Code: ESP32 with a DC Motor – Control Speed and Direction
Are you using an ESP8266?
ESP8266 NodeMCU with DC Motor and L298N Motor Driver – Control Speed and Direction (Arduino IDE)
Prerequisites
Before proceeding, make sure you check the following prerequisites:
ESP32 with Arduino IDE
We’ll program the ESP32 using Arduino IDE. So, make sure you have the ESP32 add-on installed. Follow the next tutorial if you haven’t already:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
Alternatively, you may also want to program the ESP32 using VS Code and the platformIO extension:
Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266
Parts Required
To complete this tutorial you need the following parts:
ESP32 DOIT DEVKIT V1 Board
–
read ESP32 Development Boards Review and Comparison
Mini DC motor
L298N motor driver
Power source: 4x 1.5 AA batteries or
Bench power supply
100nF 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.
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 use 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 jumper is removed. Acts as a 5V output if 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’re familiar with the L298N Motor Driver, let’s see how to use it to control your DC motors.
Enable pins
The enable pins are like an ON and OFF switch for your 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
Backwards
Stop
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
Backward
Right
Left
Stop
Recommended reading:
Build Robot Car Chassis Kit for ESP32, ESP8266, Arduino, etc…
Control DC Motor with ESP32 – Speed and Direction
Now that you know how to control a DC motor with the L298N motor driver, let’s build a simple example to control the speed and direction of one DC motor.
Wiring a DC Motor to the ESP32 (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 ESP32. Follow the next schematic diagram to wire the DC motor and the L298N motor driver to the ESP32.
LN298N Motor Driver
Input 1
Input 2
Enable
GND
ESP32
GPIO 27
GPIO 26
GPIO 14
GND
We’re using the GPIOs on the previous table to connect to the motor driver. You can use any other suitable GPIOs as long as you modify the code accordingly. Learn more about the ESP32 GPIOs:
ESP32 Pinout Reference Guide
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 from the ESP32. 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 with 6V to 12V.
The switch between the battery holder and the motor driver is optional, but it is very handy to cut and apply power. This way you don’t need to constantly connect and then disconnect the wires to save power.
We recommend soldering a 0.1uF ceramic capacitor to the

Technologies used

ESP32ESP8266ArduinoBLEL298NSPIPWMArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • L298N
  • Motor Driver

Software & tools

  • Arduino IDE
  • VS Code
  • PlatformIO
  • PlatformIO

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.