I'm making a robot that should avoid obstacles and im using a HC-SR04 to measure distances. My problem is that my readings are off by a large margin. when there is no obstacle in the way I get a constant measure around 100 cm, but when I place an object in front of the reader suddenly it goes up to about 2000 cm. I've tried using the gpiozero library aswell, but same thing keeps happening. Is the reader damaged or what can I try?
import RPi.GPIO as GPIO import time import signal import sys GPIO.setmode(GPIO.BCM) pinTrigger = 12 pinEcho = 16 def close(signal, frame): GPIO.cleanup() sys.exit(0) signal.signal(signal.SIGINT, close) GPIO.setup(pinTrigger, GPIO.OUT) GPIO.setup(pinEcho, GPIO.IN) while True: GPIO.output(pinTrigger, True) time.sleep(0.00001) GPIO.output(pinTrigger, False) startTime = time.time() stopTime = time.time() while 0 == GPIO.input(pinEcho): startTime = time.time() while 1 == GPIO.input(pinEcho): stopTime = time.time() TimeElapsed = stopTime - startTime distance = (TimeElapsed * 34300) / 2 print ("Distance: %.1f cm" % distance) time.sleep(1)