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.

The circuit

Ultrasonic ranger schematic. Inside of a horizontal rectangle are two circles, representing the ultrasonic transmitter and receiver. Four labeled wires stick out of the bottom center of the rectangle: VCC, trigger, echo, and ground.

Wiring is easy: read the labels provided on the device and wire to the Arduino as follows:

  • VCC: 5V on the Arduino
  • trigger and echo are data pins which connect with the pins as described in the sketch below
  • GND: ground

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 trigger and echo wires to be plugged into. You can modify those lines if you want to use different Arduino pins.

    // ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

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 sonar.ping_cm().

(The above code is from NewPing version 1.9.0.)