HC-SR04 Ultrasonic Sensor Explained: How It Measures Distance and How to Wire It to an Arduino Uno
Share
The HC-SR04 is a four-pin ultrasonic distance sensor that measures how far away an object is by timing how long a sound pulse takes to travel out and bounce back, then converting that time into centimeters. It has no I²C or SPI interface — a microcontroller triggers it with a 10-microsecond HIGH pulse on the TRIG pin, the sensor emits an 8-cycle 40kHz ultrasonic burst, and the ECHO pin goes HIGH for a duration proportional to the round-trip time, which Arduino's built-in pulseIn() function reads directly. It runs on 5V logic, is rated for a 2cm–400cm range with roughly 3mm accuracy inside a 15-degree measuring cone, and is the standard low-cost distance sensor behind Arduino obstacle-avoidance robots, parking sensors, and tank-level monitors.
What does the HC-SR04 measure?
| Property | Value |
|---|---|
| Measures | Distance to the nearest reflective object in front of the sensor |
| Range | 2cm – 400cm |
| Accuracy | ~3mm |
| Measuring angle | ~15° |
| Interface | Digital pulse timing (TRIG/ECHO) — not I²C or SPI |
| Logic/supply | 5V |
It does not report angle, size, or shape — only the straight-line distance to whatever ultrasonic pulse reflects back first inside that 15-degree cone. If two objects sit in the sensor's field of view, you only get the distance to the nearer one.
How does the HC-SR04 actually work?
The sensor has two ultrasonic transducers behind its two visible metal cylinders — one transmits, one receives. Pulling TRIG HIGH for 10 microseconds tells the sensor to fire: it emits eight pulses of 40kHz sound, inaudible to humans, and immediately starts listening on the receiver. The moment that sound reflects off an object and returns, the sensor sets ECHO HIGH for exactly as long as the round trip took, then drops it back LOW.
Converting that duration to distance uses the speed of sound, roughly 343 m/s at room temperature. Since the pulse travels to the object and back — twice the actual distance — the formula divides by two: distance_cm = duration_us * 0.0343 / 2, which is commonly simplified to distance_cm = duration_us / 58. Both give the same number; the second is just the arithmetic pre-folded.
How do you wire the HC-SR04 to an Arduino Uno?
| Arduino Uno | HC-SR04 | Signal |
|---|---|---|
| 5V | VCC | power |
| GND | GND | ground |
| Digital Pin 2 | TRIG | trigger pulse out |
| Digital Pin 3 | ECHO | echo pulse in |
Four wires, no resistors or level shifting needed on the Uno — both boards run 5V logic natively. TRIG and ECHO can move to any other digital pins; pin 2 and pin 3 are simply a common, interrupt-capable default.
Is the HC-SR04 safe to use with 3.3V boards?
Not directly. The ECHO pin outputs a full 5V HIGH signal, and feeding 5V into a 3.3V-logic input — on an ESP32, a Raspberry Pi, or similar — risks damaging that pin over time. On those boards the ECHO line needs a voltage divider (typically two resistors bringing 5V down to roughly 3.3V) or a proper logic-level shifter before it reaches the microcontroller. TRIG is not a problem in that direction, since a 3.3V HIGH still reliably triggers the sensor. On an Arduino Uno, none of this applies — the Uno's own I/O is 5V, so the sensor connects straight across with no extra parts.
Why does the HC-SR04 need a delay between readings?
A leftover echo from one ping can still be bouncing around the room when the next TRIG pulse fires, and the sensor has no way to tell that stale echo apart from a fresh one. The commonly recommended fix is a roughly 60ms delay between consecutive readings, long enough for the previous pulse's echoes to die out before the next trigger goes out. Skipping this delay is the usual cause of a HC-SR04 that reads correctly most of the time but occasionally returns an erratic, too-short distance.
Watch it built live: a real Arduino Uno + HC-SR04 project
Rather than describe a generic build, we typed one real sentence into Compoden's AI build assistant on the storefront and let it do the part-picking, pricing, and explaining — unscripted, screenshotted as it happened:
"I want to build a distance-sensing project using an HC-SR04 and an Arduino Uno"

