WHCSRL 技术网

树莓派控制步进电机

树莓派控制步进电机

前言

测试步进电机

设备

名称型号
树莓派3B+
步进电机28BYJ-48-5V28BYJ-48-5V
步进电机驱动板UL2003芯片驱动板UL2003芯片驱动板

连接

树莓派3B+ GPIO引脚图可在此文章中查看: 树莓派 亮灯 笔记

步进电机插入驱动板

驱动板VCC接树莓派5V
驱动板GND接树莓派GND
驱动板INA接树莓派20
驱动板INB接树莓派26
驱动板INC接树莓派16
驱动板IND接树莓派19

接线图

源码

import RPi.GPIO as GPIO
import time
#BCM 对应 GPIO numbers , BOARD 对应 physical numbers。
GPIO.setmode(GPIO.BCM)

delay=2 #delay 2ms

pin_4= 20
pin_17 = 26
pin_23 = 16
pin_24 = 19

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 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 back(delay):  
    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 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 main():
    init() 
    while True:
        # forward(int(delay) / 1000.0)
        back(int(delay) / 1000.0)


main() # 调用main
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
推荐阅读