Ping Ultrasonic Range Finder

Use an HC-SR04 ultrasonic sensor to give your project the ability to calculate distance. This popular ultrasonic distance sensor provides stable and accurate distance measurements from 2cm to 450cm. It has a focus of less than 15 degrees and an accuracy of about 2mm.

Connect the VCC and GND pins to a 5V power supply, the trigger input (Trig) pin to a digital output and the echo (Echo) pin to a digital input to your microcontroller. Pulse the trigger (Trig) pin high for at least 10us (microseconds) and then wait for a high level on the echo (Echo) pin. The amount of time the Echo pin stays high corresponds to the distance that the ultrasonic sound has travelled. The quicker the response, the closer the object is.

Hardware Required

Arduino Board
HC-SR04 Ultrasonic Module Ultrasonic Range Finder
hook-up wires


The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

click the image to enlarge

image developed using Fritzing. For more circuit examples, see the Fritzing project page


The sample below is using the NewPing library.  If you are using the Arduino IDE instead of codebender you will need to download the library here and install it.

In the example code below the Trig pin is connected to pin 12 and the Echo to pin 11.  If your pins are connected to different Arduino pins then adjust the code accordingling.

#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200


void setup()

void loop()
    int uS =;
    Serial.print("Ping: ");
    Serial.print(uS / US_ROUNDTRIP_CM);

Want to save some time learning Arduino?
Join the thousands of awesome people to sign up for our
FREE Arduino Video Crash Course!