

The HC-SR04 is an ultrasonic distance sensor that uses sonar to measure the distance to an object. It emits a high-frequency sound wave and measures the time it takes for the echo to return, allowing it to calculate the distance based on the speed of sound. This sensor is widely used in robotics, automation, and obstacle detection systems due to its accuracy, affordability, and ease of use.








The HC-SR04 sensor is designed for precise distance measurement and operates within a specific range of environmental conditions. Below are its key technical details:
| Parameter | Value |
|---|---|
| Operating Voltage | 5V DC |
| Operating Current | 15 mA |
| Measuring Range | 2 cm to 400 cm (0.02 m to 4 m) |
| Measuring Angle | 15° |
| Accuracy | ±3 mm |
| Frequency | 40 kHz |
| 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. Sends a 10 µs pulse to initiate distance measurement. |
| 3 | Echo | Echo pin. Outputs a pulse proportional to the distance of the detected object. |
| 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; // 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
pinMode(trigPin, OUTPUT); // Trig pin as output
pinMode(echoPin, INPUT); // Echo pin as input
}
void loop() {
// Send a 10 µs pulse to the Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on the Echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
float distance = (duration * 0.034) / 2;
// 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
Fluctuating Distance Measurements
Sensor Not Detecting Objects
Echo Pin Always HIGH
Q: Can the HC-SR04 measure distances through transparent materials like glass?
A: No, the HC-SR04 relies on sound waves, which do not pass through transparent materials like glass effectively.
Q: What is the maximum sampling rate of the HC-SR04?
A: The HC-SR04 requires approximately 60 ms for each measurement, allowing for a maximum sampling rate of about 16 measurements per second.
Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: While the HC-SR04 operates at 5V, it can be interfaced with a 3.3V microcontroller using a voltage divider on the Echo pin to protect the microcontroller's input.
Q: Does temperature affect the accuracy of the HC-SR04?
A: Yes, the speed of sound varies with temperature. For precise measurements, consider compensating for temperature variations.