What is an Ultrasonic Sensor?
An ultrasonic sensor is an electronic device that measures the distance to a target object by emitting ultrasonic sound waves (above 20 kHz, beyond human hearing range) and converting the reflected sound into an electrical signal. The HC-SR04 is the most popular ultrasonic sensor used in hobbyist and educational projects worldwide.
It works on the simple principle of **echolocation** — the same technique used by bats and dolphins. The sensor sends out a pulse of ultrasonic sound, which bounces off an object and returns. By measuring the time taken for the echo to return, the sensor calculates the distance.
HC-SR04 Specifications
|---|---|
The sensor has **4 pins**: VCC (5V), Trig (Trigger), Echo, and GND. The two cylindrical "eyes" on the front are the ultrasonic transmitter and receiver.
How It Works — Step by Step
1. **Trigger Pulse**: Send a HIGH signal to the Trig pin for at least 10 microseconds.
2. **Ultrasonic Burst**: The sensor automatically sends 8 pulses of 40 kHz sound.
3. **Echo Reception**: The sensor's receiver listens for the reflected sound waves.
4. **Echo Pin Output**: The Echo pin goes HIGH for a duration equal to the round-trip time.
5. **Distance Calculation**:
Distance (cm) = (Time in μs × 0.034) / 2
We divide by 2 because the sound travels to the object and back.
💡 **Pro Tip**: The speed of sound is approximately 343 m/s at 20°C. Temperature changes can affect accuracy by ~0.6 m/s per degree Celsius.
Wiring with Arduino UNO
**Connections:**
- **VCC** → Arduino 5V
- **GND** → Arduino GND
- **Trig** → Arduino Digital Pin 9
- **Echo** → Arduino Digital Pin 10
**Arduino Code:**
#define TRIG_PIN 9
#define ECHO_PIN 10
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Send trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read echo duration
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = (duration * 0.034) / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Real-World Projects You Can Build
🤖 **Obstacle-Avoiding Robot**: Mount the sensor on the front of a chassis, detect obstacles, and steer the motors left or right.
🅿️ **Smart Parking System**: Detect whether a parking spot is occupied and display availability on an LED/LCD screen.
📏 **Digital Tape Measure**: Build a handheld device that measures room dimensions and displays distance on an OLED screen.
🚰 **Water Level Monitor**: Mount the sensor above a tank and measure the water level in real-time using IoT with ESP32 + Blynk.
🔔 **Intruder Alert System**: Place near doorways to detect motion — trigger a buzzer or send an alert via Wi-Fi.
Tips & Common Mistakes
- **Don't power from 3.3V** — the HC-SR04 needs 5V. Use a level shifter if using ESP32 (3.3V logic).
- **Minimum distance is 2cm** — anything closer gives unreliable readings.
- **Soft or angled surfaces** absorb or deflect sound, causing incorrect readings.
- **Add a small delay (50–100ms)** between measurements to avoid echo interference.
- **Use NewPing library** for more reliable readings: `#include <NewPing.h>`

