本教程教会你使用树莓派控制ULN2003 驱动板、28BYJ-48-5V减速步进电机。
所需材料:
母对母杜邦线:至少6根
ULN2003 驱动板 1个
28BYJ-48-5V减速步进电机 1个。
小白说明:步进电机在连结好驱动板,启动树莓派之后,在没启动程序时,驱动板上的灯是不会亮的。之前很多人认为不亮就怀疑板子有问题。
1、连线:
驱动板 树莓派
IN1 接 GPIO 17
IN2 接 GPIO 18
IN3 接 GPIO 21
IN4 接 GPIO 22
+ 接 5V
- 接 GND
2、连结好线之后,进入打开树莓派桌面上的LXT终端(本文用的远程PUTTY),输入如下命令:sudo nano bujindianji.py
打开后,输入如下内容:
- #-----------------------------------
- # Name: bujindianji
- # Created: 03/16/2014
- #-----------------------------------
- #!/usr/bin/env python
-
- # Import required libraries
- import time
- import RPi.GPIO as GPIO
-
- GPIO.setmode(GPIO.BCM)
-
- StepPins = [17,18,27,22]
-
- # Set all pins as output
- for pin in StepPins:
- print "Setup pins"
- GPIO.setup(pin,GPIO.OUT)
- GPIO.output(pin, False)
-
- # Define some settings
- StepCounter = 0
- WaitTime = 0.02
-
- # Define simple sequence
- StepCount1 = 4
- Seq1 = []
- Seq1 = range(0, StepCount1)
- Seq1[0] = [1,0,0,0]
- Seq1[1] = [0,1,0,0]
- Seq1[2] = [0,0,1,0]
- Seq1[3] = [0,0,0,1]
-
- # Define advanced sequence
- # as shown in manufacturers datasheet
- StepCount2 = 8
- Seq2 = []
- Seq2 = range(0, StepCount2)
- Seq2[0] = [1,0,0,0]
- Seq2[1] = [1,1,0,0]
- Seq2[2] = [0,1,0,0]
- Seq2[3] = [0,1,1,0]
- Seq2[4] = [0,0,1,0]
- Seq2[5] = [0,0,1,1]
- Seq2[6] = [0,0,0,1]
- Seq2[7] = [1,0,0,1]
-
- # Choose a sequence to use
- Seq = Seq1
- StepCount = StepCount1
-
- # Start main loop
- while 1==1:
-
- for pin in range(0, 4):
- xpin = StepPins[pin]
- if Seq[StepCounter][pin]!=0:
- print " Step %i Enable %i" %(StepCounter,xpin)
- GPIO.output(xpin, True)
- else:
- GPIO.output(xpin, False)
-
- StepCounter += 1
-
- # If we reach the end of the sequence
- # start again
- if (StepCounter==StepCount):
- StepCounter = 0
- if (StepCounter<0):
- StepCounter = StepCount
-
- # Wait before moving on
- time.sleep(WaitTime)
复制代码
然后按ctrl+x 按y 回车 达到保存目的
然后输入命令:
udo python bujindianji.py
就可以看到电机在转动了。LXT终端也会有数字变化,如下图。 代码中的,WaitTime = 0.02 可以调节0.02来控制转速。。。0.5会很慢。
|
|
|
|
|
|