HC-SR04 Value variations over time

Hello,

I am struggling with a issue regarding the Ultra Sonic Sensor HC-SR04.
In my project humidity governor for a cigar Humidor I am using the SR04 for checking water level in tank using the NewPing Library. During development time I found that the measured distance value is sometimes changing.

Here you can see a measurement over 9 hours.

To be shure, that it ist not an side effect off my software, I made a seperate test:
Using a different Arduino, another sensor (but same shippment), an as software just the example code of the newping library. The Sensor is mounted in a wooden box with a fixed distance, an garanteed no optical disturbance, like external light, side walls in range and so on.

And, surprising, I found the same effect!

Here you can see a 3 days measurement. The Sensor îs read every 10 seconds. The data are collected via Serial Monitor.


You can see, the measured Value is change between two levels.
A better impression can be seen in a stastical frequency distribution diagramm:

Distance Sensor - Arduino approx. 40cm. I tried both: normal wire an shielded wire: no difference. Temperature was quite constant (indoor room temperature), I could not find any correlations to time of the day.

Next guess was an effect of the library
To check this, I created a software with reading the Sensor by PulseIn().
Same effect!

Here you can see a 24 hour plot where I measured the pingtime also with PulseIn(), and, to get ore date also with the pingMedian() method.

Looking at the frequency distribution, you can see that the pingMedian method ist more sharp, but at the end all have the same result.

So, this is my story.
Did anybody ever see this effekt?
Does anybody have an idea about the root cause?

For this type of device, I would have thought 5mm fluctuations would be within range of expectations.

Weedpharma

PulseIn isn't accurate, if external interrupt happened during measurements you have 3+ usec off time to service request, this probably what you have. Better way to measure timing is ICP feature of Timer 1, but you have to use specific pin, depends on the arduino board, that may be all not so convenient in case of libraries conflict or shields demanding pin.

if external interrupt happened during measurements you have 3+ usec off time to service request, this probably what you have.

But the OP is seeing fluctuations of up to 25 usec, with a bimodal distribution, if I am interpreting the figures correctly.

This is interesting, but I don't have another theory as to what the problem might be. There is a clear difference between the box and the water tank tests and it doesn't look random at all.

I agree that measuring time intervals using Timer1 in the input capture mode would be the way to go, as this in not affected by interrupts. For an introduction to the technique see this post and scroll down to reply #12 ("Timing an interval using the input capture unit").

You are right, this may be an effect of an internal (25µs?) System Interrupt, disturbing the measurements of PulsIn oder the NewPing library (I do not know, how the Newping Lib is working).

Next weekend I will repeat the test with interrupt driven measurement.