0

I'm experimenting with an HC-SR04 sonar sensor. Basically I'm measuring the distance to a cardboard box twice a second with these lines of code running on an ESP32:

unsigned long sonarMeasure() {
 digitalWrite(TRIGGER, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIGGER, LOW);
 return pulseIn(ECHO, HIGH);
}
double distance = duration * 0.03455;

The results are a bit strange. I know, that I can't expect millimeter accuracy. But why are there rather discrete jumps of about 4 mm? And when looking closer, there is another frequent jump of <1mm:

measured distances

(As far as I can tell, the jumps increase at larger distances.)

Is this an issue of how I read the sensor on the ESP32, or is it something happening internally on the HC-SR04?

I'd love to understand the reason for this effect to be able to improve the accuracy of my measurements.

asked Feb 16, 2018 at 17:19
2
  • And how exactly are you calculating the distance? Commented Feb 16, 2018 at 17:54
  • @gre_gor: double distance = duration * 0.03455 (I'll add it to my question.) Commented Feb 16, 2018 at 20:07

1 Answer 1

1

I think it's from the HC-SR04... it uses echos and it seems your bad readings are some kind of echo reflections.

What you should do is either remove the bad readings, or take the average/most common value of the last x readings.

Or throw away values too far off some default (like the last average) and average the rest.

answered Feb 16, 2018 at 17:55
2
  • Sure, I could implement some kind of filter. But actually I want to use it on a moving platform, so I can't repeat any measurement. Commented Feb 16, 2018 at 20:11
  • 2
    No, the spacing is much to small to be multiple reflections. For that to occur, you'd be seeing multiples of the base reading, rather than a distribution around a basic value. Commented Feb 16, 2018 at 21:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.