ESP32 FAQ

ESP32 Ultrasonic HC-SR04

// ultrasonic · hc-sr04 · faq

Filed under: ESP32 · HC-SR04 · Updated August 2026

Cheapest distance sensor you can wire to an ESP32: one trigger pin, one echo pin, a pulse measured in microseconds. Here’s the hookup, the 5V question, and the readings that make no sense.

How to Connect Ultrasonic Sensor to ESP32?

Four wires: VCC→5V, GND→GND, TRIG→a GPIO, ECHO→a GPIO — with a two-resistor divider on ECHO, because the HC-SR04 runs at 5V logic. Then: 10µs high on TRIG, pulseIn() on ECHO, and distance in cm is the pulse width in µs divided by 58. That’s the whole sensor.

Does ECHO need a level shifter?

Yes — a divider is enough: 1k from ECHO to the GPIO, 2k from the GPIO to GND, lands at ~3.3V. I’ve run ECHO bare on some boards and it mostly works, but the divider removes the “sometimes the pin dies” lottery. Skip the full shifter; ECHO is slow, so division is plenty.

Why does my HC-SR04 always read 0?

The pulse never made it back — check TRIG and ECHO wiring first, then the divider, since a wrong ratio pulls the logic threshold out of range. Intermittent zeros are power: the sensor on 3V3 plus a Wi-Fi transmit browns out the transducer. Feeding VCC from the 5V pin fixes most cases. The power FAQ has the rails straight.

Why does my HC-SR04 read random values?

It’s hearing its own echo off the wrong surface, or listening to noise. The beam is a ~15° cone and the first reflection wins — a desk edge, a fan blade, a loose jumper within that cone all produce nonsense. My first build read 2cm across the room because the sensor pointed at a table 3cm away. Mount it clear and the numbers settle.

What’s the real maximum range?

400cm is the spec, ~300cm is realistic indoors, and 2cm is the minimum — closer than that the echo overlaps the trigger and reads garbage. Hard, flat surfaces reflect best; soft fabric and rounded edges eat the pulse. Past 3-4m, an IR or time-of-flight sensor is the better tool.

How do I measure distance in code?

Ten lines: digitalWrite(TRIG, HIGH), 10µs delay, back LOW, then pulseIn(ECHO, HIGH) with a timeout like 30000µs (≈5m) so a lost echo doesn’t hang the loop. Divide by 58 for cm or 148 for inches; the factor assumes 20°C air, close enough indoors. I’ve never seen a build where 57.5 vs 58 mattered. Distance examples are ready to copy.

Can I use several HC-SR04s on one ESP32?

Yes — each sensor needs its own TRIG and ECHO GPIO, and nothing else: 4 sensors take 8 pins and run fine side by side. The gotcha is crosstalk: fire them one at a time, a short delay between each TRIG pulse, or sensor B hears sensor A’s echo.

Ultrasonic, IR, or time-of-flight — which sensor?

Ultrasonic wins on price and 4m range but loses on precision and blind spots (soft objects, edges). IR proximity is a few cheap centimeters, no echo math. A time-of-flight sensor like the VL53L0X nails millimeter precision to ~2m over I2C but costs more. For bumpers, HC-SR04; for measuring, ToF.

Can I power the HC-SR04 from 3.3V?

It runs, badly. At 3.3V the sensor still fires, but range drops, readings get noisier, and ECHO’s high sits near the ESP32’s logic threshold — a recipe for half-valid pulses. The chip wants 5V, so take it from the 5V pin and keep the divider. Depends on your board’s regulator, but most devkits handle one sensor easily.

Why does my distance flip to 400 suddenly?

That’s a missed echo — the sensor fired, nothing came back in the timeout window, and pulseIn returned the timeout value. Causes: target left the cone, an absorbent surface (curtain, clothes), or the sensor angled past ~30° off perpendicular. Filter it: ignore the 400-ish reads and use the last good one.

Measure Distance on ESP32 Right Now — In Your Browser

Run the real thing before you wire the real thing — free, no account:

The Serial Monitor reports what the echo timer computes:

ESP32 HC-SR04 ready
Distance: 23 cm
Distance: 23 cm
Out of range

Move the target in the virtual scene and the number follows; push it past sensing distance and you get the Out of range branch — the same no-echo case you would debug on a bench with a tape measure.

Leave a Reply

Your email address will not be published. Required fields are marked *