Read the world · 07 / 16
HC-SR04: Echo Time to Distance
Move an obstacle in an HC-SR04 lesson. Follow trigger and echo signals, convert echo time to centimeters, and compare distance readings.
Connect a distance reading to a simple decision.
Understand it
For this simplified ultrasonic example, echo duration in microseconds divided by 58 estimates centimeters. The model uses the distance control directly; it does not simulate sound propagation, reflection or timing noise.
Your experiment
Run and move the obstacle between 10 cm and 50 cm. Compare the displayed distance with the changing echo duration.
Connections
- D9 — Ultrasonic trigger
- D10 — Ultrasonic echo
- 5V / GND — Module supply
Sketch
This sketch illustrates the hardware logic. The workbench runs its corresponding teaching model, not a C++ compiler.
void setup() {
pinMode(9, OUTPUT);
pinMode(10, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(9, LOW);
delayMicroseconds(2);
digitalWrite(9, HIGH);
delayMicroseconds(10);
digitalWrite(9, LOW);
long cm = pulseIn(10, HIGH) / 58;
Serial.println(cm);
}Quick check
Why does a farther obstacle produce a longer echo time?
Show explanation
The sound travels to the obstacle and back. A longer round-trip path takes longer; the lesson uses echo microseconds divided by 58 to estimate centimeters.