Reputation: 33
I am currently trying to control the 28-BYJ-48 step motor with a uln2003 driver. When I run my code, the motor buzzes and does not move. In the code attached, I am using pygame to recieve input from a controller, and ultimately turn the motor. I was able to turn the motor with a demo code, but with the new code the motor just buzzes, and changes pitch when the direction is changed. (receiving the input is not the problem, I have tested it multiple times)
Attached below is the code:
control_pins = [7, 8, 11, 9]
for pin in control_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_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]
]
halfstep_ccw = [
[1,0,0,1],
[0,0,0,1],
[0,0,1,1],
[0,0,1,0],
[0,1,1,0],
[0,1,0,0],
[1,1,0,0],
[1,0,0,0]
]
control = controller()
#Note, that to use the driver and the controller at all times,
#They must be in a loop
quit = False
while quit == False:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
quit = True
#This is getting axis 1 to move forward and backward
move_FB = control.get_value(1)
#This is getting axis 2 to move left and right
move_LR = control.get_value(2)
#This is getting the value of the buttons, with axis i
circle = control.get_button_value(13)
R1 = control.get_button_value(11)
L1 = control.get_button_value(10)
#For buttons, 1 is pressed, 0 is not pressed
#This will quit the program
elif circle == 1:
quit = True
#Move the stepper motor clockwise
elif R1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_cw[halfstep][pin])
#Move the stepper motor counter-clockwise
elif L1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_ccw[halfstep][pin])
else:
stop()
GPIO.cleanup()
pygame.QUIT
Any help will be much appreciated.
Upvotes: 1
Views: 1358
Reputation: 49
I use this code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
coil_A_1_pin = 17 # IN1
coil_A_2_pin = 18 # IN2
coil_B_1_pin = 21 # IN3
coil_B_2_pin = 22 # IN4
# adjust if different
StepCount=8
Seq = [[1,0,0,1],
[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]]
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
def setStep(w1, w2, w3, w4):
GPIO.output(coil_A_1_pin, w1)
GPIO.output(coil_A_2_pin, w2)
GPIO.output(coil_B_1_pin, w3)
GPIO.output(coil_B_2_pin, w4)
def forward(delay, steps):
for i in range(steps):
for j in range(StepCount):
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
time.sleep(delay)
def backwards(delay, steps):
for i in range(steps):
for j in reversed(range(StepCount)):
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
time.sleep(delay)
if name == '__main__':
while True:
delay = input("Time Delay (ms)?")
steps = input("How many steps forward? ")
forward(int(delay) / 1000.0, int(steps))
steps = input("How many steps backwards? ")
backwards(int(delay) / 1000.0, int(steps))
Upvotes: 0
Reputation: 33
Answered my own question. just added in a time.sleep function, as shown below.
elif R1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_cw[halfstep][pin])
time.sleep(0.001)
#Move the stepper motor counter-clockwise
elif L1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_ccw[halfstep][pin])
time.sleep(0.001)
Upvotes: 1