Wednesday, October 12, 2016

106: EasyDriver + Linear Stepper Motor
-- Finally --

I've long lost track of how many failed attempts I've made to get this to work. Here's my successful EasyDriver wiring:
The messy setup

Here's some Python code that works for me.
Note: I had to install PWM -- see

import RPi.GPIO as GPIO
import time, sys
STEP = 22
DIR = 17

sp = GPIO.PWM(STEP, 800)# 800 seems faster than 500 ???

# usage: sudo python direction steps
dir = sys.argv[1]        # f or b -- forward or back
steps = int(sys.argv[2]) # max on my linear screw about 700

def spin(dir, steps):
  GPIO.output(DIR, dir)
  while steps > 0:
    time.sleep(0.005) # smaller delay looses steps ???
    steps -= 1
  return True

if dir == "f":  # i.e., "forward" away from the motor 
  dir = False
  dir = True
spin(dir, steps)
print "Done"

More about the linear motor, see:, post 46.

Not represented in the above code: I added a relay switch for the 5v power to the motor. Otherwise steppers draw current constantly (and the motor housing can get hot). Too bad that the EasyDriver doesn't work like the ULN2003 -- which allows you to turn the 4 motor leads off without an added switch. Re relays: I got 5 single-relay devices on the cheap from Amazon; they lack opto-isolation and have no mounting holes -- but cheap.

No comments:

Post a Comment