Components · 3 min read

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.

HC-SR04
HC-SR04: two ultrasonic transducers, one transmitter and one receiver, and 4 pins (VCC, TRIG, ECHO, GND).

How does the HC-SR04 work?

  1. The Arduino sends a 10-microsecond pulse to the TRIG pin.
  2. The sensor emits 8 sound pulses at 40 kHz.
  3. The sound bounces off the object and returns; meanwhile the ECHO pin stays HIGH.
  4. 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

FeatureValue
Supply5 V, about 15 mA
Range2–400 cm
ResolutionAbout 3 mm
Beam angleAbout 15°
PinsVCC, 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
}
The 30,000 µs timeout given to pulseIn corresponds to about 5 meters. Without a timeout, the code can wait up to 1 second when there's no object.

Using it with ESP32 and Raspberry Pi Pico

The HC-SR04's ECHO pin outputs 5 V, while ESP32 and Pico pins are 3.3 V. Don't connect ECHO directly: build a voltage divider with 1 kΩ and 2 kΩ resistors, or use the HC-SR04P version that runs on 3.3 V.

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.

See all projects
HC-SR04 Distance Measurement (Serial Display)
#27Beginner

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.

4.6(44)
64
Parking Sensor with HC-SR04
#28Intermediate
Premium

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.

4.8(64)
83
Measuring Distance Using LCD Display
#31Advanced
Premium

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.

4.5(44)
57
Height Measurement Project
#33Intermediate
Premium

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.

4.5(25)
33
Obstacle Avoidance Robot
#81Advanced
Premium

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

5.0(38)
49
ESP32 HC-SR04 Ultrasonic Distance Sensor
#116Intermediate
Premium

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

4.9(27)
43
ESP32 Prayer Rakat Counter (HC-SR04 + SSD1306 OLED + 2 Buttons)
#136Advanced
Premium

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

4.8(11)
14
ESP32 Parking Sensor (HC-SR04 + 5-LED Bar + Buzzer + OLED)
#142Advanced
Premium

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

0.0(0)

Learn how the HC-SR04 measures distance with sound, how to connect it to Arduino, the distance formula and how to make readings stable.

HC-SR04 Distance Sensor: Ultrasonic Distance Measurement with Arduino | BlueGrays