Stepper Motor Control with L298N Motor Driver and ESP32

In this user guide, we will learn how to control a stepper motor using L298N Motor Driver with ESP32 and Arduino IDE. This is a quick guide where we will learn how to interface an L298N motor driver with ESP32 board and eventually learn how to control bipolar stepper motors in our case NEMA 17 with it. We will show you an Arduino sketch that will control the speed and direction of bipolar stepper motors (NEMA 17) every easily.

We have a similar guide with Arduino and ESP8266 NodeMCU:

Stepper Motors Introduction

Stepper motors are DC brushless and synchronous motors. They rotate in discrete steps of predefined values and are able to rotate both clockwise and anticlockwise. Unlike other DC motors, they provide a precise position control according to the number of steps per revolution for which the motor is designed. That means a complete one revolution of a stepper motor is divided into a discrete number of steps. They are commonly used in CNC machines, Robotics, 2D and 3D printers.

For this guide, we will use NEMA 17 stepper motor to demonstrate bipolar stepper motor control using the L298N motor driver module.

NEMA 17 Stepper Motor

NEMA 17 is a bipolar stepper motor rated at 12V with 200 steps per revolution and 60 rpm speed. In NEMA 17 all pins are connected internally with the coil. To make the movement we need to magnetize the coil. The rotation of the motor requires the magnetic field to make a single step. The time required to make the coil fully magnetic depends on the induction of the coil.

Internally to control the stepper motor we will have to use the green and black pair. The second pair will be of red and blue.

NEMA 23 Stepper motor pinout

The table below shows the different ways to differentiate the wires for our stepper motor. This is important to know when connecting the stepper motor with the L298N Motor Driver outputs. You need to refer to the datasheet of your stepper motor to be sure as different manufacturers use different configurations.

Pin No Name Name Colour
1AA+Black
2CA-Green
3BB+Red
4DB-Blue

Features:

  1. Its rated voltage is 12V
  2. Phase current is 2.2A
  3. The Holding torque is equal to 40N.cm
  4. One step angle will be of 1.8 Deg.
  5. Total steps for each resolution will be 200.
  6. 4-wire and 8-inch lead
  7. Number of phases are 4
  8. Total inductance by each phase will be 2.8 mH
  9. The resistance of the coil is 1.5 Ohm per coil.

L298N Motor Driver Module

The L298N motor driver module is very easy to use with microcontrollers and relatively inexpensive as well. It is widely used in controlling robots as we can connect up to four motors at once but if we want to control the speed and direction as well then it allows two dc motors or a single stepper motor to be connected. Thus, it is perfect for two-wheeled robots. This module is mainly used in robotics and in controlling dc and stepping motors.

The L298N motor driver module consists of an L298N motor driver IC, 78M05 5V regulator, 5V jumper enable, power LED, heat sink, resistors, and capacitors all combined in an integrated circuit. The diagram below shows all the components consisting inside the module.

L298N Motor Driver pic1
L298N Motor Driver Module

The L298N Motor driver IC is powerfully built with a big heat sink. It is a dual-channel H bridge motor driver which can be easily used to drive one single stepper motor.

The module also has a 78M05 5V regulator which is enabled through a jumper. Keeping the jumper intact, means the 5V regulator is enabled. If the motor power supply is less than 12V then we will power the module through the voltage regulator. The 5V pin in this case acts as an output to power the microcontroller. If the power supply is more than 12V, make sure the jumper is not intact and supply 5V power through the pin separately.

Note: If the jumper is connected, do not supply power to both the motor power supply input and the 5V power supply input.

Refer to the in-depth guide about the L298N motor driver with ESP32 by following this link.

Interface L298N DC Motor Driver with ESP32 and NEMA 17 Stepper Motor

We will require the following equipment.

Required Equipment

  1. ESP32 board
  2. L289N Motor driver Module
  3. NEMA 17 stepper motor
  4. External 12 V power supply
  5. Connecting Wires

Assemble the circuit as shown in the connection diagram below.

L298N driver with esp32 and stepper motor connection diagram
ESP32 with L298N Motor Driver and Stepper Motor

For which ever stepper motor you are using refer to its datasheet to find the correct coloured wires to differentiate the A+, A-, B+ and B- wires. This will enable us to properly connect the motor with the OUT1, OUT2, OUT3 and OUT4 pins of the driver module. Connect the stepper motor’s coloured wires with correct output pins of the driver module. In our case, black wire is A+ and green wire is A- hence they will connect with OUT1 and OUT2 respectively. Likewise, the red wire is B+ and the blue wire is B- hence they will connect with OUT3 and OUT4 respectively.

