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.
Run this experiment →
Why it’s here
Connect a distance reading to a simple decision.
How it works
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.
Try it
- Open this 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
Example sketch
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);
}Check your understanding
Why does a farther obstacle produce a longer echo time?
Show the answer
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.