Raspberry Pi Stepper Motor

      No Comments on Raspberry Pi Stepper Motor

In preparation for the pi class I wanted to get a motor running and found this excellent tutorial:

http://www.raspberrypi-spy.co.uk/2012/07/stepper-motor-control-in-python/

It works great once I realized the motor has a 64:1 gear reduction and  you need kilo-steps to see it moving.  Also while the code was very simple it looked it was written in C.  I decided to make it more pythonic:

#!/usr/bin/python
# ___ ___ _ ____
# / _ \/ _ \(_) __/__ __ __
# / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/ /_/___/ .__/\_, /
# /_/ /___/
#
# Stepper Motor Test
#
# A simple script to control
# a stepper motor.
#
# Author : Matt Hawkins
# Date : 11/07/2012
#  Changes to be more pythoning  8 November 2014
#
# http://www.raspberrypi-spy.co.uk/
#
# Import required libraries
import time
import RPi.GPIO as GPIO
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
 
# Define GPIO signals to use
# Pins 18,22,24,26
# GPIO24,GPIO25,GPIO8,GPIO7
StepPins = [24,25,8,7]
 
# Set all pins as output
for pin in StepPins:
# print "Setup pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
 
# Define some settings
StepCounter = 0
WaitTime = 0.020
# Define simple sequence
Seq1 = [[1,0,0,0],
        [0,1,0,0],
        [0,0,1,0],
        [0,0,0,1]]
 
# Define advanced sequence
# as shown in manufacturers datasheet
 
Seq2 = [[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]]
# Choose a sequence to use
Seq = Seq1
while True :
    StepCounter = 0
    for step in Seq :
        for pin,phase in zip(StepPins,step) :
 
            if phase :
                # print " Step %d Enable %i" %(StepCounter,pin)
                GPIO.output(pin, True)
            else:
                GPIO.output(pin, False)
 
            StepCounter += 1
 
            # Wait before moving on
            time.sleep(WaitTime)

Leave a Reply