AR-27Boshlang‘ich

HC-SR04 Distance Measurement (Serial Display)

4.6(50)
82 bajarildi
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. The Arduino writes that distance in centimetres to your computer screen every half second, showing readings between 3 and 100 centimetres. It is the basis for projects like parking sensors and robots that dodge obstacles.

Video

Elektr sxemasi

HC-SR04 Distance Measurement (Serial Display) - Elektr sxemasi

Manba kodi

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   HC-SR04 Distance Measurement (Serial Display)
4// ─────────────────────────────────────────
5// Measures distance with the HC-SR04 ultrasonic sensor and prints results
6// between 3 and 100 cm to the Serial Monitor in cm every 0.5 s.
7//
8// Pins:
9//   D7 -> HC-SR04 Trig
10//   D6 -> HC-SR04 Echo
11//   HC-SR04 VCC to 5V, GND to GND
12
13int echoPin = 6;        // Echo input
14int trigPin = 7;        // Trigger output
15int maxDistance = 100;  // Largest distance shown (cm)
16int minDistance = 3;    // Smallest distance shown (cm)
17
18void setup() {
19  pinMode(trigPin, OUTPUT);
20  pinMode(echoPin, INPUT);
21  Serial.begin(9600);
22}
23
24void loop() {
25  // A 10 us trigger pulse sends the sound burst
26  digitalWrite(trigPin, HIGH);
27  delayMicroseconds(10);
28  digitalWrite(trigPin, LOW);
29
30  // How long Echo stays HIGH (us) is the round-trip time. Sound travels at 0.034 cm/us; halving the
31  // path gives the distance
32  unsigned long duration = pulseIn(echoPin, HIGH);
33  double totalPath = (double)duration * 0.034;
34  int distance = totalPath / 2;
35
36  // Print only inside the valid range
37  if (distance > minDistance && distance < maxDistance) {
38    Serial.print("Distance: ");
39    Serial.print(distance);
40    Serial.println("cm");
41  }
42
43  delay(500);   // Wait 0.5 s
44}
45

Ko‘p so‘raladigan savollar

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.