HC-SR04 Ultrasonic Sensor

The HC-SR04 is a popular ultrasonic sensor used for measuring distances by emitting ultrasonic waves and detecting their reflections. It is widely used in robotics, automation, and obstacle detection projects.
Key Features:
Power Supply: 5V DC
Operating Current: 15mA
Measuring Angle: <15°
Operating Frequency: 40kHz
Range: 2 cm to 400 cm
Accuracy: ±0.3 cm
Pin Configuration:
- VCC: Connects to the power supply (+5V)
- Trig: Trigger input pin
- Echo: Echo output pin
- GND: Ground pin
Working Principle:
The HC-SR04 sensor determines the distance of an object using the time taken by ultrasonic waves to travel to the object and back.
- A 10-microsecond pulse is sent to the Trig pin to start the measurement.
- The sensor emits eight 40kHz ultrasonic pulses.
- If these waves hit an object, they reflect and return to the sensor.
- The Echo pin outputs a signal proportional to the time taken for the pulses to return.
- Distance is calculated using the formula:
Distance = (Speed of Sound × Time) / 2,
where the speed of sound is approximately 343 m/s.
Interfacing with Arduino:
To connect the HC-SR04 with an Arduino:
Connect VCC to 5V and GND to GND on the Arduino.
Connect the Trig and Echo pins to two digital I/O pins (e.g., D9 and D10).
Here’s a simple Arduino code to measure distance:
define trigPin 9
define echoPin 10
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.0343) / 2;
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm”);
delay(500);
}
Applications:
Obstacle avoidance in robotics
Security systems
Automated parking systems
Liquid level measurement