Incorrect Distance Readings from HC-SR04 Ultrasonic Sensor on BeagleBone Black

Hey guys based on my question on developing an obstacle detection system using a BeagleBone Black running Embedded Linux using HC-SR04 ultrasonic sensor to measure distances and displays the measurements on a 16x2 LCD screen. i was finally able to solve the issue with the lcd screen it turns out it was faulty and i had to change it and is now working smootly, but am having another issue where am getting incorrect distance reading, i know this because from my observations the distance readings are inconsistent and unrealistic, with extreme values and occasional zero readings, please how can i resolve this here's my code
def measure_distance_simple():
GPIO.output(TRIG_PIN, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(TRIG_PIN, GPIO.LOW)

while GPIO.input(ECHO_PIN) == GPIO.LOW:
start_time = time.time()

while GPIO.input(ECHO_PIN) == GPIO.HIGH:
end_time = time.time()

duration = end_time - start_time
distance = (duration * 34300) / 2
return distance

try:
while True:
distance = measure_distance_simple()
print(f"Distance: {distance:.2f} cm")
time.sleep(1)

except KeyboardInterrupt:
GPIO.cleanup()
def measure_distance_simple():
GPIO.output(TRIG_PIN, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(TRIG_PIN, GPIO.LOW)

while GPIO.input(ECHO_PIN) == GPIO.LOW:
start_time = time.time()

while GPIO.input(ECHO_PIN) == GPIO.HIGH:
end_time = time.time()

duration = end_time - start_time
distance = (duration * 34300) / 2
return distance

try:
while True:
distance = measure_distance_simple()
print(f"Distance: {distance:.2f} cm")
time.sleep(1)

except KeyboardInterrupt:
GPIO.cleanup()
attachment 0
Solution:
It's likely because of the noise or timing inaccuracies. Try adding a timeout for the echo response Try if that would work!...
Jump to solution
4 Replies
Solution
wafa_ath
wafa_ath5mo ago
It's likely because of the noise or timing inaccuracies. Try adding a timeout for the echo response Try if that would work!
Boss lady
Boss lady5mo ago
thanks @wafa_ath it works perfectly now
Boss lady
Boss lady5mo ago
still based on myquestion on developing an obstacle detection system using a BeagleBone Black running Embedded Linux using HC-SR04 ultrasonic sensor to measure distances and displays the measurements on a 16x2 LCD screen, how do i ensure my obstacle detection system starts automatically when the BeagleBone Black boots up
wafa_ath
wafa_ath5mo ago
Stack Overflow
Executing a script on startup using BeagleBone Black
I have an a.out which I want to run when my BeagleBone boots up. It is actually a socket server which I want to start as soon as the BeagleBone powers up. I tried to put this in /etc/init.d, but it...
Want results from more Discord servers?
Add your server