#27Beginner

HC-SR04 Distance Measurement (Serial Display)

4.6(44)
65 completed
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.

Video

Circuit Diagram

HC-SR04 Distance Measurement (Serial Display) - Circuit

Source Code

Arduino (C++)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   HC-SR04 Mesafe Olcme (Serial Ekran)
4// ─────────────────────────────────────────
5// TR: HC-SR04 ultrasonik sensorle mesafeyi olcer ve 3-100 cm arasindaki
6//     sonuclari 0.5 sn'de bir Serial Monitor'e cm olarak yazar.
7// EN: Measures distance with the HC-SR04 ultrasonic sensor and prints results
8//     between 3 and 100 cm to the Serial Monitor in cm every 0.5 s.
9//
10// Pinler / Pins:
11//   D7 -> HC-SR04 Trig
12//   D6 -> HC-SR04 Echo
13//   HC-SR04 VCC 5V'a, GND GND'ye / HC-SR04 VCC to 5V, GND to GND
14
15int echoPin = 6;        // Yanki girisi / Echo input
16int trigPin = 7;        // Tetik cikisi / Trigger output
17int maxDistance = 100;  // Gosterilen en buyuk mesafe (cm) / Largest distance shown (cm)
18int minDistance = 3;    // Gosterilen en kucuk mesafe (cm) / Smallest distance shown (cm)
19
20void setup() {
21  pinMode(trigPin, OUTPUT);
22  pinMode(echoPin, INPUT);
23  Serial.begin(9600);
24}
25
26void loop() {
27  // 10 us tetik darbesi ses dalgasini gonderir / A 10 us trigger pulse sends the sound burst
28  digitalWrite(trigPin, HIGH);
29  delayMicroseconds(10);
30  digitalWrite(trigPin, LOW);
31
32  // TR: Echo'nun HIGH kalma suresi (us) gidis-donus suresidir. Ses 0.034 cm/us
33  //     hizla gider; yol ikiye bolunerek mesafe bulunur
34  // EN: How long Echo stays HIGH (us) is the round-trip time. Sound travels at
35  //     0.034 cm/us; halving the path gives the distance
36  unsigned long duration = pulseIn(echoPin, HIGH);
37  double totalPath = (double)duration * 0.034;
38  int distance = totalPath / 2;
39
40  // Yalniz gecerli aralikta yaz / Print only inside the valid range
41  if (distance > minDistance && distance < maxDistance) {
42    Serial.print("Distance: ");
43    Serial.print(distance);
44    Serial.println("cm");
45  }
46
47  delay(500);   // 0.5 sn bekle / Wait 0.5 s
48}
49

FAQ

How does the HC-SR04 measure distance?▾

The sensor emits a 40 kHz sound wave and measures the time it takes for the echo to return. Sound travels through the air at a speed of approximately 343 m/s; the distance is converted to centimeters using the formula: distance = time × 0.0343 / 2 (dividing by 2 accounts for the round trip).

What are the shortest and longest distances it can measure?▾

In practice, it measures distances ranging from approximately 2 cm to 400 cm. Measurements may be inaccurate or read as 0 when the distance is less than 2 cm, or when measuring very far distances or on sound-absorbing surfaces (such as fabric or foam).

Sometimes the readings are 0 or very large—why is that?▾

If no echo is received, `pulseIn` will time out. Make sure the sensor is facing a flat, hard surface, wait at least 60 ms between measurements, and filter out erroneous readings in your code.

Can I use it with the ESP32?▾

The HC-SR04 operates at 5 V, and its ECHO pin outputs 5 V. Since the ESP32 operates on 3.3 V logic, you should either add a voltage divider (e.g., 1 kΩ + 2 kΩ) to the ECHO line or use the 3.3 V-compatible HC-SR04P model.

HC-SR04 Distance Measurement (Serial Display) | BlueGrays