I have several problems with the ultrasonic range finder HC-SR04. They are connected to the manual time measuring from ultrasonic signal to it’s echo:
- The code often stops to work while waiting for an UP or DOWN edge. I am unsure what is wrong here, maybe the circuit has some bad solder joint.
- Even if it works, the measured distances are not really precise. I expected some inaccuracy because I don’t use a realtime OS, but they are really, really bad. See the image:
First look at the measured distances: they vary greatly, although the sensor was mounted solid facing a wall and nothing moved. A very simple filtering approach (where each new value is only weighted 0.3 as opposed to the previously filtered value with weight 0.7) doesn’t make the values usable; consecutive filtered readings (approximately 12.2 ms lie between them) sometimes still differ by more than 4 cm! If we want to get a speed approximation from this, we really need a more sophisticated method like the altitude sensor fusion that I proposed earlier, otherwise we would get a speed of 4 cm in 12.2 ms, resulting in approximately 3.28 meters per second – very fast, compared to the reality of no movement at all!
Maybe the first point was a hardware failure – I tried another one of my HC-SR04 and it worked much more reliable. But the second point really bugs me. Even if the altitude sensor fusion will work perfectly, the results could still be much better with more precise sensor readings. This is why I ordered a another ultrasonic range finder that has an own timer and an I2C interface: This SRF02, which measures the distances itself and then gives the results. Hopefully I can play around with it soon.