简体   繁体   中英

Stepper Motor with Raspberry Pi only works with some delay times with GPIOZero

I am experimenting with connecting Stepper Motor 28BYJ-48 with ULN2003 Driver board to Raspberry Pi Model 3 B using Python and GPIOZero. I hacked together some code that works, using a few different examples I found, and I can now get the motor to turn in either direction. There is a time delay problem, however. When turning counterclockwise, the code works using any delay time from 2 milliseconds all the way out to 50. In the clockwise direction however, it ONLY works at 2 and 3 millisecond delay, but no others. Very weird.

Code looks like this (very rough - just trying to prove the concept):

import time
import sys
from gpiozero import OutputDevice as stepper

def moveBackward(stepCounter):

  while True:
      stepDir = -1
      for pin in range(0,4):
         xPin=stepPins[pin]          # Get GPIO
         if seq[stepCounter][pin]!=0:
            xPin.on()
         else:
            xPin.off()

      stepCounter += stepDir
      print(stepCounter) 
      if (stepCounter < 0):
          stepCounter = stepCount+stepDir 

      time.sleep(waitTime)     # Wait before moving onimport time 

def moveForward(stepCounter):
  while True:
      stepDir = 1
      for pin in range(0,4):
          xPin=stepPins[pin]          # Get GPIO
          if seq[stepCounter][pin]!=0:
             xPin.on()
          else:
             xPin.off()

      stepCounter += stepDir
      if (stepCounter >= stepCount):
          stepCounter = 0   

      time.sleep(waitTime) 


IN1 = stepper(17)
IN2 = stepper(22)
IN3 = stepper(23)
IN4 = stepper(24)

stepPins = [IN1,IN2,IN3,IN4] # Motor GPIO pins</p><p>
stepDir = 1        # Set to 1 for clockwise
                       # Set to -1 for anti-clockwise
mode = 0            # mode = 1: Low Speed ==> Higher Power
                       # mode = 0: High Speed ==> Lower Power

if mode:              # Low Speed ==> High Power
  seq = [[1,0,0,1], # Define step sequence as shown in manufacturers datasheet
         [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]]
else:                    # High Speed ==> Low Power 
  seq = [[1,0,0,0], # Define step sequence as shown in manufacturers datasheet
         [0,1,0,0],
         [0,0,1,0],
         [0,0,0,1]]
stepCount = len(seq)


if len(sys.argv)>1: # Read wait time from command line
   waitTime = int(sys.argv[1])/float(1000)
   print(waitTime)
else:
   waitTime = 0.004    # 2 miliseconds was the maximun speed got on my tests
stepCounter = 0



while True:                          # Start main loop

  moveBackward(stepCounter)  # counterclockwise
  #moveForward(stepCounter)  # clockwise

So I found this code which works nicely - not sure what the problem above is caused by. https://github.com/custom-build-robots/Stepper-motor-28BYJ-48-Raspberry-Pi/commit/d8f6b5f3ee4d6b22d3fe40d81d0be09287c97c89

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM