一、舵机,我的树莓派测试的时候ChangeDutyCycle里面的值大概是2-12,总共180度
舵极测试
GPIO.setmode(GPIO.BCM)
panPin = 17
GPIO.setup(17, GPIO.OUT)
GPIO.setwarnings(False)
pan = GPIO.PWM(17, 50)
pan.start(2)
pan.ChangeDutyCycle(7)
二、超声波模块,所接的GPIO是BCM模式下的20与21
import RPi.GPIO as GPIO
import time
Trig_pin = 20
Echo_pin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(Trig_pin, GPIO.OUT)
GPIO.setup(Echo_pin, GPIO.IN)
time.sleep(2)
def checkdist():
GPIO.output(Trig_pin, GPIO.HIGH)
time.sleep(0.00015)
GPIO.output(Trig_pin, GPIO.LOW)
while not GPIO.input(Echo_pin):
pass
t1= time.time()
while GPIO.input(Echo_pin):
pass
t2 = time.time()
t = (t2-t1)*340*100/2
return t
try:
while True:
print('Distanse:%0.2f cm'%checkdist())
if checkdist()<15:
print('dis < 15')
else :
print(' dis>15 ')
except KeyboardInterrupt:
GPIO.cleanup