I recently purchased the Arduino HC-SR04 Ultrasonic Range Finder. You can purchase this item for around £5 and you can use it to calculate the distance from the range finder with an accuracy of 2-400 cm.

Arduino HC-SR04
Arduino HC-SR04

Although fuzzy in the picture the pins are labelled as such. I have also included instructions on how you can wire this to the Arduino.

  • Vcc – Connect this to +5V
  • Trig – Connect this to a digital I/O pin (2)
  • Echo – Connect this to a digital I/O pin (3)
  • Gnd – Connect this to the Gnd

The ultrasound sensor works by sending out a pulse and then measuring the duration of time until a response is heard. This is achieved using the Arduino pulseIn() method. Details of which can be found on this page.

The pulseIn method requires you send a pulse of at least 10 microseconds, so the code I have provided creates this pulse and then converts the duration into cm. This is done using the speed of sound and converting this into cm per milliseconds and then halfing as we only want the distance to the object and not back.

More detailed information on ultrasound can be found here and in particular this image:


The code sample to read the range and print the results via serial cable is shown below:

* Ultrasonic Module HC-SR04
* This sketch reads an estimated distance from the HC-SR04.
* This is done by sending a pulse to the TRIG pin on the HC-SR04 
* and then using the ECHO pin to take a reading. The duration from
* pulse to responce is used to calculate the distance.
* The HC-SR04 has an estimated range 2-400cm.
* http://www.micropik.com/PDF/HCSR04.pdf
* - Nick Horne 2013 -
// The HC-SR04 has 4 pins.
// Connect VCC to +5V on the Arduino.
// Connect Gnd to Gnd on the Arduino. 
// Trig to pin 2 on the Arduino.
const int hcTrigPin = 2;
// Echo to pin 3 on the Arduino.
const int hcEchoPin = 3; 
void setup() {
// Start serial conenction.
// Set up the pins.
pinMode(hcTrigPin, OUTPUT);
pinMode(hcEchoPin, INPUT);
void loop() {
// You need to give the ultrasound a HIGH pulse of 10 microseconds to register.
// See http://arduino.cc/en/Reference/pulseIn
// Give a short LOW pulse beforehand to ensure its a true 'HIGH'.
digitalWrite(hcTrigPin, LOW);
digitalWrite(hcTrigPin, HIGH);
digitalWrite(hcTrigPin, LOW);
// Get the pulse duration (we are reading a HIGH pulse).
long pulseDuration = pulseIn(hcEchoPin, HIGH);
// Pring the results on serial.
Serial.print("Got reading: ");
Serial.print( microsecondsToCentimeters(pulseDuration) );
// Do a short sleep before looping.
// Convert the microsecond duration into centimeters.
long microsecondsToCentimeters(long microseconds) {
// Using the speed of sound 340 m/s or (29) microseconds we can
// Work out how far an object is away using duration / 29.
// However as we are measuing the sound that bounces back, we need
// to half this again.
return microseconds / 29 / 2;

You should get some fairly accurate readings and your serial output should look as follows:

range finder readings
range finder readings
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *