HC-SR04 rangefinder configuration

Hi

I have a HC-SR04 rangefinder which should not even work but somehow it works perfectly on ground upto 4m and then in flight it goes crazy even when something is in range, reading 0. It uses GPIO to communicate on the trig and echo pins. so I suspect lack of echo signal on the rangefinder in flight but i need to verify this. Weirdly, the stat is 4 which means good signal which suggests proper functioning of the rangefinder, but the quality is -1, meaning invalid. the readings works but then spikes down to 0 very often

So, is there some way to log the GPIO signals? The transmitted data on the trig from pixhawk and the echo signal input?

In the log RCOU messages it detects the pin as GPIO (verified by the text that comes on hovering over the pin) but its value stays 0

As always show your settings/parameter to such questions.

Did you read and follow the wiki correct.

I assume not 100% as there is mentioned to set the max range not greater than 2m, as otherwise ardupilot has some problems in correct readings.

Is your rangefinder looking downwards?

It is forward facing

I will attach a log and param soon.

I have followed the wiki

By setting the range as given, the rangefinder value becomes stuck to 0 soon

Also, my rangefinder is reliable able to read upto 4m, has been tested on ground.

Trying 4m extends this

Somehow, at 12m mark, the value never becomes 0 on ground

I have tried, 2m, 3m, 4m and it works for a kur a few minutes and then fails

On 12m it works for atleast 30min since that is the max I have tested and the range value is always correct

Wow, which exact module you are using as all datasheets I have seen state a range from 2cm upto 400cm.

I have found those to be junk. 10yrs ago… But, what can you expect when you can buy 5 of those for $9.

1 Like

Keep in mind that noise floor is much higher when flying.

Some random module from Amazon

I know they trash but just wanted something for one direction and upto 3m. It should atleast work, just not as good as some other stuff.

I was first thinking about VL53L0X or VL53L1X since they are cheap but they have trash precision and they do not work outside

Hm, what is a random module.
Especially on Amazon, every product has a manufacturer’s identifier, a reference number, and an Amazon internal product number (ASIN).
How you testet the range? What kind of objects? Moving or steady?

Tested with rapid movements, and as well as with normal moving stuff

This is the sensor

What ever it is it is the same as all other:

Robocraze HC-SR04 Ultrasonic Distance Sensor

The HC-SR04 Ultrasonic Distance Sensor provides very short (2CM) to long-range (4M) detection

I had a look to the ardupilot source. The update rate of the trigger impuls is 15Hz => period 67ms.
The distance is calculated on 58µs / cm. So this is a absolut maximum distance of 11,55m which ardupilot theoretically can measure. Also ardupilot has a relative simple inbuilt glitch filter. It still rejected any rapid changing values with a change from one measurement to the next greater than 0,5m. And last ardupilot sets the distance to 0 if measurments missed for more than a second.
This are just only the limits from ardupilot firmware.

The 0 feature is pretty troubling

I have the source set up
Can I just remove the code for setting to 0

I have no plan of using it for any purpose, it is just there for logging

The max distance can be set to 450 and then it will not go to 0

If you are able to built your own firmware it is possible to change any code.
So you can just try to comment out the line
state.distance_m = 0.0f;
in file
ardupilot/libraries/AP_RangeFinder/AP_RangeFinder_HC_SR04.cpp

But if you do it on a copy of the master brunch you also will get all other firmware changes since your actual version. So be carefully to select the correct version you want.

Also the expected behaviour is that you always get the last measurement but you don’t get any information how old is this information

1 Like

Ok
Will try asap

From a previous issue with compass I had cloned entire copter 4.6.3 stable

So it shouldn’t be a problem

I looked into the code as you mentioned and it only sets to 0 when it fails, if it gives valid reading then it passes the valid reading again. But, in my case the readings very often fall to 0 which can be fixed but sometimes they work and they set to 0 and never climb back. This should not be caused from the code as per my understanding, so it could be hardware. According to me, I need to log the gpio pin status so I can see in the log what happens when it fails, like stopping of echo, failure to return, or something else. So, can I log a GPIO without LUA since the Pixhawk cant run LUA?

Super weird new symptom

I removed the distance setting to zero line.
Also with these cheap sensors (some others I have used with unos) read 1183 when no obstacle is there and I suspect mine to be doing the same
I increase the 67ms in some if line in the rangefinder code to 70 which allows 1183 in range

Now weirdly, it reads correctly on start and then reading stays stuck there until I reboot and then the new reading comes and stays there stuck what to do??
In a test flight it seems to be working weirdly:

Here is the mounting

the soldering is also good.
I will do more test flights as soon as possible and will report back the results.

It is not clear to me what you meant with “read 1183 when no obstacle is there”.
How is your measurement setup. You pointing with sensor in a complete free room with no walls or other things reflecting the waves?
Value 1183 you meant 1183cm?
On ardupilot the measurement calculation is triggered by the returning signal. If no echo is coming no update, so far I understood the code.

I have used some sensors hc sr04 in normal projects like ultrasonic distance meter
When I point something faraway, it read 1183 cm

This is also possible with this setup so I increased the 67ms window to 70 which allows upto 1200cm which covers 1183cm

This is only answering part of my question.
Where you are pointing with your sensor in the test mentioned above? You are pointing to nowhere or to wall some meters away? How exact was your test?

Wall 20-25m away and also when no object

Do you have the possibility to monitor the “Echo” pin on the module parallel to ardupilot.
If the the walls are 20-25m away they are far out of range. So the module didn’t trigger the ardupilot update routine. So the your above described behaviour is as expected