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.
const int TRIG_PIN = 9; // tetik / trigger
const int ECHO_PIN = 10; // yankı / echo
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); // 10 µs darbe / 10 µs pulse
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000); // en fazla 30 ms bekle / wait max 30 ms
if (duration == 0) {
Serial.println("Menzil dışı / out of range");
} else {
float cm = duration * 0.0343 / 2; // gidiş-dönüş / round trip
Serial.print(cm);
Serial.println(" cm");
}
delay(100); // ölçümler arası / between readings
}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)
Project that measures distance with HC-SR04 ultrasonic sensor and displays the result in cm on Serial Monitor. It teaches the principle of distance calculation with sound wave.

Parking Sensor with HC-SR04
Parking sensor project with HC-SR04 sensor that increases the buzzer beep rate as the object approaches and warns with LED. Teaches feedback logic proportional to distance.

Measuring Distance Using LCD Display
A project that displays the readings from the HC-SR04 distance sensor in cm in real time on a 2x16 LCD screen. It combines sensor data and LCD display.

Height Measurement Project
Project that performs height measurement by calibrating with HC-SR04 sensor and I2C LCD screen. It offers a practical application by managing calibration and measurement steps with two buttons.

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. When it encounters an obstacle

ESP32 HC-SR04 Ultrasonic Distance Sensor
The HC-SR04 ultrasonic sensor calculates distance by measuring the time it takes for the sound wave it emits to bounce off an object and return (Time of Flight). In this project, we will send a 10-microsecond pulse to the TRIG pin of the ESP32 and read the echo duration from the ECHO pin using `pulseIn()`. We will display the calculated distance in centimeters on the Serial Monitor. Since the HC-SR04 operates at 5V, a **voltage divider** is required on the ECHO pin—otherwise, the ESP32’s 3.3V GP

ESP32 Prayer Rakat Counter (HC-SR04 + SSD1306 OLED + 2 Buttons)
Do you sometimes forget how many rak’ahs you’ve prayed while performing salat? This project creates a rak’ah counter that is placed next to your prayer mat and counts your prostrations when you bring your hand close to it. The HC-SR04 ultrasonic distance sensor detects hand proximity, the SSD1306 OLED display shows the rak’ah count, and you can select 2, 3, or 4 rak’ahs and reset the count using two buttons. It features a smart state machine with a 2.5-second debounce and a “hand-away” algorithm

ESP32 Parking Sensor (HC-SR04 + 5-LED Bar + Buzzer + OLED)
The ESP32 version of the car parking sensor! We use the HC-SR04 ultrasonic sensor to measure the distance to the object in front of it. As the distance decreases, the 5-LED bar fills up (green → yellow → red) and the buzzer beep interval shortens. When the object gets very close (under 10 cm), a continuous alarm sounds and all LEDs light up. The SSD1306 OLED display shows the current distance in cm in large font, a status message (STOP / CAUTION / CLOSE / STOP! / DANGER), and a simulation of the
More articles
Learn how the HC-SR04 measures distance with sound, how to connect it to Arduino, the distance formula and how to make readings stable.

