HC-SR 04: An Ultrasonic sensor


Ultrasonic sensors, with the HC-SR04 as a prime example, have revolutionized distance measurement and object detection. This comprehensive guide delves into the intricate workings of ultrasonic sensors, highlighting their principles, applications, and seamless integration with platforms like Arduino.

The HC-SR04 is an ultrasonic sensor that is used to measure the distance to an object. It works by emitting a high-frequency sound wave and then measuring the time it takes for the wave to return. The distance to the object is then calculated based on the speed of sound. The module is known for its accuracy and ease of use. It has become a cornerstone in projects involving distance measurement, object detection, and more.

How it works:

The HC-SR04 ultrasonic sensor uses a principle called echolocation. Echolocation is a method used by animals such as bats and dolphins to navigate and find food. It works by sending out a sound wave and then listening for the echo of the sound wave. The time it takes for the echo to return is used to calculate the distance to the object.

The HC-SR04 ultrasonic sensor works in a similar way. The transmitter emits a sound wave at a frequency of 40 kHz. This sound wave travels through the air and is reflected off of the object. The receiver then receives the sound wave and generates a pulse on the echo pin. The length of the pulse is proportional to the time it took for the sound wave to travel to the object and back.

The speed of sound in air is 340 meters per second. So, if the time it takes for the sound wave to travel to the object and back is 1 millisecond, then the distance to the object is 0.17 meters (or 17 centimeters).

The HC-SR04 ultrasonic sensor has a maximum range of 400 centimeters. This means that it can measure the distance to objects that are up to 4 meters away. However, the accuracy of the sensor will decrease as the distance to the object increases.

The HC-SR04 ultrasonic sensor is also affected by the ambient conditions. The speed of sound in air changes with temperature and humidity. So, the distance measurement will be more accurate in a controlled environment.

Here are some additional things to keep in mind when using an HC-SR04 ultrasonic sensor:

  • The sensor should be mounted so that the transmitter and receiver are facing each other.

  • The sensor should be placed in a clear area, without any obstacles between the sensor and the object.

  • The sensor should be used in a controlled environment, with a consistent temperature and humidity.

Measuring Distance with Ultrasonic Sensors:

Ultrasonic sensors operate by emitting sound waves towards an object and then analyzing the time it takes for the waves to return after reflecting off the object. This time interval is crucial for calculating the distance between the sensor and the object. The formula employed for this calculation is D = ½ T x C, where D signifies the distance, T represents the time interval, and C denotes the speed of sound, approximately 343 meters per second. To illustrate, imagine an ultrasonic sensor directed at a box. If it takes 0.025 seconds for the emitted sound waves to bounce back, the calculated distance from the ultrasonic sensor to the box would be:

D = 0.5 x 0.025 x 343

which yields an approximate value of 4

Pin configuration:

The HC-SR04 ultrasonic sensor has four pins: Vcc, Trigger, Echo, Ground.

The Vcc pin powers the sensor, usually with +5V. The Trigger pin is an input pin that triggers the ultrasonic sound pulses. The Echo pin is an output pin that produces a pulse when the reflected signal is received. The length of the pulse is proportional to the time it took for the transmitted signal to be detected.

The Ground pin is connected to the ground of the system. The four pins are connected to a breadboard and then connected to an Arduino using jump wires. The Vcc and Ground pins go to the 5V and GND pins on the Arduino. The Trigger and Echo pins go to any digital Arduino pin.

How to connect with Arduino

Connect the pin as shown in the figure, and upload the below code to Arduino UNO


  • Distance Measurement: Ultrasonic sensors are highly accurate in measuring distances, making them suitable for robotics and automation.

  • Object Detection: They are used to detect the presence of objects in various scenarios, from parking systems to security applications.

  • Flow Metering: Ultrasonic flow meters utilize these sensors to measure fluid flow rates in pipelines.

  • Level Sensing: They help measure fluid levels in tanks and containers.

  • Collision Avoidance: Ultrasonic sensors are integral to collision avoidance systems in vehicles.

  • Weather Monitoring: They are used in weather stations for wind speed and direction measurement.

  • Automation: Ultrasonic sensors contribute to automating processes in industries like manufacturing and agriculture.