ESP32 - Stepper Motor

In this tutorial, we are going to learn how to use ESP32 to control the stepper motor:

The tutorial can apply to any kind of bipolar stepper motor (4 wires). The tutorial will take NEMA 17 stepper motor as an example.

ESP32 Stepper motor

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Stepper Motor Nema 17
1×L298N Motor Driver Module
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Stepper Motor

There are two common types of stepper motors:

  • Bipolar: These motors come with 4 wires.
  • Unipolar: Unipolar motors can have either 5 or 6 wires. In the case of a 6-wire unipolar stepper motor, you can effectively use four out of the six wires to control it as if it were a bipolar stepper motor.

If you're specifically interested in a 5-wire unipolar stepper motor, you can refer to the tutorial titled ESP32 - Control 28BYJ-48 Stepper Motor Using ULN2003 Driver.

However, this tutorial is exclusively dedicated to bipolar stepper motors.

Bipolar Stepper Motor pinout

A bipolar stepper motor has four pins, and these pins can be named differently depending on the manufacturer. Below, you'll find a table with some common pin names:

PIN NO Naming 1 Naming 2 Naming 3
1 A+ A A
2 A- A C
3 B+ B B
4 B- B D
Bipolar Stepper Motor pinout

The arrangement of pins, the names of wires, and the colors of wires can differ from one manufacturer to another. To understand how the wire colors correspond to the pin names, it's essential to consult the datasheet or user manual provided by the manufacturer. The image above illustrates the specifications of two distinct motors, each featuring its unique wire naming and wire coloring conventions.

Steps per Revolution

