ESP32: HC-SR04 Ultrasonic Distance
ESP32: HC-SR04 Ultrasonic Distance is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.
FREE SIMULATOR EXAMPLE · BEGINNER
ESP32: HC-SR04 Ultrasonic Distance
This example measures distance with an HC-SR04 ultrasonic sensor: TRIG on GPIO18, ECHO on GPIO19, and the result in centimetres printed to the Serial Monitor twice a second. No libraries at all — the timing protocol is done by hand, so you can see exactly what the sensor expects.
| Board | ESP32 DevKit (Xtensa LX6, 240 MHz) |
|---|---|
| Difficulty | beginner |
| Category | sensors |
| Libraries | None — just the Arduino core |
| Runs in | Sensbench browser simulator — free, no account |

The exact circuit you get when you open this example — already wired, ready to run.
The sketch
// ESP32 — HC-SR04 Ultrasonic Distance Sensor
// Wiring: TRIG → D18 | ECHO → D19 | VCC → 3V3 | GND → GND
#define TRIG_PIN 18
#define ECHO_PIN 19
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.println("ESP32 HC-SR04 ready");
}
long measureCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long d = pulseIn(ECHO_PIN, HIGH, 30000UL);
return (d == 0) ? -1 : (long)(d * 0.0343 / 2.0);
}
void loop() {
long cm = measureCm();
if (cm < 0) Serial.println("Out of range");
else Serial.printf("Distance: %ld cm\n", cm);
delay(500);
}
How it works
measureCm() bit-bangs the whole protocol: TRIG low for 2 µs, high for exactly 10 µs via delayMicroseconds(10), then low again. That 10 µs pulse is the contract — it tells the HC-SR04 to fire eight 40 kHz ultrasonic bursts and raise ECHO for as long as the sound takes to come back.
The measurement is pulseIn(ECHO_PIN, HIGH, 30000UL): it times the ECHO pulse in microseconds with a 30 ms timeout, returning 0 if nothing echoes. Then d * 0.0343 / 2.0 converts time to centimetres — 0.0343 cm/µs is the speed of sound, halved because the pulse flew there and back. A −1 sentinel on timeout is what prints “Out of range”.
Before you build it for real
The simulator wires VCC to 3V3 and it works — on a real bench the HC-SR04 is a 5 V part. At 3.3 V the transducers are underdriven and range collapses, and at 5 V the ECHO pin pushes 5 V back into GPIO19, which is not 5 V tolerant: use a voltage divider (1 kΩ + 2 kΩ) on ECHO. Second, pulseIn() is blocking — with the 30 ms timeout your loop stalls up to 30 ms per reading. Fine for a demo, fatal for a robot that also needs to steer.
Get the parts
Related examples
FAQ
Why does it print "Out of range"?
pulseIn() returned 0, meaning no echo arrived within the 30 ms timeout — roughly five metres of round trip. On real hardware it usually means the sensor is aimed at a soft or angled surface, the target is beyond range, or TRIG and ECHO are swapped.
How accurate is the 0.0343 conversion?
0.0343 cm/µs is the speed of sound at about 20 °C; it drifts roughly 0.6 m/s per degree of air temperature. For a demo expect ±1 cm — plenty for a parking sensor. If you need better, scale the constant by a measured temperature from a second sensor.