

The HCSR-04 Ultrasonic Sensor, manufactured by Sensor, is a versatile and widely used electronic component designed for distance measurement and object detection. It operates by emitting ultrasonic sound waves and measuring the time it takes for the echo to return after bouncing off an object. This time-of-flight measurement allows for accurate distance calculations.








The HCSR-04 Ultrasonic Sensor is designed for ease of use and reliable performance. Below are its key technical details:
| Parameter | Value |
|---|---|
| Operating Voltage | 5V DC |
| Operating Current | 15 mA |
| Operating Frequency | 40 kHz |
| Measuring Range | 2 cm to 400 cm |
| Accuracy | ±3 mm |
| 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 HCSR-04 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 ultrasonic transmission. |
| 3 | Echo | Echo pin. Outputs a pulse width 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 connect and use the HCSR-04 with an Arduino UNO:
| HCSR-04 Pin | Arduino UNO Pin |
|---|---|
| VCC | 5V |
| Trig | Digital Pin 9 |
| Echo | Digital Pin 10 |
| GND | GND |
// Define pins for the HCSR-04 Ultrasonic Sensor
const int trigPin = 9; // Trigger pin connected to Arduino pin 9
const int echoPin = 10; // Echo pin connected to Arduino 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 cm
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 Distance Measurements
Sensor Not Detecting Objects
Echo Pin Always HIGH or LOW
Q: Can the HCSR-04 detect transparent objects?
A: The sensor may struggle to detect transparent objects like glass, as they may not reflect ultrasonic waves effectively.
Q: What is the maximum angle of detection?
A: The HCSR-04 has a detection angle of approximately 15 degrees.
Q: Can I use the HCSR-04 with a 3.3V microcontroller?
A: The HCSR-04 requires a 5V power supply. Use a level shifter for compatibility with 3.3V logic.
Q: How do I improve accuracy?
A: Use multiple measurements and average the results to reduce noise and improve accuracy.