Measure Distance with HC-SR04 & Arduino
The HC-SR04 fires a 40 kHz ultrasonic pulse and times the echo. Distance (cm) = duration (µs) ÷ 58. You'll wire it to Arduino, read distance in the Serial Monitor, and use it in obstacle-avoiding robots and parking-assist projects.
Materials
- Arduino Uno R3
- HC-SR04 ultrasonic sensor
- Breadboard
- 4 × jumper wires
Tools
- Arduino IDE
Step-by-step guide
- 1
Wire the sensor
VCC → 5V, GND → GND, TRIG → pin 9, ECHO → pin 10.
- 2
Understand the trick
Send a 10 µs HIGH pulse on TRIG. The sensor bursts 8 cycles at 40 kHz, then pulls ECHO HIGH until it hears the echo. pulseIn() returns the HIGH duration in µs.
- 3
Upload the sketch
Paste the code (see below), open Serial Monitor at 9600 baud.
- 4
Test
Wave your hand at different distances. Values under 400 cm are reliable; over that the echo weakens.
FAQs
Why is my reading 0?
Either ECHO isn't wired, the object is too close (< 2 cm) or too soft (fabric absorbs ultrasound).
Can I use HC-SR04 with ESP32?
Yes — but ESP32 GPIO is 3.3 V and HC-SR04 ECHO is 5 V. Use a voltage divider (1 kΩ + 2 kΩ) on the ECHO line to protect the ESP32.

