在Raspberry pi 3上使用US-100时出错

时间:2018-01-20 17:44:37

标签: python raspberry-pi raspberry-pi3 pwm

我为我的US-100超声波测距传感器编写了以下代码。但我每次都得到垃圾结果。当我移动我的传感器时,我的阅读没有任何变化。此外,当我将GND和VCC连接到传感器时,输入停止生成。需要帮助将电路图连接到US-100和Raspberry pi 3以及导致垃圾结果的此代码中的错误

import RPi.GPIO as GPIO
import time
import logging

LOG_FILENAME='US_100.log'
logging.basicConfig(format='%(asctime)s %(message)s',filename='US_100', 
level=logging.DEBUG)


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
TRIG = 23
ECHO = 24
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
while True:
    GPIO.output(TRIG,False)
    time.sleep(1.5)
    print("Waiting for sensor to settle")
    GPIO.output(TRIG,True)
    time.sleep(0.0001)
    GPIO.output(TRIG, False)
    pulse_start=time.time()
    while GPIO.input(ECHO) == 0:
        pulse_start = time.time()
    while GPIO.input(ECHO) == 1:
        pulse_end = time.time()
    pulse_duration = (pulse_end-pulse_start)
    print("Pulse duration =%1f " %pulse_duration)
    distance = (pulse_duration*343/2)
    if distance >0.5 and distance <400:
        print("Distance = %1f" %distance)
    else:
        print("Out of Range")
logging.debug(distance)

即使物体相距15-20厘米,我也会得到的结果图像。

Output Image

0 个答案:

没有答案