首页 > 解决方案 > 树莓派 28-BYJ-48 带 uln2003 的步进电机控制

问题描述

我目前正在尝试使用 uln2003 驱动器控制 28-BYJ-48 步进电机。当我运行我的代码时,电机嗡嗡作响并且不动。在附加的代码中,我使用 pygame 从控制器接收输入,并最终转动电机。我可以使用演示代码转动电机,但使用新代码时电机只会嗡嗡作响,并在改变方向时改变螺距。(接收输入不是问题,我测试过多次)

下面附上代码:

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

任何帮助都感激不尽。

标签: pythonraspberry-pi3

解决方案


回答了我自己的问题。只是在time.sleep函数中添加了一个,如下图。

        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)

推荐阅读