2016-04-12 53 views
0

我實際上想在經典的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玩嗎?正如你所看到的,數據表是相當光明的,所以也許你會看到我的可憐的電子技能已經錯過了

問候!

回答

0

您期待GPIO_ECHO爲從一開始。根據文件,它首先是,然後,然後返回到 。

也許

while GPIO.input(GPIO_ECHO)==0: 
    # some short sleep might be better 
    pass 

start = time.time() 

while GPIO.input(GPIO_ECHO)==1: 
    pass 

while GPIO.input(GPIO_ECHO)==0: 
    pass 

stop = time.time() 

有許多可用於檢測上升沿和下降沿的方法,參見例如raspi.tv arcticle。使用這些方法可能會更好。

0

據:

https://forum.arduino.cc/index.php?topic=153700.30

傳感器是獲取足夠的電量相當敏感 - 檢查你的5V不會下降太多。

此外樹莓派GPIO引腳是3V3-他們可能不喜歡傳感器的輸出(大概是5V),並且傳感器可能不會從Raspberry pi的3V3輸出上觸發。

相關問題