使用HC-SR04超声波传感器找不到距离

时间:2020-07-22 17:39:33

标签: python raspberry-pi arduino-ultra-sonic

我正在尝试使用HC SR04超声波传感器查找到附近物体的距离。我通过以下链接关注了本教程:

HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi

我复制了给定的所有说明。尝试运行代码时遇到问题。

import RPi.GPIO as GPIO
import time

# Setting the GPIO pins mode
GPIO.setmode(GPIO.BCM)

# Setting the pin values
TRIG = 23
ECHO = 24

# Assigning the pins to GPIO
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

# Allowing the sensor to settle down
GPIO.output(TRIG, False)
print('Waiting for thr sensor to settle down!!')
time.sleep(2)

# Sending a trigger pulse of 10 micro seconds duration
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

# Starting the measurement process
pulse_start = 0
pulse_end = 0

# Checking for the last timestamp at which the ECHO is OFF
while GPIO.input(ECHO) == 0:
    print('Running ECHO = 0 loop')
    pulse_start = time.time()

print('Pulse start: ',pulse_start)

# Checking for the last timestamp at which the ECHO is ON
while GPIO.input(ECHO) == 1:
    print('Running ECHO = 1 loop')
    pulse_end = time.time()

print('Pulse end: ',pulse_end)

# Calculating the pulse duration
pulse_duration = pulse_end - pulse_start

# Distance Calc
distance = round((343/2)*pulse_duration,2)
print('Distance: ', distance, 'm')

# Cleaning up the pins
GPIO.cleanup()

该代码似乎没有进入第一个while循环,在该循环中正在检查ECHO引脚是否为低电平信号。这在计算距离时给了我一个问题。上面代码的输出如下:

Code Output

任何人都可以指出问题所在吗?

0 个答案:

没有答案