STEPPER MOTOR INTERFACING WITH NODEMCU
Introduction
NodeMCU GPIOs can be used to control stepper motor rotation. We can generate a sequence of control signals on the GPIO pins of NodeMCU.
NodeMCU interface with Stepper Motor
Example
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 centre tap wires of the stepper motor are connected to 5V supply.
ULN2003 driver is used to driving unipolar stepper motor.
Note: To find winding coils and their centre tap leads, measure resistance in between the leads. From centre leads we will get half the resistance value as compared to the resistance between winding ends.
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
Arduino Sketch for Stepper Motor
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);
}