It picked five in-stock parts and explained the reasoning for each. On the sensor itself, it noted: "The HC-SR04 Ultrasonic Sensor is how your project measures distance. It sends out a high-frequency sound pulse and listens for the echo. The time it takes for the sound to return tells the microcontroller how far away an object is." It also flagged the real-world failure mode unprompted: "the HC-SR04 sensor needs clear line of sight to measure distance accurately; obstacles or soft, sound-absorbing surfaces can lead to inaccurate readings."
Watch the firmware get generated
We then asked Compoden's build companion, Soldr, to write the actual Arduino sketch for the same project — targeting the Uno specifically, with real pin definitions and a real distance-measurement function:

The generated sketch defines TRIG_PIN 2 and ECHO_PIN 3 to match the wiring table above, includes a MAX_DISTANCE_CM 400 constant to bound the pulseIn() timeout and filter out erroneous readings, and opens with wiring comments spelling out each connection (VCC to 5V, GND to GND, TRIG to pin 2, ECHO to pin 3) directly above the pin definitions — so the code and the physical wiring stay traceable to each other. It builds around a dedicated measureDistanceCm() function rather than inlining the pulse timing in loop(), which keeps the sketch straightforward to extend later.
So what is the HC-SR04 actually good for?
Anything that needs to know how far away something is without touching it: obstacle-avoidance robots, automated parking-assist rigs, water-level and tank monitoring, and contactless measuring tools. It's the default first distance sensor for makers because it needs no library beyond a single built-in Arduino function, costs very little, and its four pins map onto almost any microcontroller with digital I/O.
Get everything in this build
The exact five parts Soldr picked above, at today's live price and stock — each links straight to checkout, or add the whole tray in one click.
| Part | Price | |
|---|---|---|
| Arduino Uno R3 CH340G ATmega328P Board | ₹230 | Add to cart → |
| HC-SR04 Ultrasonic Sensor | ₹85 | Add to cart → |
| 23 AWG Multi-Strand Breadboard Wire | ₹10 | Add to cart → |
| Male-to-Male Breadboard Jumper Wires (20cm, 24AWG) | ₹40 | Add to cart → |
| 5V 2A Micro-USB Power Adapter | ₹140 | Add to cart → |
| Total | ₹505 | Add all 5 to cart → |
Prices and stock verified live at the time this was written; Compoden's storefront always reflects the current price at checkout.
Built and Backed by Compoden
Every part above ships from Compoden's own India stock, tested for compatibility before it's carded together — not sourced individually and hoped to work. Delivery in 3–7 days across India, with COD and UPI available at checkout. If a part in this build doesn't perform as described, Compoden's support team will help you troubleshoot or replace it.
FAQ
What is the minimum and maximum distance the HC-SR04 can measure?
2cm at the near end and 400cm at the far end, with roughly 3mm accuracy across that range.
Why do I need a delay between HC-SR04 readings?
A leftover echo from the previous ping can still be traveling when the next trigger fires, and the sensor can't distinguish it from a fresh echo. A roughly 60ms gap between readings lets the previous pulse's echoes die out first.
Can the HC-SR04 be connected directly to a 3.3V board like the ESP32?
Not on the ECHO line. ECHO outputs a full 5V HIGH, which can damage a 3.3V input over time, so it needs a voltage divider or logic-level shifter on boards that use 3.3V logic. On a 5V board like the Arduino Uno, it connects directly with no extra parts.
What formula converts the HC-SR04's echo pulse into a distance?
distance_cm = duration_us / 58, where duration_us is the time in microseconds that the ECHO pin stayed HIGH. It's the same result as duration_us × 0.0343 / 2, using the speed of sound and dividing by two for the round trip.