Overview of Stepper Motor
The Stepper motor is a brushless DC motor that divides the full rotation angle of 360° into a number of equal steps.
The motor is rotated by applying a certain sequence of control signals. The speed of rotation can be changed by changing the rate at which the control signals are applied.
For more information about Stepper Motor, its control sequence, and how to use it, refer to the topic Stepper Motor in the sensors and modules section.
NodeMCU GPIOs can be used to control stepper motor rotation. We can generate a sequence of control signals on the GPIO pins of NodeMCU. To know more about NodeMCU GPIO refer to NodeMCU GPIO with ESPlorer IDE and NodeMCU GPIO with Arduino IDE.
Connection Diagram of Stepper Motor With NodeMCU
Control Stepper Motor using NodeMCU
Let’s rotate a stepper motor in clockwise and counter-clockwise directions alternately.
Here, we are using a six-wire unipolar stepper motor. Only four wires are required to control this stepper motor. The two center-tap wires of the stepper motor are connected to the 5V supply.
ULN2003 driver is used to driving unipolar stepper motor.
Note: To find winding coils and their center tap leads, measure resistance in between the leads. From center leads we will get half the resistance value as compared to the resistance between winding ends.
We can write codes for NodeMCU DevKit in either Lua Script or C/C++ language. We are using ESPlorer IDE for writing code in Lua scripts and Arduino IDE for writing code in C/C++. To know more refer to Getting started with NodeMCU using ESPlorer IDE (which uses Lua scripting for NodeMCU) and Getting started with NodeMCU using Arduino IDE (which uses C/C++ language based Arduino sketches for NodeMCU).
Lua Script for Stepper Motor
wire1 = 8
wire2 = 7
wire3 = 6
wire4 = 5
_delay = 50000 --delay in between two steps. minimum delay more the rotational speed
gpio.mode(wire1, gpio.OUTPUT)--set four wires as output
gpio.mode(wire2, gpio.OUTPUT)
gpio.mode(wire3, gpio.OUTPUT)
gpio.mode(wire4, gpio.OUTPUT)
function sequence(a, b, c, d)--four step sequence to stepper motor
gpio.write(wire1, a)
gpio.write(wire2, b)
gpio.write(wire3, c)
gpio.write(wire4, d)
tmr.delay(_delay)
end
while true do
for i = 1 ,12 do --Rotation in one direction
sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.LOW)
sequence(gpio.HIGH, gpio.HIGH, gpio.LOW, gpio.LOW)
sequence(gpio.LOW, gpio.HIGH, gpio.LOW, gpio.LOW)
sequence(gpio.LOW, gpio.HIGH, gpio.HIGH, gpio.LOW)
sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.LOW)
sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.HIGH)
sequence(gpio.LOW, gpio.LOW, gpio.LOW, gpio.HIGH)
sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.HIGH)
end
sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.LOW)
for i = 1 ,12 do --Rotation in opposite direction
sequence(gpio.LOW, gpio.LOW, gpio.LOW, gpio.HIGH)
sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.HIGH)
sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.LOW)
sequence(gpio.LOW, gpio.HIGH, gpio.HIGH, gpio.LOW)
sequence(gpio.LOW, gpio.HIGH, gpio.LOW, gpio.LOW)
sequence(gpio.HIGH, gpio.HIGH, gpio.LOW, gpio.LOW)
sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.LOW)
sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.HIGH)
end
sequence(gpio.LOW, gpio.LOW, gpio.LOW, gpio.HIGH)
end
Stepper Motor code for NodeMCU using Arduino IDE
uint8_t wire1 = D8;
uint8_t wire2 = D7;
uint8_t wire3 = D6;
uint8_t wire4 = D5;
const uint16_t _delay = 50; /* delay in between two steps. minimum delay more the rotational speed */
void sequence(bool a, bool b, bool c, bool d){ /* four step sequence to stepper motor */
digitalWrite(wire1, a);
digitalWrite(wire2, b);
digitalWrite(wire3, c);
digitalWrite(wire4, d);
delay(_delay);
}
void setup() {
pinMode(wire1, OUTPUT); /* set four wires as output */
pinMode(wire2, OUTPUT);
pinMode(wire3, OUTPUT);
pinMode(wire4, OUTPUT);
}
void loop() {
/* Rotation in one direction */
for(int i = 0; i<12; i++)
{
sequence(HIGH, LOW, LOW, LOW);
sequence(HIGH, HIGH, LOW, LOW);
sequence(LOW, HIGH, LOW, LOW);
sequence(LOW, HIGH, HIGH, LOW);
sequence(LOW, LOW, HIGH, LOW);
sequence(LOW, LOW, HIGH, HIGH);
sequence(LOW, LOW, LOW, HIGH);
sequence(HIGH, LOW, LOW, HIGH);
}
sequence(HIGH, LOW, LOW, LOW);
/* Rotation in opposite direction */
for(int j = 0; j<12; j++)
{
sequence(LOW, LOW, LOW, HIGH);
sequence(LOW, LOW, HIGH, HIGH);
sequence(LOW, LOW, HIGH, LOW);
sequence(LOW, HIGH, HIGH, LOW);
sequence(LOW, HIGH, LOW, LOW);
sequence(HIGH, HIGH, LOW, LOW);
sequence(HIGH, LOW, LOW, LOW);
sequence(HIGH, LOW, LOW, HIGH);
}
sequence(LOW, LOW, LOW, HIGH);
}
Components Used |
||
---|---|---|
ULN2003 Motor Driver ULN2003A is a high-voltage, high-current Darlington transistor array. |
X 1 | |
NodeMCU NodeMCUNodeMCU |
X 1 | |
ESP12F ESP12E |
X 1 |
Downloads |
||
---|---|---|
|
NodeMCU stepper motor source codes | Download |