HC-SR04

A low-cost ultrasonic distance sensor that measures 2 cm to 4 m by timing a 40 kHz echo.

HC-SR04: Ultrasonic ranging

What is the HC-SR04?

The HC-SR04 is a low-cost ultrasonic distance sensor found in more Arduino and Raspberry Pi projects than almost any other part. It measures distance the way a bat or sonar does: it emits a burst of 40 kHz ultrasound from its transmitter and times how long the echo takes to bounce back to its receiver. Because the speed of sound is roughly constant, that round-trip time maps directly to distance.

It has four pins: VCC, Trig, Echo, and GND. To take a reading you send a 10 µs HIGH pulse to the Trigger pin; the module fires eight 40 kHz cycles and then holds the Echo pin HIGH for a time proportional to the round-trip distance. You convert that echo pulse width to a distance with distance (cm) = echo time (µs) ÷ 58 (from the speed of sound, ~343 m/s, divided by two for the there-and-back trip).

In practice it reliably measures from about 2 cm out to 400 cm, with roughly 3 mm resolution over a ~15° beam. It works best against flat, hard, perpendicular surfaces; soft or steeply angled targets scatter the sound and read poorly. The one thing to watch: the Echo pin outputs a 5 V signal, which needs attention on 3.3 V boards (see Circuit requirements below).

HC-SR04 pinout

PinFunctionType
VCC VCC (5V)PowerPower in
TRIG TriggerDigitalDigital input
ECHO EchoDigitalDigital output
GND GNDPowerPower in

Specifications

Operating voltage
5 V DCHC-SR04 datasheet
Operating current
15 mAHC-SR04 datasheet
Ultrasonic frequency
40 kHzHC-SR04 datasheet
Measuring range
2 cm – 400 cmHC-SR04 datasheet
Resolution
≈ 3 mm (0.3 cm)HC-SR04 datasheet
Effective beam angle
15°HC-SR04 datasheet
Trigger input
10 µs HIGH TTL pulseHC-SR04 datasheet
Echo output
TTL pulse, width ∝ distanceHC-SR04 datasheet
Interface
GPIO timing (Trig / Echo), no I²C or SPIPin definition
Dimensions
45 × 20 × 15 mmHC-SR04 datasheet

Every specification is cited to its source.

Circuit requirements

  • Power the module from 5 V. The classic HC-SR04 does not read reliably at 3.3 V. If you need 3.3 V operation, use the HC-SR04P (or RCWL-1601) variant, which accepts 3–5 V.
  • Always share a common ground between the sensor and the microcontroller, or the echo timing will be erratic.
  • On 3.3 V boards (ESP32, ESP8266, Raspberry Pi Pico, Raspberry Pi) the 5 V Echo output must be dropped to 3.3 V. Feeding 5 V straight into a 3.3 V input can damage the pin. Use a resistor divider (e.g. 1 kΩ in series with Echo, 2 kΩ to ground) or a level shifter. Trigger can usually be driven directly from a 3.3 V GPIO.
  • Leave at least ~60 ms between readings so a previous echo (or a no-object timeout) doesn't bleed into the next measurement, and use a timeout on pulseIn() so a missing echo can't block your loop.

Board compatibility

The HC-SR04 connects to any of the microcontrollers supported in Tinkered. Mind the 5 V logic level on 3.3 V boards.

Simulate the HC-SR04 in your browser

The HC-SR04 is fully simulated in Tinkered. Wire it into a circuit, write your firmware, and read real values back with no physical hardware. Test your logic before you touch a breadboard.

Open it in the simulator

Common HC-SR04 mistakes

Wiring 5 V Echo straight into a 3.3 V board

The Echo pin drives a 5 V signal. On ESP32, ESP8266, Pi Pico or Raspberry Pi, connecting it directly can exceed the input's maximum voltage and damage the pin. Add a voltage divider or level shifter on Echo.

Running it on 3.3 V power

The standard HC-SR04 expects a 5 V supply. Powered from 3.3 V it gives short, noisy, or missing readings. Use 5 V, or switch to the 3.3 V-rated HC-SR04P variant.

No common ground

If the sensor's GND isn't tied to the microcontroller's GND, the echo pulse timing floats and distances jump around. Always connect grounds together.

Polling too fast / no timeout

Re-triggering before the last echo returns, or waiting forever when nothing is in range, produces stuck or garbage values. Space readings ~60 ms apart and give pulseIn() a timeout.

Aiming at soft or angled surfaces

Foam, fabric, and steeply angled targets absorb or deflect the 40 kHz beam, so the echo never comes back cleanly. Expect best results from flat, hard surfaces facing the sensor within its ~15° cone.

HC-SR04 datasheet

Official HC-SR04 datasheet

Popular HC-SR04 projects

Obstacle-avoiding robot

Mount one (or three) on a rover to detect walls and steer around them.

Parking / reverse sensor

Beep faster as a car approaches a wall, like a factory parking assistant.

Contactless water-level monitor

Point it down a tank to read fill level without any part touching the water.

Touchless dispenser

Trigger a pump or servo when a hand enters range for a no-touch soap or sanitizer station.

Smart trash can

Open the lid automatically when someone steps close.

Ultrasonic radar

Sweep the sensor on a servo and plot distance vs. angle for a mini sonar display.

Build with the HC-SR04.

Drop the HC-SR04 into a circuit, write firmware, and simulate it in your browser, then deploy to real hardware. All in one editable Tinkered project.