How an Ultrasonic Sensor Works | HC-SR04 Working Principle

Hello Readers in this article we will see how an ultrasonic sensor works, The other name for this useful arduino module is Hc-Sr04 and can be found in arduino projects.
If you think of making an obstacle avoidance robot or a arduino toll gate system then this module is useful.
In this detailed article we will see HC-SR04 pin diagram, Principle behind its working and physics.
Consider checking our other blog on how it works on various other arduino related components.
Introduction to Ultrasonic Sensors
Imagine a bat flying in complete darkness, It effortlessly avoids obstacles using sound waves.
Ultrasonic sensors work on a similar principle that acts as the “eyes” of many modern electronic devices and robotics projects.

The HC-SR04 is one of the most popular ultrasonic sensors in the Diy maker community, providing accurate distance measurements at an inexpensive price point.
How does ultrasonic sensor work
Ultrasonic sensors operate on a simple principle known as echolocation and it follows these steps.
Sound Wave Generation
The sensor’s transmitter (Trig pin) emits high-frequency sound waves at 40 kHz which is far beyond human hearing range.
Wave Propagation
These sound waves travel through the air at approximately 343 meters per second (considering room temperature).
Echo
When the waves hit an object, they bounce back to the sensor’s receiver (Echo pin on Board).
Time Measurement
The sensor measures the time difference between sending and receiving the signal.
This process happens incredibly quickly in terms of microseconds allowing for real-time distance measurements.
Distance Calculation using HC-SR04
The distance calculation involves several formulas
HC-SR04 distance calculation formulas
Distance = (Speed of Sound × Time of Flight) ÷ 2
Expanded Formula (with temperature consideration)
Distance = (331.4 + 0.606 × Temperature) × Time of Flight ÷ 2
Where
- Distance will be in meters
- Speed of Sound is in meters per second
- Time of Flight in seconds
- Temperature is in °C
The division by 2 is because the time measured includes both the to and from the object.
ultrasonic sensor power consumption HC-SR04
The HC-SR04 sensor is designed for efficient operation with the following specs
- Operating Voltage is 5V DC
- Working Current 15mA
- Working Frequency is Max 40kHz
- Maximum Range 4m(tested)
- Minimum Range 2cm(lowest)
- Measuring Angle is15 degrees
- Trigger Input Signal is 10µs TTL pulse
- Power Consumption
- On Standby Current: < 2mA
- With Working Current: 15mA
Ultrasonic Sensor and Arduino Projects Codes
Few examples with Ultrasonic sensor and arduino projects using arduino.
Basic Code structure on Arduino IDE used in ultrasonic sensor projects.
#define TRIG_PIN 9
#define ECHO_PIN 10
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Clear trigger pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Send 10µs pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure duration of echo pulse
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance
float distance_cm = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(100); // Wait before next measurement
}
Useful Arduino Projects with HC-SR04
1. Smart Parking Assistant
In this project ultrasonic sensor can be used with arduino uno or nano in making a parking assistant.
Buzzer to indicate the obstacle while the vehicle is parking.
// Distance thresholds
const int SAFE_DISTANCE = 100; // cm
const int WARNING_DISTANCE = 50;
const int DANGER_DISTANCE = 20;
void setup() {
// Previous setup code...
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
}
void loop() {
float distance = measureDistance();
if (distance > SAFE_DISTANCE) {
setLEDs(0, 0, 1); // Green
} else if (distance > WARNING_DISTANCE) {
setLEDs(0, 1, 0); // Yellow
} else {
setLEDs(1, 0, 0); // Red
}
}
2. Automatic Door Opener
When an obstacle(hand/human) is detected by hc-sro4 the servo can be used to open and close the door.
The example code is given below and you can use arduino nano or Uno for this project.
#define SERVO_PIN 11
#define DISTANCE_THRESHOLD 50 // cm
#include <Servo.h>
Servo doorServo;
void setup() {
doorServo.attach(SERVO_PIN);
// Previous setup code...
}
void loop() {
float distance = measureDistance();
if (distance < DISTANCE_THRESHOLD) {
doorServo.write(90); // Open door
} else {
doorServo.write(0); // Close door
}
delay(100);
}
3. Water Level Monitor
Imagine you want to check the water level inside a small water tank this sensor serves the purpose.
You can use a small buzzer that makes a sound when Max water level is detected.
#define WATER_FULL 5 // cm
#define WATER_LOW 20 // cm
#define BUZZER_PIN 12
void loop() {
float waterLevel = measureDistance();
if (waterLevel <= WATER_FULL) {
// Tank full
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
} else if (waterLevel >= WATER_LOW) {
// Water level low
for(int i = 0; i < 3; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
}
}
delay(1000);
}
Advantages and Disadvantages of HC-SR04
Advantages of ultrasonic sensor
- Non-Contact Measurement
- Allows distance measurement without physical contact
- Reduces wear and tear on the sensor
- Cost-Effectiveness
- Inexpensive
- Widely available and easy to replace
Disadvantages of ultrasonic sensor
- Environmental Limitations
- Performance affected by temperature and humidity
- May not work well in vacuum environments
- Material Factors
- Soft materials may absorb sound waves
- Angled surfaces can deflect waves away from sensor
Hope you now know How an Ultrasonic Sensor Works and about hcsr04 sensor module, There are many useful projects that you can build with this.
Don’t miss to check my other projects that was built using hcsr04.