Connect positive terminal of 12V power supply with 12V terminal and negative terminal of power supply with common ground.

We are keeping the 5V Enable jumper in its place as it will power up the L298N motor driver. Then, we have connected the input pins IN1, IN2, IN3 and IN4 with appropriate GPIO pins of ESP32 board. We have used GPIO14, GPIO27, GPIO26 and GPIO25 respectively to connect with each of the input pins of the motor driver.

The enable pins ENA and ENB jumpers are not removed. This will ensure that the stepper motor stays enabled.

L298N driver with esp32 and stepper motor hardware

Arduino Sketch: Controlling Stepper Motor using L298N Motor Driver

Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it.

This basic sketch will show us how to control a stepper motor’s speed and direction of rotation using the L298N motor driver.

#include <Stepper.h>

const int steps_per_rev = 200; //Set to 200 for NIMA 17
#define IN1 14
#define IN2 27
#define IN3 26
#define IN4 25

Stepper motor(steps_per_rev, IN1, IN2, IN3, IN4);


void setup()
{
  motor.setSpeed(60);
  Serial.begin(115200);
}

void loop() 
{
  Serial.println("Rotating Clockwise...");
  motor.step(steps_per_rev);
  delay(500);

  Serial.println("Rotating Anti-clockwise...");
  motor.step(-steps_per_rev);
  delay(500);
}

How the Code Works?

Firstly, we will include the Stepper.h library. This library provides useful functions that make it easy to control the stepper motor.

#include <Stepper.h>

The next step is to define the steps per revolution. This is the number of steps our motor requires to move one complete revolution. If using NEMA 17 set the value to 200. If using 28BYJ-48 instead set the value to 48.

const int steps_per_rev = 200; //Set to 200 for NIMA 17 and set to 48 for 28BYJ-48

Next, we will define the input pins of the motor connections with the ESP32 board. As you can see we have used the GPIO pins 14,27,26 and 25 to connect with IN1, IN2, IN3 and IN4 respectively. However, you can use any other suitable ESP32 GPIO pins as well.

#define IN1 14
#define IN2 27
#define IN3 26
#define IN4 25

Now we will create an instance of the Stepper library called motor() and pass the steps per revolution and the individual motor input pins as arguments. Make sure you specify the input pins in their correct sequence. It is IN1, IN3, IN2, and IN4 for the stepper motor.


Stepper motor(steps_per_rev, IN1, IN2, IN3, IN4);

Inside the setup() function, Serial.begin() is used to establish the serial connection between the development board at a baud rate of 115200. We will use the setSpeed() method on the motor instance and pass the speed of the motor in rpm as an argument inside it. In our case we are setting the stepper motor speed to 60 revolutions per minute.


void setup()
{
  motor.setSpeed(60);
  Serial.begin(115200);
}

loop()

Inside the loop() function, we will first rotate the motor clockwise by using the step() method and passing the steps per revolution as the argument inside it. Hence the motor will rotate at steps of 200 per revolution. Likewise, to rotate the motor anti-clockwise we will pass the steps per revolution with a negative sign inside the step() method. Between the two types of rotations we will have a delay of 0.5 second. Additionally, we will print the type of rotation in the serial monitor as well.

void loop() 
{
  Serial.println("Rotating Clockwise...");
  motor.step(steps_per_rev);
  delay(500);

  Serial.println("Rotating Anti-clockwise...");
  motor.step(-steps_per_rev);
  delay(500);
}

Demonstration

To see the demonstration of the above code, upload the code to ESP32. Before uploading the code, make sure to select ESP32 Dev Module from Tools > Board.

select esp32 board

Also, select the correct COM port to which the board is connected from Tools > Port.

Once the code is uploaded to your board, the motor will start rotating.

The stepper motor will start rotating clockwise and then anti-clockwise repeatedly.

In your Arduino IDE, open up the serial monitor and you will be able to see the status of the motor rotation as well.

Stepper Motor Control with L298N Motor Driver and ESP32

Video Demo:

Further Projects

You may also like to read these projects where we have learned to control stepper motor from a web server.

You may also like to read other stepper motor tutorials:

For more L298N Motor Driver articles:

Leave a Comment