Ultrasonic Distance Sensor – HC-SR04

Ultrasonic Distance Sensor – HC-SR04 is a powerful sensor used for a variety of applications. It can detect objects, measure distances, and even measure temperature. In this blog post, we will discuss the working of the sensor and its applications.

Overview of the HC-SR04 Ultrasonic Sensor

The HC-SR04 Ultrasonic Sensor is an efficient and effective device that can accurately detect the presence and distance of objects. The features of the HC-SR04 ultrasonic sensor are easy installation, low power consumption, measurement accuracy, and reliability. This sensor is a favorite among makers, engineers, and hobbyists.

Ultrasonic Distance Sensor - HC-SR04

The HC-SR04 can accurately measure distances from 2cm to 4 meters with a resolution of 3mm. The detection angle of the HC-SR-4 sensor is 15 degrees, and, as a result, it can reliably recognize the object even in challenging scenarios. Thus, the HC-SR04 Ultrasonic Sensor is a good choice for any project requiring reliable distance readings.

How does HC-SR04 Ultrasonic Distance Sensor Work?

When the trigger pin is set HIGH for 10µs, the sensor generates and transmits an ultrasonic burst of eight pulses at 40 kHz. The pattern of 8-pulse is specially designed so that the receiver can recognize the transmitted pulses from ambient ultrasonic noise.

HC-SR04 Ultrasonic Distance Sensor working

These eight ultrasonic pulses travel through the air after remitting from the transmitter. During the same time, the echo pin goes HIGH to initiate the echo-back signal. If those eight pulses do not reflect back, then the echo signal times out and goes low after 38ms (38 milliseconds). Thus, a pulse of 38ms indicates no obstruction within the range of the sensor.

If the pulses are reflected back, the echo pin goes low on receiving the signal back. The low signal on the echo pin generates a pulse, whose width varies from 150 µs to 25 ms depending on the time taken to receive the signal.

HC-SR04 Ultrasonic Distance Sensor pulses

Calculating the Distance

We can calculate the distance from the reflected object by the width of the received pulses. The simple distance formula can be used for calculating distance.

Distance = Speed X Time

Let there be an object at an unknown distance in front of the sensor, and we receive a pulse of 400 µs on the echo pin.

The speed of sound is 340 m/s or 0.034 cm/µs. If the distance between the object and sensor is “d,” then the total distance traveled by the pulse receiving at the echo pin is “2d”.

Thus, from the above formula, the object is 6.8 centimeters from the sensor.

Technical Specifications of HC-SR04

Operating VoltageDC 5V
Operating Current15mA
Operating Frequency40KHz
Max Range4m
Min Range2cm
Ranging Accuracy3mm
Measuring Angle15 degree
Trigger Input Signal10µS TTL pulse
Dimension45 x 20 x 15mm

HC-SR04 Ultrasonic Sensor Pinout

The pinout of the HC-SR04 sensor is given below.

VCC pin is the power supply pin of the HC-SR04 ultrasonic sensor. This pin is connected to the 5V output from Arduino.

Trig (Trigger) pin triggers ultrasonic sound pulses. The sensor initiates an ultrasonic burst when this pin is set to high for 10µs.

The echo pin goes high after initiation of the ultrasonic burst and remains in the same state until the sensor receives an echo. When the echo pin receives an echo pulse, it goes low. We can calculate the distance by measuring the time the echo pin stays high.

GND is the ground pin, and it is connected to the ground of the Arduino.

Wiring an HC-SR04 Sensor to an Arduino

  1. Place the sensor on the breadboard.
  2. Connect the VCC pin to the 5 V pin on the Arduino.
  3. Connect the GND pin to the ground pin on the Arduino.
  4. Connect trig and echo pin to digital pin # 9 and #10, respectively


  1. Open the Arduino IDE software on your computer.
  2. Coding in the Arduino language will control your circuit.
  3. Open a new sketch File by clicking New.

HC-SR04 Ultrasonic Sensor Arduino Code

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   digitalWrite(pingPin, HIGH);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print("in, ");

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;

Applications of HC-SR04

Robotic Navigation System

The robotic navigation system directs whenever an obstacle or interference comes in its path. The HC-SR04 ultrasonic sensor detects any obstacle in front of it and sends a command to the robot’s microcontroller.

Burglar Alarms

The HC-SR04 Ultrasonic sensor detects the object if it comes in between the transmitted ultrasonic waves. Therefore, it can be used and installed near the door to detect the entry of a person. Thus, we can use it as a burglar alarm.

Ultrasonic Anemometers

Ultrasonic Anemometers can detect the wind speed and direction. The 2D anemometer measures the horizontal component, while a 3D anemometer also measures the vertical component of wind.


HC SR04 ultrasonic sensor is a very powerful and versatile sensor, and it can be used in many applications by writing different Arduino codes as per the desired applications.

Author- Ayushi Vyas

Valuable Resources- You may Refer:

  1. Library Installation
  2. Arduino Projects

Leave a Comment