1. Introduction
The HC-SR04 is a popular, low-cost ultrasonic sensor used for non-contact distance measurement. It uses sonar—similar to how bats and dolphins navigate—to determine the distance to an object. It is highly favored in the maker community because it is easy to use, requires only two digital pins, and provides stable readings between 2cm and 400cm.
2. Components Required
To build this project, you will need:
Arduino Board (Uno, Nano, or Mega)
HC-SR04 Ultrasonic Sensor
Breadboard
Jumper Wires (Male-to-Male)
USB Cable (to connect Arduino to your PC)
3. Circuit Diagram & Working
The sensor has four pins: VCC (5V), Trig (Trigger), Echo (Receiver), and GND (Ground).
How it Works:
Triggering: The Arduino sends a 10-microsecond high pulse to the Trig pin.
Sonic Burst: The sensor responds by emitting an 8-cycle burst of ultrasound at 40 kHz.
Echo Detection: Once the burst is sent, the Echo pin goes HIGH. It stays high until the sound bounces off an object and returns to the sensor.
Timing: The Arduino measures the duration the Echo pin stayed HIGH. We then use the speed of sound ($0.034 \text{ cm/μs}$) to calculate the distance.
4. The Code
Copy and paste the following code into your Arduino IDE:
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin (returns travel time in microseconds)
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Print results
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Code Working Breakdown:
digitalWrite(trigPin, HIGH): Initiates the "ping."pulseIn(echoPin, HIGH): This is a built-in Arduino function that waits for the pin to go HIGH, starts a timer, and stops it when the pin goes LOW. It returns the time in microseconds.duration * 0.034 / 2: Since distance is $\text{Speed} \times \text{Time}$, and the sound traveled to the object and back, we divide by 2 to get the actual distance to the object.
5. Pro Tips for Better Accuracy
Avoid Soft Surfaces: Objects like sponges or thick fabric absorb sound waves rather than reflecting them, resulting in "0" or erratic readings.
Angle Matters: If the sensor is at a steep angle ($>15^\circ$) relative to the object, the sound might bounce away and never return.
Temperature Compensation: The speed of sound changes with temperature. For extreme precision, use a DHT11 sensor to adjust the $0.034$ constant in your code based on the current air temperature.
6. Common Uses
Obstacle Avoidance: Essential for autonomous robots to stop before hitting walls.
Liquid Level Sensing: Measuring the depth of water in a tank (non-contact).
Parking Sensors: Helping cars (or DIY toy cars) detect how close they are to a curb.
Interactive Art: Triggering lights or sounds when someone walks past a specific point.
7. Conclusion
The HC-SR04 is the "gold standard" for entry-level distance sensing. Its simplicity and 5V compatibility make it the perfect starting point for any robotics or automation enthusiast.