Skip to content
Distance

HC-SR04 Ultrasonic Distance Sensor

The most reliable ultrasonic distance sensor — measure 2cm to 4m with just two GPIO pins.

The HC-SR04 measures distance by timing how long it takes a 40kHz ultrasonic pulse to bounce off an object and return. You send it a short trigger pulse, it fires eight ultrasonic bursts, and the ECHO pin stays high for exactly as long as the round trip took — convert that time to distance and you're done. No library is strictly required since the whole protocol is just two GPIO pins and a stopwatch.

It's a great first sensor for obstacle avoidance, parking sensors, liquid-level gauges, and non-contact proximity switches. The catch that trips people up most is voltage: the sensor runs on 5V and its ECHO pin outputs a 5V pulse, which will drive a 3.3V-only board's GPIO out of spec. Every board below except the 5V-native Arduino Uno needs a simple voltage divider on ECHO — see the per-board wiring notes.

Specifications

Operating voltage5 V DC
Operating current~15 mA
Measuring range2cm to 400cm
Accuracy±3mm
Measuring angle~15° effective beam angle
Ultrasonic frequency40 kHz
Trigger pulse width10 µs TTL pulse to start a measurement
InterfaceDigital trigger/echo pulse pair — no library or bus protocol required

Pinout

PinNameDescription
1VCCPower, 5V DC
2TRIGTrigger input — send a 10µs HIGH pulse to start a measurement
3ECHOEcho output — goes HIGH for the duration of the ultrasonic round trip. Outputs at VCC level (5V) regardless of board voltage.
4GNDGround

TRIG is a standard input and is safe to drive from a 3.3V GPIO — most boards read a 3.3V HIGH as a valid trigger. ECHO is the pin that causes problems: it always swings up to the sensor's 5V supply rail, so wiring it straight into a 3.3V-only GPIO (ESP32, Raspberry Pi, STM32) risks damaging that pin over time. Use a simple resistor voltage divider on ECHO — 1kΩ in series with the signal and 2kΩ from the tap point to GND works well — to bring the HIGH level down to a safe ~3.3V before it reaches a 3.3V board.

Variants

The plain HC-SR04 is the default choice for indoor, dry, breadboard-range projects. If the sensor needs to live outdoors or in a tank, use the waterproof JSN-SR04T instead — same trigger/echo timing concept, but check its longer minimum range before swapping it in. If you want native 3.3V operation without a voltage divider, the US-100 is the more forgiving pick.

VariantPrice
HC-SR04~$1–3
HY-SRF05~$2–4
JSN-SR04T~$3–6
US-100~$3–5

Board Integration

Library

None — uses built-in pulseIn() by Arduino core

No installation needed — TRIG/ECHO timing uses only digitalWrite(), digitalRead(), and pulseIn(), all part of the standard Arduino API.

Wiring

Component pinArduino Uno pinNote
VCC5V
TRIGD9
ECHOD10Safe to wire directly — the Uno's GPIOs are 5V-native, so no voltage divider is needed here.
GNDGND

Example code

C++
  • Leave at least 60ms between triggers; firing it faster than that can pick up the tail end of the previous echo and read short.
  • Objects that are soft, angled sharply away from the sensor, or thinner than the beam width can absorb or deflect the ping and return no echo at all — that's a sensor limitation, not a wiring fault.
  • The pulseIn() timeout above returns 0 on a missed echo instead of blocking loop() forever; tune the 30000us value down if you only care about short ranges and want faster failure detection.
View full Arduino Uno guide