HC-SR04 Ultrasonic Distance Sensor - Complete Guide & Arduino Code

The HC-SR04 Ultrasonic Distance Sensor is one of the most popular and affordable sensors for measuring distance and detecting objects. Using sound waves to determine distance, this sensor is widely used in robotics, automation, and IoT projects for obstacle avoidance, liquid level measurement, and proximity detection.
In this comprehensive guide, we'll explore how the HC-SR04 works, its technical specifications, and provide ready-to-use Arduino code examples for your projects.
Did You Know?
Ultrasonic sensors work on the same principle as bats and dolphins use for echolocation. They emit high-frequency sound waves (inaudible to humans) and measure the time it takes for the echoes to return, calculating distance based on the speed of sound.
How HC-SR04 Ultrasonic Sensor Works
The HC-SR04 uses ultrasonic waves to measure distance. Here's the working principle:
- You send a 10ÃÂÃÂÃÂõs trigger pulse to the Trigger pin
- The sensor emits 8 cycles of ultrasonic bursts at 40kHz
- The sound waves travel through air and bounce off objects
- The Echo pin goes high and remains high until the echo is received
- You calculate distance based on the time the Echo pin was high
The formula for distance calculation: Distance = (Time ÃÂÃÂÃÂÃÂ Speed of Sound) / 2
HC-SR04 Technical Specifications
Parameter | Specification |
---|---|
Operating Voltage | 5V DC |
Working Current | 15mA |
Measuring Angle | 15ÃÂÃÂÃÂð |
Measuring Range | 2cm - 400cm (0.8in - 13ft) |
Resolution | 0.3cm |
Frequency | 40kHz |
Dimensions | 45mm ÃÂÃÂÃÂÃÂ 20mm ÃÂÃÂÃÂÃÂ 15mm |
HC-SR04 Pinout
- VCC: 5V Power Supply
- Trig: Trigger Input (send 10ÃÂÃÂÃÂõs pulse to start measurement)
- Echo: Echo Output (pulse width proportional to distance)
- GND: Ground
Arduino Code Example - Basic Distance Measurement
Here's a simple Arduino code to get started with the HC-SR04:
const int trigPin = 9;
const int echoPin = 10;
// Variables for measurement
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600); // Start serial communication
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send 10ÃÂÃÂÃÂõs pulse to trigPin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2; // Speed of sound = 0.034 cm/ÃÂÃÂÃÂõs
// Print the distance to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Wait 1 second between measurements
}
Advanced Arduino Code - Object Detection with LED Feedback
This example adds an LED that turns on when an object is detected within a certain range:
const int echoPin = 10;
const int ledPin = 13; // Built-in LED
long duration;
int distance;
int detectionRange = 50; // Distance in cm to trigger detection
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Control LED based on distance
if (distance <= detectionRange && distance >= 2) {
digitalWrite(ledPin, HIGH); // Object detected - turn on LED
Serial.print("Object detected at: ");
Serial.print(distance);
Serial.println(" cm");
} else {
digitalWrite(ledPin, LOW); // No object - turn off LED
Serial.println("No object detected");
}
delay(500); // Shorter delay for more responsive detection
}
Popular HC-SR04 Project Ideas
This versatile sensor can be used in countless applications:
- Obstacle avoidance robots
- Parking assistance systems
- Liquid level monitoring
- Proximity-activated lighting
- Gesture recognition systems
- Smart trash cans (open when hand approaches)
- Distance measurement tools
Pro Tip
For more accurate measurements, take multiple readings and average them. Also, note that temperature affects the speed of sound - for precision applications, you might want to add temperature compensation to your code.
Where to Buy HC-SR04 Ultrasonic Sensor
Ready to add distance sensing capabilities to your projects? Get the HC-SR04 Ultrasonic Distance Sensor from RobuKits here:
Buy HC-SR04 Sensor from RobuKits