HC-SR04 measurements and different (variable) results

I've connected a HC-SR04 ultra sonic sound measurement board to my ESP32 board. I've installed a very simple sketch to do the distance measurement. But the result is a bit strange in my view. The HC-SR04 board is laying still on the table and the distance measurements vary a lot, see below:

34in, 88cm
28in, 72cm
34in, 88cm
41in, 105cm
34in, 89cm
41in, 105cm
34in, 89cm
41in, 105cm
34in, 88cm
28in, 72cm
41in, 105cm
34in, 89cm
28in, 72cm
55in, 140cm
34in, 89cm
28in, 72cm
34in, 88cm
42in, 107cm
34in, 89cm
41in, 106cm
34in, 88cm
34in, 88cm
34in, 88cm
34in, 88cm

Why is this the case? How can this be improved?

And also the spec of the HC-SR04 says it can measure up to 400 cm. During my tests 140 to 150 cm is the maximum, how come? What am I doing wrong?

I took the example code from:
https://diyi0t.com/ultrasonic-sensor-tutorial-for-arduino-and-esp8266/ or
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you got it looking at a flat surface infront of it, if its just laying on the bench, then all sorts of reflections will be coming back to the sensor.

Thanks.. Tom.. :slight_smile:

4m is well possible if you aim the sensor square at a flat solid wall that gives good sound reflections.

You won't get any reflection when you aim it at the furry hair of the cat, I often don't even get reflections from my legs until I pull up my pants so the sensor is exposed to my bare skin.

If you are in a well echoing room (e.g. inside a water tank), taking measurements too frequent will cause the sensor to pick up reflections from the previous ping, resulting in readings all over the place. Quite interesting to see. It takes a couple hundred milliseconds for echoes to reliably die out, in a big tank maybe even longer.

The code in the first link is rubbish, I suggest you ignore it

I have done some recent practical testing with the HC-SR04 sensor, which might help guide you a bit. It does get detailed, but the discussion might help you organize your thoughts on just how to approach determining distance, and what controls the range you can get. Some of the discussion mirrors what the others have mentioned, See the readme, or pdf in my github repository http://github.com/GaryDyr/HC-SR04-beam-tests. The sketch there might also help.

Thanks for all the feedback, appreciated. I had posted my sample code etc but I guess that post still needs to be approved by the moderator as I don't see it in the list above.

I want to detect if a car is parked at the private parking place in the garage. My thoughts were to do this with this distance sensor. When I measure 1 meter the car is parked, when I measure over 2 meters (or something like that) the car is away. But the car doesn't have a flat surface, so the reflection of the sound will be an issue.

Is there an different way, a alternative solution, to check if the car is parked or not? Any suggestions?

Hi,

But the car doesn't have a flat surface, so the reflection of the sound will be an issue.

The car will be a big enough size not to worry about a flat surface.

Your code doesn't need moderator approval, it looks like it just didn't copy.

Tom.. :slight_smile:

That is an interesting and nice project. I see that the sensor is located directly behind the car. This is probably a good position in that situation. In my situation I cannot position it like that.

I need to mount it higher as there is no free space on the lower side. See also my bad picture :slight_smile:

Due to the wiring I want to have the sensor at the back of the wall (left on the picture).

Just aim it roughly at where the car will be.

I want to detect if a car is parked at the private parking place in the garage. My thoughts were to do this with this distance sensor. When I measure 1 meter the car is parked, when I measure over 2 meters (or something like that) the car is away. But the car doesn't have a flat surface, so the reflection of the sound will be an issue.

Well, this HC-SR04 github repository will give you an idea of the issues with monitoring a vehicle's distance from the sensor: GitHub - GaryDyr/HC-SR04_motion_detection. See the pdf or readme. The tests reported have way more than you ever may care to know about with regard to monitoring a vehicle's front position, but you will find that 2 m is just about the maximum distance to read the front grill of a car, depending on the grill shape, of course. As for the back of a car, it might be just as bad as monitoring the front. Depends on how streamlined the car is, and whether the sensor "sees" a large enough flat area of the car that can continuously reflect back to the sensor.

Are you powering it with 5 volts?

rgroothuis:
Why is this the case? How can this be improved?

The SR04 transmits at 40kHz and receives at 38Khz. The SR04 up converter, generates 12V from V+ is known to be very noisy.

Adding a good quality electrolytic directly on the power pins of the SR04, improves its performance.

You can do a search on the I net about fixing the 38Khz receiver, when I priced it out a few years ago the parts would cost 30.00USD.

Finding the sources of ultrasonics in your residence, and eliminating them does a good job with improving the SR04's operation.

I found that particular SR04's will give false readings in a range, I used 2 SR04's that gave false readings at different distances, If A got a hit, then I fired B to check it out. If B does not get a hit at nearly the same distance then it's a falsey thing.

Moving onto a different sensor. If using ultrasonics, go for a 1Mhz sensor, which are much more stable then the SR04's.

I switched to LIDAR. Now that works great mounted on a hexapod that walks around the house on its own. I orginally used a TFMini. Switched to a TFMiniP. The hexapod can 'see' a 2 inch pipe at 500cm, set against a wall with the TFMini.

I have been experimenting with a AMG8833. I, currently have set one up so that it can 'see' if we are in the bed. I set it so that if one of us is in the bed, the AMG detects the presence, the MCU sends a message to the plants sunlamp to turn off, if on, or stay off is not on. I've been only running this for 4 days buts its working great, so far.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.