

The HC-SR04 is an ultrasonic distance sensor that uses sonar to measure the distance to an object. It emits ultrasonic waves and calculates the time it takes for the echo to return, enabling accurate distance measurements. This sensor is widely used in robotics, obstacle detection, and distance measurement applications due to its simplicity, affordability, and reliability.








The HC-SR04 sensor is designed for precise distance measurement and operates using ultrasonic sound waves. Below are its key technical details:
| Parameter | Value |
|---|---|
| Operating Voltage | 5V DC |
| Operating Current | 15 mA |
| Measuring Range | 2 cm to 400 cm (4 meters) |
| Measuring Angle | 15 degrees |
| Accuracy | ±3 mm |
| Ultrasonic Frequency | 40 kHz |
| Trigger Input Signal | 10 µs TTL pulse |
| Echo Output Signal | Pulse width 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 pulse width proportional 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 sensor with an Arduino UNO:
// Define pins for the HC-SR04 sensor
const int trigPin = 9; // Trigger 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
pinMode(trigPin, OUTPUT); // Trig pin as output
pinMode(echoPin, INPUT); // Echo pin as 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 before the next measurement
delay(500);
}
No Output or Incorrect Readings
Unstable or Fluctuating Measurements
Sensor Not Detecting Objects
Echo Pin Always HIGH or LOW
Q: Can the HC-SR04 measure distances below 2 cm?
A: No, the sensor's minimum range is 2 cm. Objects closer than this may not be detected accurately.
Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: The HC-SR04 requires a 5V power supply. However, you can use a voltage divider on the Echo pin to safely interface with a 3.3V microcontroller.
Q: How can I improve measurement accuracy?
A: Use the sensor in a stable environment, avoid ultrasonic interference, and ensure proper alignment with the target object.