Menu Close

Arduino – Ultrasonic Range sensor HC-SR04

This tutorial of Robo India explain the working concept of Ultrasonic range sensor. This tutorial is based on easy to use Library. Various built in function of the library gives ease of use to HC-SR04 Ultra sonic range sensor.

1. Introduction:

A step by step illustrated tutorial to use Utrasonic Range Sensor HC-SR04 on Arduino. lets see the working of the sensor in an easy way. This sensors transmits ultrasonic waves and receives its echo. We use the time difference between sending and receiving echo. That time interval is used to measure range or distance.This sensor is very useful to construct autonomous robots.

1.1 Ultrasonic Library:

Here is the library to use this sensor. You may download it from here.

Once you have downloaded this library. Extract this and copy the folder to My Documents/Arduino/Libraries or other similar locations. 

2. Required Hardware

Following Hardware will be required to perform this sketch of shift register.

S.No.ItemQuantity
1.R-Board with FTDI or Arduino Board1
2.Ultrasonic Sensor HC-SR041
3.Male to female Jumpers 4

3. Building Circuit

Make following circuit with the help of above mentioned components.

3.1 You may go with Robo India’s R-Board(UNO Compatible)-

or

3.2 You may go with original Arduino UNO Board-

 4. Programming:

Once we are done with circuit part, here is our programme to this circuit. Every command of the following programme is explained in the comment section.

Some salient features of the library.

1. It measures echo time and range.

2. Range it measures in Inch and Centimeter.

3. Maximum range it measure 51 cm.

4. Multiple sensor may be attached.

It has got four functions- All these functions are illustrated in the following sketch.

1. Initializing

2. Find range in cm

3. find range in Inch

4. Find echo time in mili seconds

You may download this code (Arduino Sketch) from here.

/ HC-SR04 Utra sonic ranging module on Arduino
// RoboIndia (www.roboindia.com)

#include 
Ultrasonic ultrasonic(6,7); // (Trig PIN,Echo PIN)

int range_inch = 0; // Variable to store range in Inch
int range_cm = 0;   // Variable to store range in Centi meter
int time_ms = 0;    // Varibale to store echo time in ms
void setup() {
  Serial.begin(9600); // Initializing serial communication.  
}

void loop()
{
  range_inch = ultrasonic.Ranging(INC); // function returns range in Inch.
  range_cm = ultrasonic.Ranging(CM);    // function returns range in cm.
  time_ms = ultrasonic.Timing();        // function returns echo time in ms
  Serial.print(range_inch);
  Serial.println(" Inch" );            // Printing range in Inch
  Serial.print(range_cm);              // Printing range in CM   
  Serial.println(" cm" );             
  Serial.print(time_ms);              // Printing echo time in ms  
  Serial.println(" ms" );
  delay(200);                         // waiting for a while
}

5. Output: 

After uploading this sketch open serial monitor and you will see the following like result.

Note: This sensor works on the concept of trig and echo. So if you cover the sensor with some object, in this situation it will not receive any echo. Thus this situation is considered as the maximum range. 

If you have any query please write us at info@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
http://roboindia.com

Leave a Reply