Table of Contents
KEY TAKEAWAYS
- Ultrasonic sensors measure distance by sending a sound pulse and measuring the echo return time
- The HC-SR04 sends a 40 kHz pulse on the trigger pin and returns the echo duration on the echo pin
- Distance is calculated as:
distance = (echo_time * speed_of_sound) / 2 - Maximum range is typically 2-4 meters with accuracy of about 3mm
Ultrasonic sensor’s are those sensors which is mainly used to detect object by measuring the distance of the targeted object by sending sound waves of high frequency which is not audible for humans and can be easily found in automobile safety systems i.e. anti-collision system
What is Ultrasonic Sensor
- Ultrasonic sensor is an electronic device which generates ultrasonic sound waves to recognize the object or to estimate the distance between itself and the object
- Ultrasonic sound waves are those waves whose frequencies are beyond audible range ( more than 20kilohertz). This ultrasonic sensors uses ultrasonic sound waves because its traveling speed in the air media is more than that of the speed of the audible sound waves
- Ultrasonic sensors have two transducers one is transmitter and another one is for receiver attached in one head, that transmit back information about objects nearness
- Transmitter converts electrical signal waves into ultrasonic sound waves whereas receiver converts the ultrasonic sound waves into the electrical signal waves
Working of Ultrasonic Sensor
Now let’s see figure 1 and understand the working step by step:

- HC-SR04 (ultrasonic sensor module) has two transducers one is transmitter and other one is receiver
- The transmitter firstly generates the ultrasonic sound waves of 40khz with the help of piezoelectric crystal and releases the original signal in the air medium
- While traveling through the media,(here air)when the ultrasonic sound waves encounters an obstacle or object it bounces back
- And, then the reflected signal is absorbed by the receiver site of the sensor
| NAME | FUNCTIONS |
| VCC(PIN-1) |
|
| TRIG(PIN-2) |
|
| ECHO(PIN-3) |
We can also say ECHO PIN DURATION=TOTAL PROPAGATION TIME |
| GND(PIN-4) |
|
Calculation of Distance
To calculate the actual distance between the sensor and the object we will use the formula DISTANCE=(SPEED*TIME)/2 here at 20 degree Celsius the speed of sound is 340m/sec as the medium of transmission is air or we can also say :-
Timing Diagram and it’s explanation

FROM THE ABOVE TIMING DIAGRAM OF HCSR04 LETS LOOK INTO TWO CASES:-
1. When there is no object encountered while ultrasonic wave traveling:-
- First we will set trig on a HIGH state and send 10us pulse and transmit eight ultrasonic pulses
- And, as no object therefore echo is 38ms (38ms because this is the time out period of echo pin in module HC-SR04 if no object detected)
2. When there is object encountered while ultrasonic wave traveling:-
- Here again when we send 10us pulse and transmit eight ultrasonic pulses
- And, as object detected therefore signal gets reflected back and comes back with the timing of 100us-18ms ,which is then used to determine the distance
Applications of Ultrasonic Sensor
- Vehicle detection for car wash

- People detection such as counting
- Robotic sensing in drones
- Presence detection
- Tank level

Advantages of Ultrasonic Sensor
- Low cost
- Not affected by color or transparency of the object
- Not affected by the dust
- Greater accuracy
- Better sensing capability for all material types
Limitations of Ultrasonic Sensor
- Very sensitive in different temperature and humidity as traveling speed varies accordingly
- Difficult to interpret readings from soft and curved objects
- Needs an media to send ultrasonic waves
- Have a limited range
Program code on Arduino for HC-SR04
Understanding how ultrasonic sensor works is essential for distance measurement and object detection projects.
const int trigPin = 9; //defining the trigger pin
const int echoPin = 10; //defining the echo pin
float duration; //‘duration’ will hold the length of sound wave
float distance; //‘distance’ will store data of distance of object
void setup()
{
pinMode(trigPin, OUTPUT); //declaring trig pin as output pin
pinMode(echoPin, INPUT); // declaring echo pin as the input pin
Serial.begin(9600); //serial communication takes place
}
void loop()
{
digitalWrite(trigPin, LOW); //setting the trig pin in LOW state
delayMicroseconds(2); //for 2microseconds
digitalWrite(trigPin, HIGH);//setting the trig pin in HIGH state
delayMicroseconds(10); //for 10us which emits 8pulses from the transmitter and bounces back once hit by an object in the receiver site
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);//duration is a variable that starts timing once the echo pin is high by using arduino function pulseIn()
distance = (duration*.0343)/2; //calculating distance by putting the value of duration
Serial.print("Distance: "); //printing the results of the distance
Serial.println(distance);
delay(100);
}Related on this site
- For an overview of where ultrasonic fits alongside other distance and motion technologies, see motion and proximity sensors.
- The HC-SR04 trigger/echo measurement is essentially a precise pulse-width capture — polling vs interrupts compares the two reading approaches for time-critical sensors like this.
- For the broader landscape of how an MCU reads any sensor type (ADC, I²C, SPI, 1-Wire), see interfacing sensors with microcontrollers.
Frequently Asked Questions
How does an HC-SR04 ultrasonic sensor work?
The trigger pin is pulsed high for 10 microseconds, the sensor emits an 8-cycle 40 kHz burst, and the echo pin is held high until the burst returns. The high-time of the echo pin equals the round-trip travel time of the ultrasound pulse.
📖 Related: How to improve readings of Ultrasonic Sensor – Temperature and Humidity Compensation

Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.




