ESP8266 - Stepper Motor

This tutorial instructs you how to use ESP8266 to control Stepper Motor using L298N Driver. In detail, we will learn:

The tutorial is applicable to all types of bipolar stepper motors with four wires. It will use a NEMA 17 stepper motor as an example.

ESP8266 NodeMCU Stepper motor L298N Driver

If you want control another type of stepper motor, please check out this ESP8266 - 28BYJ-48 Stepper Motor tutorial.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Stepper Motor Nema 17
1×L298N Motor Driver Module
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Stepper Motor

Two common types of stepper motors exist:

  • bipolar: this motor has 4 wires
  • unipolar: this motor has either 5 or 6 wires

For a 6-wire unipolar stepper motor, we can utilize four of its six wires and control it as if it were a bipolar stepper motor.

For 5-wire unipolar stepper motors, refer to ESP8266 - control 28BYJ-48 stepper motor using ULN2003 driver.

The emphasis of this tutorial is solely on the bipolar stepper motor.

Bipolar Stepper Motor pinout

The pinout of the bipolar Stepper Motor has 4 pins. Different manufacturers use various names for the motor's pins. The following table provides some of the more commonly used pin-naming:

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 sequence of pins, the labeling of wires, and the colors of the wires may differ between manufacturers. It is necessary to read the datasheet or manual to understand the correlation between the wire color and the pin name. The image above also displays the details of two distinct motors with dissimilar wire labeling and wire coloring.

Steps per Revolution

The specification of the motor indicates the DEG_PER_STEP. Depending on the type of control, STEP_PER_REVOLUTION can be determined using the table below:

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 instance, if the motor's datasheet states that it has 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 ESP8266

ESP8266 can produce signals to operate the stepper motor. However, the signals from ESP8266 lack the necessary voltage and/or current that the stepper motor needs. Consequently, a hardware driver is required between ESP8266 and the stepper motor. This driver has two functions:

  • Increase the control signals from ESP8266 (current and voltage)
  • Shield ESP8266 from the high current and voltage used to power the stepper motor.

There are various types of hardware drivers that can be used to manage stepper motors. One of the most commonly used hardware drivers for controlling stepper motors is the L298N Driver.

Overview of L298N Driver

A L298N Driver can be utilized to control two DC motors or a stepper motor. This tutorial instructs you how to use it to control the stepper motor.

L298N Driver Pinout

L298N Driver pinout

The L298N Driver has 11 pins and three jumpers:

  • VCC pin: This supplies power for the motor and can be anywhere between 5 to 35V.
  • GND pin: This is a common ground pin and needs to be connected to GND (0V).
  • 5V pin: This supplies power for the L298N module and can be supplied by 5V from ESP8266.
  • IN1, IN2, IN3, IN4 pins: These are connected to ESP8266's pins to receive the control signal to control the stepper motor.
  • OUT1, OUT2, OUT3, OUT4 pins: These are connected to the stepper motor.
  • ENA, ENB jumpers: These are used to enable the stepper motor and both the ENA & ENB jumpers need to be kept in place.
  • 5V-EN jumper: If this is kept in place, the power for the L298N module is got from VCC and nothing needs to be connected to the 5V pin. If it is removed, power needs to be supplied to the L298N module via a 5V pin.

The L298N driver has two input powers:

  • One for the stepper motor (VCC and GND pins): from 5 to 35 Volts.
  • One for the internal operation of the L298N module (5V and GND pins): from 5 to 7 Volts. If the 5V-EN jumper is left in place, no need this power.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Stepper Motor L298N Driver

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

※ NOTE THAT:

  • Leave the three jumpers on the L298N module in place if the motor's power supply is 12V or less.
  • The arrangement of pins on stepper motors may differ between manufacturers. Refer to the table below to ensure the correct wiring.

Wiring table between ESP8266 and L298N Driver

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

Wiring table between L298N Driver and Stepper motor

Important!: Pay no attention to the order of the wires in the stepper motor on the wiring diagram image. It is just an illustration. The arrangement of the pins on stepper motors may differ depending on the manufacturer. Make sure your wiring follows the table below.

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

