An ultrasonic ranger sends out an ultrasonic (roughly 40,000Hz) chirp and listens for an echo; it then reports back how long the echo took to return, and you can do some easy math to figure out the distance between the ranger and the surface the sound is bouncing off of.
These devices are quite inexpensive and quite easy to use. They are probably also very annoying to nearby dogs, dolphins, and bats.
Wiring is easy: read the labels provided on the device and wire to the Arduino as follows:
5Von the Arduino
echoare data pins which connect with the pins as described in the sketch below
The ultrasonic ranger, like most small simple digital devices that are Arduino inputs, does not use much current and so it’s fine to run it direclty off of the Arduino’s
5V supply, no current-limiting resistor needed.
Arduino code to talk to the ultrasonic ranger and print distances via the serial monitor
It’s easiest to talk with this device by using the NewPing library. Add this library (and many others) straight through the Arduino IDE by going to the Sketch menu –> Include Library –> Manage Libraries… and then searching for “NewPing.” Below is the NewPingExample sketch, which you can find already in the Arduino IDE by going to File menu –> Examples –> NewPing –> NewPingExample.
Note that the sketch tells you, in lines 7 and 8, which pins it expects the
echo wires to be plugged into. You can modify those lines if you want to use different Arduino pins.
If you wanted to use the distance coming off the sensor in your sketch, note that the command that both makes the ranger device do its pinging, and also reports back the distance value, is simply
(The above code is from NewPing version 1.9.0.)