HC-SR04 Ultrasonic sensor Working, Arduino tutorial & projects

The HC-SR04 ultrasonic sensor working principle is based on the speed of sound or time taken by the sound waves to travel a certain distance. It measures the distance of an object from its surface by emitting and receiving sound waves.

There are many kinds of ultrasonic sensors in the market but the most popular is HC-SR04. HC-SR04 Ultrasonic sensor working, Pinout, and Arduino tutorial is given in detail below.

HC-SR04 Ultrasonic Sensor:

HC-SR04 is a simple ultrasonic sensor that can measure distance up to 400 cm. The sensor has 4 onboard pins that connect to a microcontroller like Arduino.

HC-SR04 can be used with Arduino for projects like distance measurement. HCR04 Ultrasonic sensor working principle is similar to SONAR or Sound Navigation and Ranging. SONAR is used by the ships to navigate the objects undersea. 

HCSR04 Ultrasonic Sensor
HCSR04 Ultrasonic Sensor

Click here to Learn More about SONAR

The ultrasonic sensor, also known as a distance sensor measures the distance of an object from its surface by emitting and receiving sound waves. The transmitter transmits the sound waves whereas the Receiver receives these transmitted sound waves reflected back from the object.

Before jumping into the HC-SR04 sensor working section, let’s look at its pinout.

Pinout of HCSRO4 Ultrasonic Sensor:

HC-SR04 pinout has 4 pins. Starting from left: VCC, TRIGGER, ECHO, and GND

HCSR04 Ultrasonic sensor pinout
HCSR04 Ultrasonic sensor pinout
Pin NameFunction
VCC  Powers the Ultrasonic sensor.
TRIGGER This pin initiates the transmission of the sound waves.
ECHO Becomes low as soon as the transmitted sound waves hit the Receiver.
GROUND Goes to the circuit’s ground.
Read Also: Beginners Guide to IR Sensors

Now let’s see how the HC-SR04 distance Sensor works and measures the distance of an object.

HCSR04 Ultrasonic Sensor Working in detail:

How HC-SR04 ultrasonic Sensor works
How HC-SR04 ultrasonic Sensor works

As soon as the Trigger pin is set High(given 5 V) using a microcontroller(like Arduino), sound waves are transmitted from the transmitter. 

Note A: At the same time, the Echo pin automatically becomes high(gives 5 V as output). Let’s say the time at this point is t1.

Note B: The speed of the sound wave is 343 m/s

These sound wave travels through the air and gets reflected back by the object in front of it. And as soon as the waves are received back by the receiver, the Echo pin becomes low. Let’s call the time at this instance t2.

Now in order to find out the distance traveled by the waves, first we have to measure the total time for which the echo pin was high(Arduino does this for us).

In other words, the echo was high for exactly the same time it took the waves to get back to the receiver.

i.e, t2 - t1 = time for which the Echo pin remains high


Ttotal = t2 - t1 (total time taken by the sound waves to get back to the sensor after reflecting back from the object)

By distance-time-speed formula, we can calculate the total distance traveled by the wave(speed is constant).

Speed = Distance / Time 


Distance = Speed X Time

So simply multiply the total calculated time by the speed of sound. This gives us the total distance traveled by the sound waves. 

Dtotal = (T2 - T1) X Speed of sound 

This(total distance) is actually a two-way distance. But we want to measure the one-way distance. And since the total distance is actually double the one-way distance, we divide it by two to get the one-way distance from the sensor to the object. 

Done-way = Dtotal / 2

So this was the complete working of the HC-SR04 ultrasonic sensor and how it calculates the distance of the object from the sensor is calculated.

How HC-SR04 measures the distance?

Let’s understand this once again by an example:
Let’s suppose there is an object in front of the sensor at a 10 Cm distance. Sound waves are transmitted by the transmitter, hit the object, and then received by the receiver.

Transmitting ultrasonic waves
Receiving ultrasonic waves

The total time between these two events is calculated using the echo pin as mentioned earlier. Now using this formula oneway distance is calculated: Distance = Time * Speed of Sound/2

Distance measurement using HC-SR04 with Arduino:

Let’s interface the ultrasonic sensor with Arduino now. I am using Arduino UNO here but you can use any Arduino board. Make connections as shown in this circuit.

HCSR04 Ultrsonic sensor connections with Arduino
HCSR04 Ultrasonic sensor connections with Arduino

1) Connect Vcc and Gnd pin of the ultrasonic sensor to 5 v and GND pin of Arduino respectively.

2) Connect trigger and echo pin to any digital input /op pins. I am connecting the trigger pin to digital pin 6 and echo to digital pin 7.

HC-SR04 Distance measurement program:

Upload the program given below to your Arduino board.

const int trig = 6;
const int echo = 7;

long totaltime;
int distance;

void setup() {
pinMode(trig, OUTPUT); 
pinMode(echo, INPUT);


void loop() {

digitalWrite(trig, LOW);

digitalWrite(trig, HIGH);
digitalWrite(trig, LOW);

totaltime = pulseIn(echo, HIGH);

distance= totaltime*0.034/2;

Serial.print("Distance: ");


Code Explanation:

The code for measuring distance using an ultrasonic sensor is very simple. First, the trigger pin is set high for 10 microseconds. Then time for which the echo pin remains high is calculated using the pulse-in function. Finally, distance in centimeters is calculated using this formula:

distance= totaltime*0.034/2;

Now upload the code to Arduino and then open the serial monitor. Here you can see the calculated distance of the object from the surface of the ultrasonic sensor. Let’s move the object and see how accurate is the measurement.

Distance measurement using HC-SR04 with Arduino
Distance measurement using HC-SR04 with Arduino

Arduino HC-SR04 Ultrasonic Sensor projects

Now apart from measuring the distance you can make many cool projects using an ultrasonic sensor like:

1)Arduino based blind stick

Made by Circuit digest:

2) Arduino Radar system

by How to Mechatronics

3) Smart Dustbin

by Indian life hacker


Leave a Comment