Wiring done right

Posted in Posted on 2012-12-24 14:16

Wiring Done Right

With the new jumper leads from eBay I wired it all up again. For the power, the motor controller board has 4 pins and I don’t know why. Two of them are bridged by a jumper and by the other two there is a “-” and a “+” and “5-12V” underneath. I wired the 5V pin of the Pi (P2) to the one labelled “+” and the ground pin of the Pi (P1) to the “-“. Seems to work, but I don’t know if I just got lucky or whether it doesn’t matter which way round you put them.

For the controller lines I wired P18, P22, P24 and P26 to IN1, IN2, IN3 and IN4 respectively. That done, I turned it on and tried some software.

I think I tried the Python code on Matt’s blog first but somehow it didn’t quite work - I don’t remember now. What was great though was that although the motor didn’t seem to turn the LEDs came on so I had got something happening! After a bit of puzzling I realised that the LEDs are wired to the four control lines of the motor so they are very useful in diagnosing what is happening and checking you’ve got the right control sequence.

I spent some time Googling (finding Patrick’s blog), trying to understand the motor’s datasheet, and combining Matt and Patrick’s Python code to make my own version that works for me:

#!/usr/bin/env python
# Based on http://patrickcambria.com/downloads/stepper.py

# This code is written by Stephen C Phillips.
# It is in the public domain, so you can do what you like with it
# but a link to http://scphillips.com would be nice.

from time import sleep
import RPi.GPIO as GPIO
import argparse

parser = argparse.ArgumentParser(description='Stepper motor arguments')
parser.add_argument('-rpm', action='store', dest='rpm', type=float)
parser.add_argument('-revs', action='store', dest='revs', type=int)
args = parser.parse_args()

step_angle = 5.625 / 64
steps_per_rev = int(360 / step_angle)
T = (60 / args.rpm) / steps_per_rev

print "Stepping @ " + `T` + " seconds per step"


Q1 = 18
Q2 = 22
Q3 = 24
Q4 = 26

GPIO.setup(Q1, GPIO.OUT)
GPIO.setup(Q2, GPIO.OUT)
GPIO.setup(Q3, GPIO.OUT)
GPIO.setup(Q4, GPIO.OUT)


# This makes it go anti-clockwise
for i in range(args.revs * steps_per_rev / 8):


Lines 12-15 are just dealing with command line arguments: you can tell it how many times to turn round with -revs and how fast to go with -rpm (revolutions per minute). I found that 10rpm was about as fast as it can go.

Lines 23-38 set the pins up using the pin numbers from the Raspberry Pi’s circuit board (starts 1 bottom left, 2 top left and goes up from there). It sets them to be outputs and sets them to zero (0V I assume).

Lines 41-57 actually encode the sequence of ons and offs given in the motor’s datasheet and loops round it the right number of times. Finally, it sets all the pins to zero again. I found it made the motor turn anti-clockwise, though the datasheet says the sequence is for clockwise rotation - perhaps it is because I plugged the 5V and ground pins in the wrong way round?

I called it spin.py. To run it you have to type sudo python spin.py because you have to have root permissions to access the GPIO port.

Once this worked I started trying to do something more sophisticated but quickly realised that I needed to make a class to hold the state of the motor. More on that next time…


Comments powered by Disqus