您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

树莓派驱动5线4相步进电机

 硬件:树莓派4b+ULN2003驱动板+ 28BYJ-48 1:64步进电机

 

 硬件接线

IN1--->pin32

IN2--->pin36

IN3--->pin38

IN4--->pin40

import RPi.GPIO as GPIO
import time
 
IN1 = 32    # pin32
IN2 = 36
IN3 = 38
IN4 = 40
 
def setStep(w1, w2, w3, w4):
	GPIO.output(IN1, w1)
	GPIO.output(IN2, w2)
	GPIO.output(IN3, w3)
	GPIO.output(IN4, w4)
 
def stop():
	setStep(0, 0, 0, 0)
 
def forward(delay, steps):  
	for i in range(0, steps):
		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 backward(delay, steps):  
	for i in range(0, steps):
		setStep(0, 0, 0, 1)
		time.sleep(delay)
		setStep(0, 0, 1, 0)
		time.sleep(delay)
		setStep(0, 1, 0, 0)
		time.sleep(delay)
		setStep(1, 0, 0, 0)
		time.sleep(delay)
 
def setup():
	GPIO.setwarnings(False)
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(IN1, GPIO.OUT)      # Set pin's mode is output
	GPIO.setup(IN2, GPIO.OUT)
	GPIO.setup(IN3, GPIO.OUT)
	GPIO.setup(IN4, GPIO.OUT)
 
def loop():
	while True:
		print ("backward...")
		backward(0.003, 512)  # 512 steps --- 360 angle
		
		print ("stop...")
		stop()                 # stop
		time.sleep(3)          # sleep 3s
		
		print ("forward...")
		forward(0.005, 512)
		
		print ("stop...")
		stop()
		time.sleep(3)
 
def destroy():
	GPIO.cleanup()             # Release resource
 
if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child function destroy() will be  executed.
		destroy()

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进