##
# Stepper motor driver for 28BYJ-48 and small controller board
#
from gpiozero import DigitalOutputDevice as stepper
from time import sleep
from random import choice, randint
# key is step, tuples are stepper motor lines 1-4
# 8 half step per one revolution of internal motor shaft
# gear ration is ~64:1 therefore 4096 step per one revolution of motor
# Define the GPIO pis to use
IN1 = stepper(6)
IN2 = stepper(13)
IN3 = stepper(19)
IN4 = stepper(26)
step_pins = [IN1, IN2, IN3, IN4]
# Either 8 half steps (half) or 4 full steps (full)
speed = 'full'
# Sequence for the pins
if speed == 'half':
SEQUENCE = [
[1, 0, 0, 1],
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]
]
else:
SEQUENCE = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]
def oneStep(step_counter):
"""
Send motor one step sequence
"""
for pin in range(0, 4):
gpio_pin = step_pins[pin]
if SEQUENCE[step_counter][pin] != 0:
gpio_pin.on()
else:
gpio_pin.off()
def main(number_of_steps, direction, delay):
step_count = len(SEQUENCE)
sequence_count = 0
for i in range(number_of_steps + 1):
oneStep(sequence_count)
sequence_count += direction
# Start sequence again
if (sequence_count >= step_count):
sequence_count = 0
if (sequence_count < 0):
sequence_count = step_count + direction
# 1.4ms is minimum
sleep(0.002 + delay)
if __name__ == '__main__':
# Set the speed
delay = 0.001
# Set to 1 for clockwise, -1 for anti-clockwise
direction = 1
# ~4096 for one revolution
no_of_steps = 4096
# Move stepper motor randomly
while True:
dir = choice(1, -1)
main(randint(0, 4096), dir, delay)
sleep(0.3)