使用DYP-ME007Y-PWM超声波传感器

时间:2016-04-12 14:40:29

标签: python raspberry-pi

我实际上想在经典的Raspbian操作系统上的树莓PI Compute模块上使用这款防水超声波传感器DYP-ME007Y-PWM(http://hanjindata.lgnas.com:10000/myweb/P0400/P0400.pdf)。它有4个引脚(gnd,Trig,Echo和5V)。 这是我的原理图:

Raspberry Pi |  Sensor
GND          | GND
5V           | 5V
22           | Trig
23           | Echo

我找到了一些教程,解释了超声波传感器的工作方式,以及使用其他类型的超声波传感器(例如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()

我不工作,无论我对传感器做什么,我都会获得相同的输出 有没有人玩过这款传感器?正如你所看到的,数据表非常简洁,所以也许你会看到我糟糕的电子技能遗漏的东西

问候!

2 个答案:

答案 0 :(得分:0)

您希望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() 从一开始就 1 。根据文档,它首先 0 ,然后 1 ,然后返回 的 1

也许

   template : '<div><span><label>location=</label>{{filter}}</span></div>'

有一些方法可用于检测上升沿和下降沿,例如参见raspi.tv arcticle。使用这些方法可能更好。

答案 1 :(得分:0)

根据:

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

传感器对获得足够的电力非常敏感 - 检查您的5V电压是否下降太多。

Raspberry Pi GPIO引脚也是3V3 - 它们可能不喜欢传感器的输出(可能是5V),传感器可能不会触发Raspberry pi的3V3输出。