简单介绍用raspberry pi 3利用RPi.GPIO以PWM方式驱动伺服舵机TowerPro SG90.
准备工作:
1) Raspberry Pi 3 +Raspbian jessi 安装
2) ? 设置SSH
3) ?杜邦线若干
4)熟悉pi3的GPIO port见下图
from: http://www.raspberrypi-spy.co.uk/wp-content/uploads/2012/06/Raspberry-Pi-GPIO-Layout-Model-B-Plus-rotated-2700x900.png
5) Tower Pro SG90见下图:
它只需要三根接线,见下图:
? 将板子上2号pin(就是5v的输出电压)与电机的Vcc(红线)相连。6号pin与地线相连。再找一个GPIO口(比如GPIO4,也就是板上编号为7的)相连。
用python的RPi包,进行直接GPIO口的读写操作。程序如下:
<p>
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
SERVO = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(SERVO, GPIO.OUT)
p = GPIO.PWM(SERVO, 50)
p.start(2.5)
try:
while True:
p.ChangeDutyCycle(7.5)
time.sleep(1)
p.ChangeDutyCycle(12.5)
time.sleep(1)
p.ChangeDutyCycle(2.5)
time.sleep(1)
print "Done loop"
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
</p>