HC-SR04 reading 0cm

In my project I intend to use an HC-SR04 to determine if a water barrel is sufficiently full of water or not.

I only intend to measure when I actually need to pump water from the barrel, so I would like to be certain of the result of the measurement.

As a test - and with two different HC-SR04, I am measuring every 5s over a fixed distance. Although I am overall satisfied with the measurements, every now and then, I have noticed that the reading is 0cm and not the expected 68cm(ish).

Since I have tried with two different sensors, my assumption is that it is not a hardware failure.

Have anyone else seen something similar?

I am using the NewPing library.

You could filter it out using your code. Your code will simply ignore the 0 reading.

if (Reading > 0)
{
your code
}

Yes - this happens sometimes with cheap sensors, they're not perfect. You need to do some filtering.

At that 5 min mark, don't take a single reading, take a number, 20 or 30 or so, then average those readings. Will give a more stable signal already.

Thank you for your suggestions.

I realize that I am going to do some filtering when actually implementing this.

I did some more tests, and logged a little over 2200 measurements, and it turned out that around 9% of those were reading zero's.

The longest streak of non-zero measurements were 53, the shortest two.

Time to change sensors.
Or post your code.
Or both.