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 (typical) |
Measuring Range | 2 cm to 400 cm |
Measuring Angle | 15° |
Accuracy | ±3 mm |
Signal Frequency | 40 kHz |
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 a 5V DC source. |
2 | Trig | Trigger pin. A 10 µs HIGH pulse initiates the ultrasonic signal. |
3 | Echo | Echo pin. Outputs a pulse proportional to the time taken for the echo to return. |
4 | GND | Ground pin. Connect to the ground of the power supply. |
The HC-SR04 sensor is simple to use and can be integrated into a variety of circuits. Below are the steps to use the sensor effectively:
Send a 10 µs HIGH pulse to the Trig pin to trigger the ultrasonic burst.
The sensor emits an 8-cycle burst of 40 kHz sound waves.
The Echo pin outputs a HIGH pulse whose duration corresponds to the time taken for the sound wave to travel to the object and back.
Measure the duration of the HIGH pulse on the Echo pin to calculate the distance using the formula:
[ \text{Distance (cm)} = \frac{\text{Pulse Duration (µs)} \times 0.034}{2} ]
Below is an example Arduino sketch to measure distance using the HC-SR04:
// 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 for the sensor
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 * 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
Unstable or Fluctuating Readings
Echo Pin Always LOW
Q: Can the HC-SR04 measure distances less than 2 cm?
A: No, the minimum measurable distance 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: 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.
By following this documentation, you can effectively integrate the HC-SR04 ultrasonic sensor into your projects for reliable distance measurement.