ESP8266 - Stepper Motor
This tutorial instructs you how to use ESP8266 to control Stepper Motor using L298N Driver. In detail, we will learn:
- How to use ESP8266 and L298N driver to control a bipolar stepper motor
- How to program ESP8266 to control the position of the stepper motor
- How to program ESP8266 to control the speed of the stepper motor
- How to program ESP8266 to control the direction of the stepper motor
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.
If you want control another type of stepper motor, please check out this ESP8266 - 28BYJ-48 Stepper Motor tutorial.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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 |
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
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
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:
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
※ 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.
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
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.
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.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!