Sunday, October 9, 2016

105: RPi Stepper Motor
-- revised sleep time --

After much pain (read: time wasted) I have a half-assed program that runs the cheapo 28BYJ-48 motor (5V DC with ULN2003 Driver Board).

Here's some Python:


import RPi.GPIO as pin, time, sys

pin.setmode(pin.BCM)

Cpin = [6,13,19,26] # to UNL2003 in1, in2, in3, in4; external 5v/GND

pin.setup(6,pin.OUT)
pin.setup(13,pin.OUT)
pin.setup(19,pin.OUT)
pin.setup(26,pin.OUT)

# rotate clockwise
Cw = [[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]]
# counterclockwise
Ccw = [[0,0,0,1],[0,0,1,1],[0,0,1,0],[0,1,1,0],[0,1,0,0],[1,1,0,0],[0,0,0,1],[1,0,0,1]]

def motorOff(): # so it stops drawing current
pin.output(Cpin[0], 0)
pin.output(Cpin[1], 0)
pin.output(Cpin[2], 0)
pin.output(Cpin[3], 0)

def moveCw(steps):
for i in range(steps):
for j in range(8):
for k in range(4):
pin.output(Cpin[k], Cw[j][k])
time.sleep(0.002) ### Changed! .001 caused missed steps

def moveCcw(steps):
for i in range(steps):
for j in range(8):
for k in range(4):
pin.output(Cpin[k], Ccw[j][k])
time.sleep(0.002)
s = int(sys.argv[1])
print s

# Full rotation approx. 128.3. How do you get an exact number?

print "cw"
moveCw(s)
time.sleep(2)

print "ccw"
moveCcw(s)

motorOff()

pin.cleanup()

The following bits also work for counterclockwise. But which (if either) is better?
[[1,0,0,1],[0,0,0,1],[0,0,1,0],[0,1,1,0],[0,1,0,0],[1,1,0,0],[0,0,0,1],[1,0,0,0]]

I spent even more time trying to get a linear stepper motor with the (so-called) EasyDriver to work. No luck. I have 2 sets of this stuff and both work with an Arduino. See http://dicks-photon-arduino.blogspot.com/, posts 43 and 46.

No comments:

Post a Comment