In this article, we will interface ultrasonic sensor HC-SR04 with Arduino and learn about ultrasonic waves and the working of the HC-SR04 sensor. These sensors are mostly used to measure the distance using soundwaves with a frequency above 20 kHz above the human range of hearing. They can be used on a robot to avoid obstacles and prevent them from crashing into an object, reversing/parking sensors used in automobiles, industrial imaging, SONAR, prenatal scanning, ultrasonography, detect flaws in the metal, etc. The sensor consists of a piezoelectric crystal using which converts electric energy into acoustic energy. The soundwaves are then transmitted to fall on an object from where they will get reflected. The reflected waves are used to calculate the time passed which is further used to calculate the distance of the object from the sensor using distance, speed, time formula.

1. CONCEPTS – Ultrasonic Sensor HC-SR04 with Arduino

a. What is Ultrasonic Sensor HC-SR04?

Ultrasonic Sensor; HC-SR04 is a distance measuring sensor, with ultrasonic technology, provides a simple method for measuring distances, where high accuracy combined with a large measurement range is required. It is widely used in robotics applications, safety systems or in place of infrared units, to which it offers higher performance.

 Ultrasonic Sensor HC-SR04 with Arduino
Figure 1: Ultrasonic Sensor HC-SR04

b. Working of an Ultrasonic Sensor HC-SR04

The operation of this sensor is very simple; the sensor has two circular elements placed on the base: the first element called trigger emits an ultrasound that bounces off a surface and is read by the second element called echo. By measuring the round trip time of the signal and dividing it by two it is possible to obtain the distance to the object with a fairly high precision margin, in the order of one centimetre. The system also works in the presence of objects placed up to angles of 30 degrees which makes it even more interesting. The gap in distances that can be monitored by this sensor is from two centimetres to four meters. As for the power supply, the HC-SR04 must be powered at 5V, the classic Arduino voltage.

detection of reflected sound waves using HC-SR04 Ultrasonic Sensor
Figure 2: Detecting Obstacle using HC-SR04

c. Timing Diagram of HC-SR04

The HC-SR04 sensor has 4 pins: Vcc (+ 5V), Trigger, Echo, GND. To activate the sensor, a HIGH pulse is sent on the Trigger pin for at least 10 microseconds. As soon as the Trigger pin is LOW after 10 microseconds; the sensor transmits a burst of ultrasonic waves just after the transmission is complete the Echo pins goes Low to High. Echo pin will stay at HIGH until the ultrasonic waves are reflected by an obstacle and detected by the receiver. Moreover, if the transmitted wave is taking more than 38μs to return, it is discarded considering that no obstacle is detected because time measurements greater than 38μs are not reliable.
Also, to be on the safe side always give a delay of 50 to 60 μs to ensure that there is no interference with the previous or the next measurement.

Timing Diagram fo Ultrasonic Sensor HC-SR04
Figure 3: Timing Diagram of HC-SR04

d. Calculation of Distance

Using an Arduino we trigger the “Trig” pin with HIGH voltage for more than 10 μs. Just after that, we will start the timer which will record the time(t) elapsed until the echo goes from HIGH to LOW.

We know,

Speed of sound, s = 343 m/s
Total time = t μs
Time taken to reach an obstacle, = t/2

Now to calculate the Distance(D)

Distance = speed x time
or, D = 343 m/s x t/2 μs

We know, 1m=100cm and 1s=1000000μs

D =34300/1000000 cm/μs x t/2 μs
D = (t/2)/1000000/34300 cm
D = (t/2)/29.1 cm



Figure nolabel Ultrasonic sensor HC-SR04 with Arduino
Figure 4: Connecting HC-SR04 Ultrasonic sensor with Arduino


Program: Measure distance using Ultrasonic sensor HC-SR04 and print in on the serial monitor.

  This program measure distance using Ultrasonic sensor HC-SR04 and print
  it on the serial monitor.
  Arduino    HC-SR04  | Arduino    HC-SR04
  7          Trig     | 5V         Vcc
  6          Echo     | GND        Gnd

#define trigPin 7                //define Trigger pin
#define echoPin 6                //define Echo pin

void setup()
  Serial.begin(9600);            //Initialize serial port
  pinMode(trigPin,OUTPUT);       //set Trigger pin mode as output pin
  pinMode(echoPin,INPUT);        //set Echo pin mode as input pin

void loop()
  int duration, distance;           
  digitalWrite(trigPin,HIGH);    //set trigger pin to high
  delayMicroseconds(60);         //delay of 60 Microseconds
  digitalWrite(trigPin,LOW);     //set trigger pin to LOW
  duration=pulseIn(echoPin,HIGH);//wait for Echo pin to go HIGH
  distance=(duration/2)/29.1;    //apply formula to calculate 
  Serial.print(distance);        //print distance on Serial Monitor


Leave a Reply