Prior to purchasing a stepper motor, we suggest that you review the datasheet, specification, or manual of the stepper motor. Ensure that it includes the correlation between the pin's color and name. For instance, this stepper motor supplies the mapping as seen in the following image:

Stepper Motor coil color mapping

Using the mapping, the wiring table is:

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 the wiring tables presented above, OUT1 and OUT2, as well as OUT3 and OUT4, can be interchanged. This increases the number of possible wiring configurations. However, this may also cause the motor to change its direction of rotation (clockwise to anticlockwise, or vice versa).

How to control a Stepper motor using an L298N driver.

Controlling a stepper motor can be a challenging endeavor, particularly when we need to do so without interruption. Fortunately, the AccelStepper library makes it a breeze.

The Arduino IDE includes a built-in Stepper library. However, we do not suggest utilizing this library as:

  • It contains a blocking function, which prevents the ESP8266 from performing other tasks while controlling the stepper motor.
  • It lacks sufficient features.

Instead, we suggest you utilize the AccelStepper library. This library provides:

  • Acceleration
  • Deceleration
  • Full-step and half-step driving
  • The ability to control multiple steppers at once, with each stepper able to move independently
  • Disadvantage: Does not support micro-step driving

How To Control the Position of Stepper Motor via L298N Driver

We can achieve the desired position of the stepper motor through: . 1. Setting the current position as the reference point . 1. Establishing the reference point as the current position

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

※ NOTE THAT:

The stepper.moveTo() function is non-blocking, which is a great advantage of the library. However, when using it, we must be careful:

  • Ensure that 'stepper.run()' is called as often as possible, preferably within the void loop() function.
  • Avoid using the delay() function while the motor is in motion.
  • Do not use Serial.print() or Serial.println() functions while the motor is running, as this will slow down its movement.

How To Control the Speed of Stepper Motor via L298N Driver

We can regulate both speed and acceleration/deceleration through the utilization of a few straightforward 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 wire the motor as indicated, it will rotate in a:

  • Clockwise direction: when the motor is moved from a lower position to a higher position (Position increment)
  • Anticlockwise direction: when the motor is shifted from a higher position to a lower position (Position decrement)
  • If the current position is 100, and we command the motor to 200, it will rotate in a clockwise direction.
  • When the current position is -200 and the motor is instructed to -100, it will rotate in a clockwise direction.
  • If the current position is 200 and the motor is commanded to 100, it will rotate in an anticlockwise direction.
  • If the current position is -100 and the motor is instructed to -200, it will rotate in an anticlockwise direction.

※ NOTE THAT:

As previously noted, if you exchange OUT1 with OUT2, or OUT3 with OUT4, the increase in the position may be counterclockwise and the decrease in the position may be clockwise.

How To Stop Stepper Motor

  • The stepper motor will come to a halt when it reaches the desired position.
  • If an immediate stop is desired, the stepper.stop() function can be used.

ESP8266 Code - Stepper Motor Code

The code below:

  • Makes the motor rotate one revolution in a clockwise direction
  • Pauses the motor for 5 seconds
  • Causes the motor to rotate one revolution in an anticlockwise direction
  • Halts the motor for 5 seconds
  • Repeats this process continually
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-stepper-motor */ #include <AccelStepper.h> #define DEG_PER_STEP 1.8 #define STEP_PER_REVOLUTION (360 / DEG_PER_STEP) AccelStepper stepper(AccelStepper::FULL4WIRE, D1, D2, D5, D6); 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 }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “AccelStepper”, then locate the AccelStepper library created by Mike McCauley.
  • Click the Install button to install the AccelStepper library.
ESP8266 NodeMCU AccelStepper library

Copy the code and open it with the Arduino IDE.

Click the Upload button on the Arduino IDE to upload the code to the ESP8266.

You will observe that the stepper motor rotates one revolution in the clockwise direction, pauses for 5 seconds, then rotates back one revolution in the anticlockwise direction and pauses again for 5 seconds. This process is repeated continuously.

Check the results on the 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

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!