HC-SR04 Ultrasonic Distance Sensor
The HC-SR04 uses four pins (VCC, Trig, Echo, GND) to measure distance from 2 cm to 400 cm by sending a 10 µs ultrasonic pulse on Trig and timing the reflected sound wave return on Echo.
How It Works & Why It Matters
Just like bat echolocation, high-frequency 40 kHz sound waves bounce off nearby surfaces back to the sensor.
At room temperature, sound travels ~0.034 cm per microsecond; dividing duration × 0.034 by 2 converts round-trip time into one-way distance.
digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH); float distanceCm = duration * 0.034 / 2.0;
🏫 Classroom Example
A smart trash-can lid project opens automatically with a servo motor whenever distanceCm drops below 20 cm.
⚖️ HC-SR04 Ultrasonic vs. PIR Motion Sensor
HC-SR04 gives exact distance in centimeters; PIR only outputs a digital YES/NO signal when warm motion crosses its field.
Forgetting to divide by 2 (reporting double the actual distance) or swapping VCC and GND wires.