The HC-SR04 is the default choice for distance sensing in beginner robotics — obstacle-avoiding robots, parking sensors, theremin-style projects, anything that needs to know “how far away is that thing.” It’s cheap, simple to wire, and the one real gotcha with it isn’t in the wiring diagram most tutorials show you.
An HC-SR04 ultrasonic distance sensor. Photo: Nevit Dilmen, CC BY-SA 3.0 (resized)
Pinout
| Pin | Name | What it does |
|---|---|---|
| 1 | VCC | Power, 5V |
| 2 | TRIG | Trigger input — a 10µs HIGH pulse starts a measurement |
| 3 | ECHO | Echo output — goes HIGH for a duration proportional to distance |
| 4 | GND | Ground |
How it actually works
Send a 10µs HIGH pulse to TRIG, and the sensor emits a burst of eight 40kHz ultrasonic pulses — above human hearing range — then immediately starts listening for the echo. The moment it sends the burst, ECHO goes HIGH; the moment it detects the reflection bouncing back, ECHO goes LOW. The width of that HIGH pulse is directly proportional to the round-trip travel time of the sound, which is directly proportional to distance.
Converting that pulse width to a distance uses the speed of sound, roughly 343 m/s at room temperature (0.034 cm per microsecond), and you divide by two because the pulse width covers the round trip — out to the object and back:
long duration = pulseIn(echoPin, HIGH);
float distanceCm = duration * 0.034 / 2;
The gotcha most tutorials skip: ECHO is a 5V signal
This is the one thing worth knowing before you wire an HC-SR04 to anything other than a classic 5V Arduino. The sensor’s ECHO pin outputs a genuine 5V HIGH signal, and feeding that straight into a 3.3V-only input — an ESP32, an ESP8266, or a Raspberry Pi’s GPIO — risks damaging that pin over time, since none of those are 5V-tolerant on their inputs.
The fix is a simple voltage divider on the ECHO line only (TRIG is an input to the sensor, so it’s fine driven from a 3.3V output): a 1kΩ resistor from ECHO to the microcontroller’s input pin, then a 2kΩ resistor from that same input pin to ground. That combination drops the 5V signal down to a safe ~3.3V before it reaches the 3.3V-only pin. TRIG can be wired directly with no divider needed.
Other wiring and code notes
- Give it a timeout.
pulseIn()blocks until it sees the expected transition, and if nothing’s in range (or the wiring’s wrong), it can hang for a full second or more waiting for an echo that never comes. Pass a timeout in microseconds as the third argument —pulseIn(echoPin, HIGH, 30000)— so a missed reading returns 0 instead of stalling your whole sketch. - Effective range is roughly 2cm to 400cm. Objects closer than about 2cm reflect the pulse back before the sensor’s finished transmitting, giving unreliable readings; objects much past 4m, or angled surfaces that scatter the sound away rather than back at the sensor, often return no echo at all.
- Soft, angled, or sound-absorbent surfaces are the usual cause of “random” bad readings, not a faulty sensor — foam, fabric, and anything angled more than about 15° off directly-facing the sensor scatters the ultrasonic pulse instead of reflecting it straight back.
- Running more than one HC-SR04 at once? Fire them one at a time with a short delay between, not simultaneously — one sensor’s pulse can trigger a false echo on a neighbouring sensor listening at the same moment, an effect usually called crosstalk.
Variants worth knowing about
The HY-SRF05 is a near-identical five-pin variant with an extra OUT pin, allowing single-pin trigger-and-echo operation if you’re short on I/O — it’s backward-pin-compatible with the standard HC-SR04 wiring if you ignore the extra pin. The JSN-SR04T is a waterproof version with a separate probe on a cable, meant for outdoor or liquid-level sensing where the standard board’s exposed transducers won’t survive the environment — worth knowing it exists if a project needs to go outside, since the standard HC-SR04 isn’t weatherproof at all.
If you haven’t wired up a sensor on a breadboard before, how a breadboard is actually connected underneath is worth a read first — most “the sensor gives garbage readings” reports turn out to be a wiring issue once traced back.