HC-SR04 Ultrasonic Sensor Pinout, Working & Arduino 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 projects are given in detail below.

What is 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. HC-SR04 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.

HC-SR04 Pinout

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.
This table shows the functions of each pin on HC-SR04
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.

How does HC-SR04 works?

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

Step 1: As soon as the Trigger pin is set High(programmed), 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 instance is t1.

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

Step 2: 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.

Step 3: 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

OR

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

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

Speed = Distance / Time 

OR 

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 

Step 5: 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

Of course, this calculation is done by the Arduino itself. We did this manually to show you how the distance is calculated.

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

How to measure distance using HC-SR04 with Arduino?

To make the distance measurement project using an HC-SR04 ultrasonic sensor, I am using Arduino UNO here. You can use any other Arduino board.

First, connect the sensor with Arduino as shown below.

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

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

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.

Arduino 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);
Serial.begin(9600); 

}

void loop() {

digitalWrite(trig, LOW);
delayMicroseconds(2);

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

totaltime = pulseIn(echo, HIGH);

distance= totaltime*0.034/2;

Serial.print("Distance: ");
Serial.println(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

Leave a Comment