

The HC-SR04 is an ultrasonic distance sensor that measures distance by emitting ultrasonic waves and calculating the time it takes for the echo to return. It is widely used in robotics, automation, and IoT applications for obstacle detection, distance measurement, and proximity sensing. 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 40 kHz and measuring the time it takes for the echo to return after hitting an object. Below are its key technical details:
| Parameter | Value |
|---|---|
| Operating Voltage | 5V DC |
| Operating Current | 15 mA |
| Measuring Range | 2 cm to 400 cm |
| Accuracy | ±3 mm |
| Operating Frequency | 40 kHz |
| 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 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 pulse whose duration corresponds to the measured distance. |
| 4 | GND | Ground pin. Connect to the ground of the power supply. |
Below is an example of how to use 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 pin modes for Trig and Echo
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(500);
}
pulseIn() function measures the duration of the HIGH pulse on the Echo pin.delay() value in the loop() function to control the measurement frequency.No Output or Incorrect Readings:
Unstable or Fluctuating Measurements:
Sensor Not Detecting Objects:
Echo Pin Always HIGH or LOW:
Q1: Can the HC-SR04 measure distances through transparent materials like glass?
A1: No, the HC-SR04 relies on ultrasonic waves, which do not pass through transparent materials like glass effectively.
Q2: Can I use the HC-SR04 with a 3.3V microcontroller?
A2: Yes, but you need a level shifter or resistor divider to safely interface the Echo pin with the microcontroller.
Q3: What is the maximum measurement frequency of the HC-SR04?
A3: The sensor requires a minimum delay of 60 ms between measurements, allowing for a maximum frequency of approximately 16 Hz.
Q4: How can I improve measurement accuracy?
A4: Use a stable power supply, avoid ultrasonic interference, and ensure the sensor is mounted securely and aligned properly.