HC-SR04 Ultrasonic Distance Sensor
Measure echo travel time with an HC-SR04 and print distance to Serial Monitor. Handle a missing echo explicitly instead of reporting a false zero distance.

Wire it up
Disconnect power before changing connections. Follow terminal names rather than wire colors.
| From | To | Why it matters |
|---|---|---|
| 5V | HC-SR04 VCC | Use this tutorial with a 5 V AVR board |
| GND | HC-SR04 GND | Common reference |
| D7 | HC-SR04 TRIG | Trigger output |
| D8 | HC-SR04 ECHO | Echo input; HC-SR04 output is 5 V |
The complete sketch
const int triggerPin = 7;
const int echoPin = 8;
void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) {
Serial.println("No echo");
} else {
float distanceCm = duration * 0.0343 / 2.0;
Serial.print("distance:");
Serial.println(distanceCm);
}
delay(100);
}
The launch buttons use this same code and a matching saved circuit.
Build it step by step
Connect the module
Unplug USB. Connect VCC to 5 V, GND to GND, TRIG to D7, and ECHO to D8. Do not connect the 5 V echo directly to a 3.3 V-only input.
Verify and upload
Open the project as a new sketch, Verify, grant port access, and Upload.
Read the distance
Open Serial Monitor at 9600 baud. Aim at a flat object in front of the transducers. In simulation, adjust the sensor’s distance property.
Explore the plot
Switch to Serial Plotter for numeric distance samples. No echo is a text diagnostic, not a valid distance measurement.
Understand the sketch
Trigger then listen
A 10 µs HIGH pulse on TRIG requests a measurement. pulseIn measures the HIGH duration of ECHO in microseconds.
Round-trip travel
The sound travels to the object and back. Multiplying by approximately 0.0343 cm/µs converts time to round-trip distance; divide by two for one-way distance. Temperature and geometry affect real readings.
A bounded wait
The 30000 µs timeout prevents waiting indefinitely. A zero result means no complete pulse arrived in time, so the sketch prints No echo. delay(100) spaces measurements.
If something is different
Always No echo
Check TRIG/ECHO wiring and common ground. Use a flat target at a sensible range; soft or angled surfaces can reflect sound away.
Garbled text
Match Serial Monitor to Serial.begin(9600).
Readings jump
Keep the target steady. Reflections, temperature, sensor limits, and surface shape affect measurements; simulation is an idealized model.
Expected output and tested scope
Serial output reports labeled distance samples. A missing pulse prints No echo rather than a false zero measurement.
Version 1 · Last source- and build-matched test: 2026-10-25. Actual shipped AVR-GCC WASM compilation and Uno AVR engine execution; no physical hardware test.
Supported tutorial target: Arduino Uno R3 / ATmega328P. Compile: PASS · Modeled execution: PASS. Other boards require a separate review of pins, voltage, libraries and the exact build profile.