Skip to content

Instantly share code, notes, and snippets.

@leoforney
Created April 29, 2023 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoforney/de300d44ef5dccb244b23e4f434ea4c4 to your computer and use it in GitHub Desktop.
Save leoforney/de300d44ef5dccb244b23e4f434ea4c4 to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
import time
# Motor pins (change these according to your connections)
pin_A = 17
pin_B = 27
pin_A_not = 22
pin_B_not = 23
# Setup GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_A, GPIO.OUT)
GPIO.setup(pin_B, GPIO.OUT)
GPIO.setup(pin_A_not, GPIO.OUT)
GPIO.setup(pin_B_not, GPIO.OUT)
# Stepper motor steps
steps = [0, 25, 0, 50, 0, 75, 0, 100]
# Full extend steps
full_extend = 100
# Sequence for the stepper motor
seq = [
[1, 0, 0, 1],
[1, 0, 1, 0],
[0, 1, 1, 0],
[0, 1, 0, 1]
]
# Motor movement function
def move_motor(steps):
for step in range(steps):
for sequence in seq:
set_motor_pins(sequence)
time.sleep(0.001)
def set_motor_pins(sequence):
GPIO.output(pin_A, sequence[0])
GPIO.output(pin_B, sequence[1])
GPIO.output(pin_A_not, sequence[2])
GPIO.output(pin_B_not, sequence[3])
# Main loop
try:
while True:
for step in steps:
move_motor(step * (full_extend // 100))
time.sleep(1)
except KeyboardInterrupt:
print("Interrupted by user")
finally:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment