0

我正在尝试在我的树莓派设备上配置 US100 超声波距离传感器。我会遵循链接中描述的方法,但不会从设备获得任何输出。

我也在分享我的代码,这样如果有人能在任何情况下发现我做错了什么。

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 = 18
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)
4

0 回答 0