Introduction
The HC-SR04 is an ultrasonic distance sensor that uses sonar to measure distances. It emits ultrasonic waves and measures the time it takes for the echo to return, allowing it to calculate the distance to an object. This sensor is widely used in robotics, automation, and IoT applications for tasks such as obstacle detection, distance measurement, and object tracking. Its affordability, ease of use, and accuracy make it a popular choice for hobbyists and professionals alike.
Common Applications:
- Obstacle detection in robotics
- Distance measurement in automation systems
- Liquid level measurement
- Proximity sensing in IoT devices
- Parking assistance systems
Technical Specifications
The HC-SR04 sensor operates by emitting ultrasonic waves at a frequency of 40 kHz and measuring the time it takes for the echo to return. Below are its key technical details:
Pin Configuration and Descriptions
The HC-SR04 sensor has four pins, as described in the table below:
Usage Instructions
How to Use the HC-SR04 in a Circuit
- Power the Sensor: Connect the VCC pin to a 5V power supply and the GND pin to ground.
- Trigger the Sensor: Send a 10 µs HIGH pulse to the Trig pin to initiate a measurement.
- Read the Echo: Measure the duration of the HIGH pulse on the Echo pin. The duration is proportional to the distance of the object.
- Calculate Distance: Use the formula below to calculate the distance:
[
\text{Distance (cm)} = \frac{\text{Pulse Duration (µs)}}{58}
]
Alternatively, for distance in inches:
[
\text{Distance (in)} = \frac{\text{Pulse Duration (µs)}}{148}
]
Important Considerations and Best Practices
- Ensure the sensor is mounted securely and aligned properly for accurate measurements.
- Avoid placing the sensor near ultrasonic noise sources, as they may interfere with its operation.
- Use a capacitor (e.g., 10 µF) across the VCC and GND pins to stabilize the power supply.
- The sensor has a minimum range of 2 cm. Objects closer than this may not be detected accurately.
- For best results, ensure the object being measured has a flat, hard surface to reflect the ultrasonic waves.
Example: Using HC-SR04 with Arduino UNO
Below is an example code to interface 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 trigPin as output and echoPin as input
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(100);
}
Notes:
- Ensure the Trig and Echo pins are connected to the correct digital pins on the Arduino.
- Use a resistor divider or logic level shifter if interfacing with a 3.3V microcontroller.
Troubleshooting and FAQs
Common Issues and Solutions
No Output or Incorrect Readings:
- Ensure the sensor is powered with 5V and the connections are secure.
- Verify that the Trig pin is receiving a 10 µs HIGH pulse.
- Check for obstacles or noise sources that may interfere with the ultrasonic waves.
Unstable or Fluctuating Measurements:
- Add a capacitor (e.g., 10 µF) across the VCC and GND pins to stabilize the power supply.
- Ensure the object being measured has a flat, hard surface for better reflection.
Sensor Not Detecting Close Objects:
- The HC-SR04 has a minimum range of 2 cm. Objects closer than this may not be detected accurately.
Echo Pin Stays HIGH Indefinitely:
- This may occur if no object is detected within the sensor's range. Ensure there is an object within 400 cm.
FAQs
Q: Can the HC-SR04 measure distances through transparent materials like glass?
A: No, the HC-SR04 relies on ultrasonic waves, which do not pass through transparent materials like glass effectively.
Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: Yes, but you will need a logic level shifter or a resistor divider for the Echo pin to avoid damaging the 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.
Q: How can I improve measurement accuracy?
A: Use a stable power supply, avoid ultrasonic noise sources, and ensure proper alignment of the sensor.