Introduction
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, providing accurate distance measurements typically ranging from 2 cm to 4 meters. This sensor is widely used in robotics, obstacle detection, and distance measurement applications due to its simplicity, affordability, and reliability.
Common Applications
- Obstacle detection in robotics
- Distance measurement in automation systems
- Liquid level sensing
- Parking assistance systems
- Proximity detection in security 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 the key technical details:
Pin Configuration
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. This 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
- Ensure the sensor is mounted securely and aligned properly for accurate measurements.
- Avoid placing the sensor near ultrasonic noise sources or reflective surfaces that may cause inaccurate readings.
- The sensor has a minimum range of 2 cm; objects closer than this may not be detected accurately.
- Use a resistor divider or level shifter if connecting the Echo pin to a microcontroller operating at 3.3V logic levels.
Example Code for Arduino UNO
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() {
pinMode(trigPin, OUTPUT); // Set Trig pin as output
pinMode(echoPin, INPUT); // Set Echo pin as input
Serial.begin(9600); // Initialize serial communication at 9600 baud
}
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; // Speed of sound is ~0.034 cm/µs
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Wait for 500 ms before the next measurement
}
Notes on the Code
- The
pulseIn() function measures the duration of the HIGH pulse on the Echo pin.
- The speed of sound is approximately 343 m/s, which translates to 0.034 cm/µs.
- The division by 2 accounts for the round-trip time of the ultrasonic wave.
Troubleshooting and FAQs
Common Issues
No Output or Incorrect Readings
- Cause: Improper wiring or loose connections.
- Solution: Double-check all connections and ensure the sensor is powered correctly.
Constant Maximum Distance (400 cm)
- Cause: No object detected within range or object is too close (<2 cm).
- Solution: Ensure there is an object within the sensor's range and avoid placing objects too close.
Fluctuating Readings
- Cause: Environmental noise or reflective surfaces.
- Solution: Use a stable mounting setup and avoid placing the sensor near reflective or vibrating surfaces.
Echo Pin Voltage Too High
- Cause: Direct connection to a 3.3V microcontroller without level shifting.
- Solution: Use a resistor divider or level shifter to step down the voltage.
FAQs
Q: Can the HC-SR04 detect transparent objects?
A: The sensor may struggle to detect transparent objects like glass due to poor ultrasonic wave reflection.
Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: Yes, but you must use a level shifter or resistor divider for the Echo pin to avoid damaging the microcontroller.
Q: What is the maximum sampling rate of the HC-SR04?
A: The sensor requires a minimum delay of 60 ms between measurements, allowing for a maximum sampling rate of approximately 16 Hz.
Q: How do I improve accuracy?
A: Use a stable power supply, avoid environmental noise, and ensure proper alignment of the sensor.