我實際上想在經典的Raspbian操作系統上在我的樹莓派PI計算模塊上使用這種防水超聲波傳感器DYP-ME007Y-PWM(http://hanjindata.lgnas.com:10000/myweb/P0400/P0400.pdf)。它有4個引腳(gnd,Trig,Echo和5V)。 這裏是我的示意:使用DYP-ME007Y-PWM超聲波傳感器
Raspberry Pi | Sensor
GND | GND
5V | 5V
22 | Trig
23 | Echo
我發現一些教程解釋傳感器工作和mannage如何超聲波具有與其他種類的超聲波傳感器的這樣一個爲例(http://www.micropik.com/PDF/HCSR04.pdf)
這裏良好的效果是我的代碼:
# Import required Python libraries
import time
import RPi.GPIO as GPIO
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_TRIGGER = 22
GPIO_ECHO = 23
print "Ultrasonic Measurement"
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
# Allow module to settle
time.sleep(0.5)
# Send 10us pulse to trigger
while True:
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
# That was the distance there and back so halve the value
distance = (elapsed * 34000)/2
print "Distance : %.1f" % distance
time.sleep(0.05)
# Reset GPIO settings
GPIO.cleanup()
我不工作,我總是得到相同的輸出我無論做什麼與我的傳感器 有誰有這種傳感器alreeady玩嗎?正如你所看到的,數據表是相當光明的,所以也許你會看到我的可憐的電子技能已經錯過了
問候!