The motor's specification specifies the degree per step (let's call DEG_PER_STEP). Depending on the method of control, the steps per revolution (let's call STEP_PER_REVOLUTION) is calculated as the following table:

Control method Steps per Revolution Real degree per step
Full-step STEP_PER_REVOLUTION = 360 / DEG_PER_STEP DEG_PER_STEP
Half-step STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * 2 DEG_PER_STEP / 2
Micro-step (1/n) STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * n DEG_PER_STEP / n

For example, If the motor's datasheet specifies 1.8 degree/step:

Control method Steps per Revolution Real degree per step
Full-step 200 steps/revolution 1.8°
Half-step 400 steps/revolution 0.9°
Micro-step (1/n) (200 * n) steps/revolution (1.8 / n)°

How to control a stepper motor using ESP32

The ESP32 can produce signals to command the stepper motor, but these signals may lack the necessary voltage and current levels that the stepper motor demands. Hence, an intermediary hardware driver is needed to bridge the gap between the ESP32 and the stepper motor. This driver serves two primary functions:

  • Amplifying Control Signals: It boosts the current and voltage of the control signals originating from the ESP32, ensuring that they meet the requirements of the stepper motor.
  • Protecting the ESP32: Simultaneously, it shields the ESP32 from the higher current and voltage levels used to power the stepper motor, preventing any potential damage.

Various hardware drivers are available for controlling stepper motors, and one commonly used option is the L298N Driver.

Introduction to L298N Driver

A single L298N Driver can be used to control two DC motors or a stepper motor. In this tutorial, we learn how to use it to control the stepper motor.

L298N Driver Pinout

L298N Driver Pinout

L298N Driver Pinout

L298N Driver has 11 pins and three jumpers:

  • VCC pin: supplies power for the motor. It can be anywhere between 5 to 35V.
  • GND pin: is a common ground pin, needs to be connected to GND (0V).
  • 5V pin: supplies power for L298N module. It can be supplied by 5V from ESP32.
  • IN1, IN2, IN3, IN4 pins: are connected to ESP32's pins to receive the control signal to control the stepper motor.
  • OUT1, OUT2, OUT3, OUT4 pins: are connected to the stepper motor.
  • ENA, ENB jumpers: are used to enable stepper motor. You need to keep both the ENA & ENB jumpers in place.
  • 5V-EN jumper: if we keep 5V-EN jumper in place, the power for the L298N module is got from VCC, we do not need to connect anything to the 5V pin. if we remove the 5V-EN jumper, we need to supply power to the L298N module via a 5V pin

As described above, the L298N driver has two input powers:

  • One for stepper motor (VCC and GND pins): from 5 to 35V.
  • One for the L298N module's internal operation (5V and GND pins): from 5 to 7V. if the 5V-EN jumper is kept in place, we do not need to connect this pin to anything.

Wiring Diagram

ESP32 Stepper Motor L298N Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Please keep all three jumpers on the L298N module in place (in case motor's power supply ≤ 12V)
  • Order of the pins on stepper motors may vary between manufacturers. Please check the below table for correct wiring.

Wiring table between ESP32 and L298N Driver

ESP32 pins L298N pins
7 IN1
6 IN2
5 IN3
4 IN4

Wiring table between L298N Driver and Stepper motor

Important!: Please do not care about the wire order of the stepper motor on the above wiring diagram image. It is just an example. The order of the pins on stepper motors may vary between manufacturers. Make sure that your wiring follows the below table.

L298N pins Stepper motor pins Or Or
OUT1 A+ A A
OUT2 A- A C
OUT3 B+ B B
OUT4 B- B D

Before buying a stepper motor, we would recommend you check the check datasheet, specification, or manual of the stepper motor. Make sure that they provide the mapping between the pin's color and name. For example, This stepper motor provides the mapping as below image:

Stepper Motor coil color mapping

Based on that mapping, the wiring table becomes:

L298N pins stepper motor pins wire color
OUT1 A black wire
OUT2 C green wire
OUT3 B red wire
OUT4 D blue wire

※ NOTE THAT:

In all the above wiring tables between the stepper motor and L298N Driver, we can swap OUT1 with OUT2, OUT3 with OUT4. Therefore, there are more ways to do the wiring. However, if we swap them, the motors' rotation direction may be changed (clockwise to anticlockwise, and vice versa).

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How to control a Stepper motor using an L298N driver.

Controlling a stepper motor is not a simple task, especially when we want to control it in a non-blocking manner. Fortunately, thanks to AccelStepper library, controlling the stepper motor becomes a piece of cake.

Arduino IDE also has a built-in Stepper library. However, We do not recommend you to use this library because:

  • The library provides the blocking function. It means it blocks ESP32 from doing other works while it controls the stepper motor.
  • It does not have sufficient functions.

Instead, we recommend you use the AccelStepper library. This library supports:

  • Acceleration
  • Deceleration.
  • Full-step and half-step driving.
  • Multiple simultaneous steppers, with independent concurrent stepping on each stepper.
  • Disadvantage: NOT support micro-step driving

How To Control the Position of Stepper Motor via L298N Driver

We can move the stepper motor to the desired position by using:

stepper.moveTo(desiredPosition); // move motor one revolution

※ NOTE THAT:

The stepper.moveTo() function is non-blocking. This is a great point of the library. However, we have to pay attentions when using this function:

  • Call 'stepper.run()' as frequently as possible. It should be called in the void loop() function.
  • Do NOT use delay() function when motor is moving.
  • Should NOT use Serial.print() and Serial.println() function when motor is moving. These functions make the stepper motor move slower.

How To Control the Speed of Stepper Motor via L298N Driver

We can control not only the speed but also acceleration and deceleration by using some simple functions.

stepper.setAcceleration(50.0); // set acceleration/deceleration stepper.setSpeed(200); // set desired speed

How To Control the Direction of Stepper Motor via L298N Driver

If you do the wiring as above, the motor will rotate in:

  • Clockwise direction: if we control the motor from a position to higher position (Position increment)
  • Anticlockwise direction: if we control the motor from a position to lower position (Position decrement)

For example:

  • If the current position is 100 and we control the motor to 200, the motor rotates in the clockwise direction
  • If the current position is -200 and we control the motor to -100, the motor rotates in the clockwise direction
  • If the current position is 200 and we control the motor to 100, the motor rotates in the anticlockwise direction
  • If the current position is -100 and we control the motor to -200, the motor rotates in the anticlockwise direction

※ NOTE THAT:

As mentioned before, if you swap OUT1 with OUT2, or OUT3 with OUT4, the increment of the position may be anticlockwise and the decrement of the position may be clockwise.

How To Stop Stepper Motor

  • The stepper motor will automatically stop after reaching the desired position.
  • The stepper motor can be forced to stop immediately anytime by using stepper.stop() function.

ESP32 Code - Stepper Motor Code

The below code does:

  • Rotate motor one revolution in the clockwise direction
  • Stop motor 5 seconds
  • Rotate motor back one revolution in the anticlockwise direction
  • Stop motor 5 seconds
  • That process is repeated again and again
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-stepper-motor */ #include <AccelStepper.h> #define DEG_PER_STEP 1.8 #define STEP_PER_REVOLUTION (360 / DEG_PER_STEP) AccelStepper stepper(AccelStepper::FULL4WIRE, 19, 18, 17, 16); // ESP32 pin: GPIO19, GPIO18, GPIO17, GPIO16 long moveToPosition = STEP_PER_REVOLUTION; void setup() { Serial.begin(9600); stepper.setAcceleration(200.0); // set acceleration stepper.setSpeed(200); // set initial speed stepper.setCurrentPosition(0); // set position to 0 stepper.moveTo(STEP_PER_REVOLUTION); // move motor one revolution, in clockwise direction Serial.println("Motor moving in clockwise direction"); } void loop() { if (stepper.distanceToGo() == 0) { Serial.println("Motor is stopped"); delay(5000); // stop 5 seconds stepper.setCurrentPosition(0); // reset position to 0 moveToPosition = -1 * moveToPosition; // reverse direction stepper.moveTo(moveToPosition); // move motor one revolution if (stepper.distanceToGo() > 0) Serial.println("Motor moving in clockwise direction"); else if (stepper.distanceToGo() < 0) Serial.println("Motor moving in anticlockwise direction"); } // Serial.print(F("position: ")); // Serial.println(stepper.currentPosition()); stepper.run(); // MUST be called as frequently as possible }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “AccelStepper”, then find the AccelStepper library by Mike McCauley
  • Click Install button to install AccelStepper library.
ESP32 AccelStepper library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • You will see:
    • Stepper motor rotates one revolution in the clockwise direction
    • Stepper motor stops 5 seconds
    • Stepper motor rotates back one revolution in the anticlockwise direction
    • Stepper motor stops 5 seconds
    • The above process is run repeatedly.
  • See the result on Serial Monitor
COM6
Send
Motor moving in clockwise direction Motor is stopped Motor moving in anticlockwise direction Motor is stopped Motor moving in clockwise direction Motor is stopped Motor moving in anticlockwise direction Motor is stopped Motor moving in clockwise direction Motor is stopped Motor moving in anticlockwise direction Motor is stopped
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES