filter for ultrasound sensor

Hi, I am learning to use a sonar ultrasound sensors, it seems the sensor data fluctuate a lot around the designated distance, for example, when I want to detect object within 12 inches, at the threshold around 12" the sensor data will bounces around 12.xxxxx and 11.xxxx inches. Do I need a filter of some sort to give a more stable data output? Thx.

use a running average.

see this thread http://forum.arduino.cc/index.php?topic=356312.0

You could try using the "NewPing" library. It has a 'ping_median' method, which averages the value taken over a number of readings. You can detemine the number of readings to be averaged, default=5.

Another good method is to set up hysteresis in your code.

OldSteve: You could try using the "NewPing" library. It has a 'ping_median' method, which averages the value taken over a number of readings. You can detemine the number of readings to be averaged, default=5.

Another good method is to set up hysteresis in your code.

Yes, using median mode in New Ping (avarage values of 5), or your can write a bit more complex code to analyze the date like I did. Read 5 times (or more, up to you) distance1 (d1) ; d2; d3; d4; d5... Find value/s that suddenly bigger than previous value. Remove them. Recalculate average. You should output value to monitor to find the best different. In my case: 100cm 97cm 300cm 101cm 102cm. Remove 300cm data, then you are fine. Hope it helps.