Skip to content

Instantly share code, notes, and snippets.

@bhwarren
Last active November 20, 2019 01:34
Show Gist options
  • Save bhwarren/f849f86750aac150ce73d6857d447229 to your computer and use it in GitHub Desktop.
Save bhwarren/f849f86750aac150ce73d6857d447229 to your computer and use it in GitHub Desktop.
rotate the stepper motor attached to the raspberry pi, specifying revolutions in halfstep sequences
import RPi.GPIO as GPIO
import time
import sys
args = sys.argv[1:]
GPIO.setmode(GPIO.BOARD)
controlPins = [7,11,13,15]
for pin in controlPins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstepSeq = [
[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],
[1,0,0,1]
]
reverseHalfstepSeq = [
[1,0,0,1],
[0,0,0,1],
[0,0,1,1],
[0,0,1,0],
[0,1,1,0],
[0,1,0,0],
[1,1,0,0],
[1,0,0,0]
]
def boolify(strInput):
return strInput.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
def rotate(rotations=1, counterClockwise=False):
steps = int(512*rotations)
sequence = reverseHalfstepSeq if counterClockwise else halfstepSeq
for i in range(steps):
for halfstep in range(len(sequence)):
for pin in range(len(controlPins)):
GPIO.output(controlPins[pin], sequence[halfstep][pin])
time.sleep(0.001)
if __name__ == "__main__":
rotations = float(args[0]) if len(args) > 0 else 1
counterClockwise = boolify(args[1]) if len(args) > 1 else False
rotate(rotations, counterClockwise)
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment