Ultrasonic distance sensors

Submitted by james on
HC-SR04 ultrasonic sensor

The HC-SR04 ultrasonic distance module is an inexpensive device to detect distances with centimeter accuracy, up to a few meters.

The device emits ultrasonic pulses of sound and listens for an echo. The echo time depends on the distance of the object that reflected the sound.

As you might expect, ultrasonic sensors work best when directed perpendicular to large, solid, flat surfaces (like walls) which readily reflect sound. They are be much less effective at detecting soft objects like carpets, pillows, and animals. They also do not reliably detect narrow objects such as the legs of chairs and tables. Ultrasound is highly directional and more "beam-like" than audible frequencies of sound. At shallow angles, the beam may glance off the object and bounce around the room before it reaches the sensor, giving spurious readings.

Given all of the caveats above, ultrasonic distance sensors work surprisingly well, giving measurements that are accurate to within a couple of centimeters under a variety of conditions.

To use the HC-SR04 in your Arduino sketches, you can use a library called NewPing: https://bitbucket.org/teckel12/arduino-new-ping/downloads/

JSN-SR04T

The JSN-SR04T is functionally very similar to the HC-SR04, except it is waterproof and does not give accurate readings at very close ranges. People use it for applications like monitoring their sump pump.

Achieving stable readings

An ever-present problem with measuring things in the real world is noise. Noise is particularly problematic if your machine makes drastic adjustments in response to sensor readings (like a robot slamming on the brakes or turning around).

One way to "smooth" a noisy sensor is by taking multiple readings and averaging them together. The averaging method used is usually the median because it is less susceptible to outliers than the mean is.