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:
(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.
double distance = duration * 0.03455
(I'll add it to my question.)