HC-SR04 Distance Sensor: Ultrasonic Distance Measurement with Arduino
How does the HC-SR04 ultrasonic distance sensor work and how do you connect it to Arduino? TRIG and ECHO pins, the distance formula, ESP32 and Pico wiring, and tips for better accuracy.
The HC-SR04 is a cheap and very common sensor that measures the distance to an object in front of it with sound. It works the way bats find their way: it sends out a high-frequency sound you can't hear and measures how long the echo takes to come back. It's the basis of parking sensors, obstacle-avoiding robots, tank level gauges and height meters.

How does the HC-SR04 work?
- The Arduino sends a 10-microsecond pulse to the TRIG pin.
- The sensor emits 8 sound pulses at 40 kHz.
- The sound bounces off the object and returns; meanwhile the ECHO pin stays HIGH.
- The Arduino measures how long ECHO stays HIGH and calculates the distance.
Sound travels through air at about 343 m/s, so it covers a centimeter in about 29 microseconds. The measured time covers the trip there and back, so it's divided by two: distance (cm) = time (µs) × 0.0343 / 2, or simply time / 58.
Specifications
| Feature | Value |
|---|---|
| Supply | 5 V, about 15 mA |
| Range | 2–400 cm |
| Resolution | About 3 mm |
| Beam angle | About 15° |
| Pins | VCC, TRIG, ECHO, GND |
Measuring distance with Arduino
Connect VCC to 5V, GND to GND, TRIG to pin 9 and ECHO to pin 10. The code below prints the distance to the serial monitor and reports separately when there's no object or it's too far away.
1const int TRIG_PIN = 9; // tetik / trigger
2const int ECHO_PIN = 10; // yankı / echo
3
4void setup() {
5 pinMode(TRIG_PIN, OUTPUT);
6 pinMode(ECHO_PIN, INPUT);
7 Serial.begin(9600);
8}
9
10void loop() {
11 digitalWrite(TRIG_PIN, LOW);
12 delayMicroseconds(2);
13 digitalWrite(TRIG_PIN, HIGH); // 10 µs darbe / 10 µs pulse
14 delayMicroseconds(10);
15 digitalWrite(TRIG_PIN, LOW);
16
17 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // en fazla 30 ms bekle / wait max 30 ms
18 if (duration == 0) {
19 Serial.println("Menzil dışı / out of range");
20 } else {
21 float cm = duration * 0.0343 / 2; // gidiş-dönüş / round trip
22 Serial.print(cm);
23 Serial.println(" cm");
24 }
25 delay(100); // ölçümler arası / between readings
26}Using it with ESP32 and Raspberry Pi Pico
Tips for accurate readings
- Soft and angled surfaces: fabric, foam and angled surfaces don't reflect sound well; readings may be missed.
- Take an average: using the median of 3–5 readings removes sudden jumps.
- Temperature effect: the speed of sound changes by about 0.6 m/s per 1 °C; for precise work, correct it with a temperature sensor.
- Several sensors: sensors triggered at the same time hear each other's echoes; trigger them one after another.
- Measurement interval: leave at least 60 ms between readings so the previous echo dies out.
To combine the sensor with a buzzer for a parking sensor, see What Is a Buzzer?, and to get to know other sensors, read the Arduino Sensors Guide.
Frequently asked questions
How far can the HC-SR04 measure?
In theory up to 4 meters. In practice you get reliable results up to 2–3 meters on a flat, hard surface.
Why does my HC-SR04 always show 0 or a huge value?
TRIG and ECHO may be swapped, the object may be out of range, or the sensor may be facing a surface that doesn't reflect sound. Check the wiring and use a timeout with pulseIn.
Can the HC-SR04 measure water level?
Yes, mount it facing down on a tank lid and measure the distance to the water surface. It needs protection in humid conditions; the waterproof JSN-SR04T is a better choice.
What other distance sensors are there for Arduino?
The infrared TCRT5000 is used for short distances and line detection, and the laser-based VL53L0X for millimeter precision. The HC-SR04 is the most common thanks to its balance of price and range.
Projects to build after reading this
See all projects
HC-SR04 Distance Measurement (Serial Display)
The HC-SR04 sensor works just like a bat finding its way in the dark: it sends out a sound you cannot hear, listens for the echo and measures how far away the object in front of it is.

Parking Sensor with HC-SR04
A mini version of a car's parking sensor: the closer something gets, the faster the beeps.

Measuring Distance Using LCD Display
A tool that measures distance without a ruler: move your hand back and forth in front of the sensor and watch the number change.

Height Measurement Project
A height meter that hangs from a door frame and tells you, in centimetres, how tall the person standing under it is.

Obstacle Avoidance Robot
How about building a smart vehicle that finds its own way? The Obstacle Avoidance Robot constantly scans its surroundings thanks to the ultrasonic sensor in front of it.

ESP32 HC-SR04 Ultrasonic Distance Sensor
The HC-SR04 ultrasonic sensor measures distance by timing how long its sound pulse takes to bounce off an object and return (time of flight). In this project the ESP32 sends a 10-microsecond pulse to the TRIG pin and reads the echo time from the ECHO pin with pulseIn(). The distance is shown in cm on a 16x2 I2C LCD. The HC-SR04 runs on 5V, so the ECHO pin needs a voltage divider, or the ESP32's 3.3V GPIO gets damaged.

ESP32 Prayer Rakat Counter (HC-SR04 + SSD1306 OLED + 2 Buttons)
Do you sometimes lose track of which rakat you are on during prayer? This project is a rakat counter that sits next to your prayer mat and counts your prostrations when you bring your hand close. The HC-SR04 ultrasonic distance sensor detects your hand, the SSD1306 OLED display shows the count, and two buttons let you choose 2, 3 or 4 rakats and reset. A 2.5-second debounce and a "hand-away" check make sure no prostration is counted twice.

ESP32 Parking Sensor (HC-SR04 + 5-LED Bar + Buzzer + OLED)
The ESP32 version of a car parking sensor! The HC-SR04 ultrasonic sensor measures the distance to the object in front. As it gets closer, a 5-LED bar fills up (green → yellow → red) and the buzzer beeps faster. Very close (under 10 cm), a continuous alarm sounds and all LEDs light up. The SSD1306 OLED display shows the live distance in cm in large text, plus a status message.
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

