I'm trying to create a distance measuring device to measure the salt levels of a water softener within a home. I'm a software engineer by trade but am new to learning the electronic side of hardware.
It seems like everything should work, I've read quite a bit of guides on how to measure distance with the HC-SR04, but it seems that each recording I get is different which is odd given that the distance sensor is sitting face down on a wooden table. Should it not be reading 0 each time or something really close to it?
Below you will see a screenshot of the python results. Note that the results where it indicates that it returned a large number, those numbers were EXTREMELY large. Typically 3,000 and up to 120,000. Is it possible that something is going on with the GPIO pins themselves?
I might mention that my HC-SR04 is wired directly to the GPIO. My ground is grounded, VCC is to 5v, Echo has a 1k resistor on it and is on BCM22, and my Trigger is on BCM17.
Here is a picture of the HC-SR04 on the tabletop.
This is what my python file consists of.
import RPi.GPIO as GPIO
import os, signal
from time import time, sleep
while True:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(22,GPIO.IN)
GPIO.output(17, GPIO.LOW)
sleep(0.1)
GPIO.output(17, GPIO.HIGH)
sleep(0.00001)
GPIO.output(17, GPIO.LOW)
while GPIO.input(22) == GPIO.LOW:
pulse_start = time()
while GPIO.input(22) == GPIO.HIGH:
pulse_end = time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17160.5
distance = round(distance, 2)
if (distance > 4):
print("Distance recorded a really large number, something isn't right. Restarting...")
GPIO.cleanup()
sleep(0)
else:
print(distance)
GPIO.cleanup()
sleep(60)
Any and all help on this would be appreciated. If I've done something wrong, I'll be the first to admit to it, but please do provide helpful feedback. I would prefer to NOT use a breadboard as space is limited.