树莓派驱动步进式电机python编程实现

步进电机是一种将电脉冲转化为角位移的执行机构。通俗一点讲:当步进驱动器接收到一个脉冲信号,它就驱动步进电机按设定的方向转动一个固定的角度(即步进角)。

所需材料:

1.母对母杜邦线:至少6

2.ULN2003 驱动板1

3.28BYJ-48-5V 减速步进电机1个。

接线:

IN1GPIO04

IN2GPIO17

IN3GPIO23

IN4GPIO24

VCC5v

GNDGround


python 编码实现

import RPi.GPIO as GPIO
import time

delay=2 #delay 2ms

pin_4 = 4
pin_17 = 17
pin_23 = 23
pin_24 = 24

GPIO.setmode(GPIO.BCM) #设置引脚的编码方式
	
def init():
    GPIO.setwarnings(False)
    GPIO.setup(pin_4, GPIO.OUT)
    GPIO.setup(pin_17, GPIO.OUT)
    GPIO.setup(pin_23, GPIO.OUT)
    GPIO.setup(pin_24, GPIO.OUT)

 
def forward(delay):  
    setStep(1, 0, 0, 0)
    time.sleep(delay)
    setStep(0, 1, 0, 0)
    time.sleep(delay)
    setStep(0, 0, 1, 0)
    time.sleep(delay)
    setStep(0, 0, 0, 1)
    time.sleep(delay)

def setStep(w1, w2, w3, w4):
  GPIO.output(pin_4, w1)
  GPIO.output(pin_17, w2)
  GPIO.output(pin_23, w3)
  GPIO.output(pin_24, w4)
  
def main():
    init() 
    while True:
        forward(int(delay) / 1000.0)
		
main() #调用main



你可能感兴趣的:(其他)