1

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?

enter image description here

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.

enter image description here

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.

Quinton Chester
  • 336
  • 4
  • 13
  • Getting values that large means that the output pulse triggered for over six seconds. Can you use an oscilloscope to verify if the sensor is actually doing that or if your code just isn't running fast enough and is missing parts of the sensor's output pulses? – Blender Mar 09 '18 at 03:19
  • @Blender, Unfortunately I do not have an oscilloscope. As I mentioned, I am *very* new to this side of things. Any other recommendations? Should my initial sleep be longer than .1? Maybe .5 or so? – Quinton Chester Mar 09 '18 at 03:24
  • The Raspberry Pi's GPIO pins are 3.3V, yet this sensor is 5V. Your sensor may not be reliably picking up your initial trigger pulse because the voltage isn't high enough, and you might damage the GPIO pins by connecting them to a 5V source. – Blender Mar 09 '18 at 03:37
  • @Blender, the power is 5v, the ground is fine, the echo has a 1k resistor on it thus dropping it from 5v. The out from the GPIO is enough to trigger the ultrasonic sensor, so it should be fine as well. – Quinton Chester Mar 09 '18 at 03:47
  • The datasheet states that the minimum range is 2cm. Does your sensor work properly if you don't place it face down on a table? – Blender Mar 09 '18 at 03:57
  • if your obstacle is directly nearby the ultarsonic receiver tranceiver as on your image then the signal will not get reflected where it should so it will timeout ... that is what the min range is for so you need to have at leas 2cm of air between sensors and obstacle (counting from the top of the cylinders). However I just measured this Crappy sensor and its veeeeerry inaccurate ... looks like it has some sort of ranges up to ~7 cm is more or less precise but above that threshold it looses precision a lot and can be off even by 5-7 mm haven't tested more than 20cm have turned of that crap. – Spektre Feb 15 '21 at 10:24
  • And planning to bypass its onboard MCU as it is simply coded poorly ... Last time I was doing [SONAR like this I used AT89C51 with 20MHz](https://stackoverflow.com/a/43961689/2521214) and got 0.3 mm resolution for 4 sensor pairs simultaneously up to 3 m the board has way faster MCU so they must have screw something ... – Spektre Feb 15 '21 at 10:43
  • I successfully bypassed the onboard MCU improving accuracy (the majority of error was due poor coding of MCU probably some interrupt messing with timing). The resulting error is ~0.1mm and ~4 mm. The ~4 mm is wavelength of received signal after processing and depends on reflective surface quality and distance (its getting worse the more far it is) because the analog processing part of the sensor is really weird and first and last period get screwed in amplitude so it is sometimes not accepted as signal causing measuring the next period (so the distance jumps by ~4 mm) – Spektre Feb 19 '21 at 07:57
  • with increasing distance the error might jump to higher multiple of ~4mm. I plan to change the circuitry completely to obtain more reliable results however that would require more time I do not have right now for it so I am putting the project on the shelf for now ... – Spektre Feb 19 '21 at 07:59

0 Answers0