

The HC-SR04 is an ultrasonic distance sensor that uses sonar to measure distances. It emits ultrasonic waves and measures the time it takes for the echo to return, allowing it to calculate the distance to an object. This sensor is widely used in robotics, automation, and IoT applications for tasks such as obstacle detection, distance measurement, and object tracking. Its affordability, ease of use, and accuracy make it a popular choice for hobbyists and professionals alike.








The HC-SR04 sensor operates by emitting ultrasonic waves at a frequency of 40 kHz and measuring the time it takes for the echo to return. Below are its key technical details:
| Parameter | Value |
|---|---|
| Operating Voltage | 5V DC |
| Operating Current | 15 mA (typical) |
| Operating Frequency | 40 kHz |
| Measuring Range | 2 cm to 400 cm |
| Measuring Angle | 15° |
| Resolution | 0.3 cm |
| Trigger Input Signal | 10 µs TTL pulse |
| Echo Output Signal | TTL pulse proportional to distance |
| Dimensions | 45 mm x 20 mm x 15 mm |
The HC-SR04 sensor has four pins, as described in the table below:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply pin. Connect to 5V DC. |
| 2 | Trig | Trigger pin. Send a 10 µs HIGH pulse to initiate distance measurement. |
| 3 | Echo | Echo pin. Outputs a HIGH pulse whose duration corresponds to the measured distance. |
| 4 | GND | Ground pin. Connect to the ground of the power supply. |
Below is an example code to interface the HC-SR04 with an Arduino UNO:
// Define pins for the HC-SR04 sensor
const int trigPin = 9; // Trig pin connected to digital pin 9
const int echoPin = 10; // Echo pin connected to digital pin 10
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set trigPin as output and echoPin as input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Send a 10 µs HIGH pulse to the Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the HIGH pulse on the Echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float distance = duration / 58.0;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait for a short period before the next measurement
delay(100);
}
No Output or Incorrect Readings:
Unstable or Fluctuating Measurements:
Sensor Not Detecting Close Objects:
Echo Pin Stays HIGH Indefinitely:
Q: Can the HC-SR04 measure distances through transparent materials like glass?
A: No, the HC-SR04 relies on ultrasonic waves, which do not pass through transparent materials like glass effectively.
Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: Yes, but you will need a logic level shifter or a resistor divider for the Echo pin to avoid damaging the microcontroller.
Q: What is the maximum range of the HC-SR04?
A: The maximum range is 400 cm (4 meters), but accuracy may decrease at longer distances.
Q: How can I improve measurement accuracy?
A: Use a stable power supply, avoid ultrasonic noise sources, and ensure proper alignment of the sensor.