Overview

The HC-SR04 is an ultrasonic distance ranging module. It provides 2 cm to 400 cm non-contact measurement capability with a ranging accuracy of up to 3 mm.

The module includes an ultrasonic transmitter transducer, a receiver transducer, and control circuitry. Sending a 10 µs pulse to the Trig pin causes the module to emit an 8-cycle burst of 40 kHz ultrasound and set the Echo pin HIGH. The duration of the Echo HIGH pulse is directly proportional to the time taken for the ultrasonic wave to travel to the target and reflect back.

Quick reference

Operating voltage (VCC)4.5 V to 5.5 V DC (5 V nominal)
Operating current15 mA active, <2 mA< 2\text{ mA} quiescent
Ultrasonic frequency40 kHz
Ranging distance2 cm to 400 cm (0.8 in to 157 in)
Ranging accuracy3 mm
Measuring angle<15< 15^\circ effective cone angle
Trigger input signal10 µs TTL pulse (Trig)
Echo output signal5V TTL pulse duration proportional to distance (Echo)

Pinout

Standard 4-Pin Header

PinNameTypeDescription
1VCCPowerSupply voltage (+5.0 V DC)
2TrigDigital InputTrigger input pin (Apply 10 μs\ge 10\text{ }\mu\text{s} HIGH pulse to start measurement)
3EchoDigital OutputEcho output pin (5V TTL HIGH pulse width = round-trip time)
4GNDPowerGround (0 V)

Specifications

ParameterSymbolMinTypMaxUnitConditions
Supply VoltageVCCV_{CC}4.55.05.5VDC
Active Supply CurrentICCI_{CC}101520mADuring measurement
Quiescent CurrentIQI_{Q}23mAIdle
Ranging Distancedd2400cm
Measuring Angleθ\theta1530°Target surface dependent
Trigger Pulse Widthttrigt_{trig}10µsMinimum TTL pulse
Echo Pulse Widthtechot_{echo}15025000µs150 μs2.5 cm150\text{ }\mu\text{s} \approx 2.5\text{ cm}, 25 ms4 m25\text{ ms} \approx 4\text{ m}
Timeout Periodttimeoutt_{timeout}38msOut-of-range / no echo (~6.5 m equivalent)

Communication & Distance Calculation

  1. Microcontroller holds Trig pin HIGH for at least 10 µs.
  2. HC-SR04 automatically sends eight 40 kHz sound pulses and sets Echo HIGH.
  3. Microcontroller measures the duration techot_{echo} (in microseconds) that Echo remains HIGH.
  4. Calculate distance using the speed of sound in air (vsound343 m/s=0.0343 cm/μsv_{sound} \approx 343\text{ m/s} = 0.0343\text{ cm/}\mu\text{s} at 20C20^\circ\text{C}):

Distance (cm)=techo×0.03432=techo58\text{Distance (cm)} = \frac{t_{echo} \times 0.0343}{2} = \frac{t_{echo}}{58}

Distance (inches)=techo148\text{Distance (inches)} = \frac{t_{echo}}{148}

text
Trig: ────┌─ 10µs ─┐──────────────────────────────────────────────────
          │        │
Echo: ────┴────────┴───┌─────────────── t_echo ───────────────┐────────
                       │                                       │
Sound:                 ░▒▓ 8x 40kHz Bursts ...                 │

Wiring

HC-SR04 PinMicrocontroller (Arduino Uno / ESP32)Notes
VCC5VMust be 5V DC (3.3V will cause range reduction or failure)
TrigDigital Output Pin (e.g. GPIO5)5V TTL input (3.3V GPIO is generally accepted)
EchoDigital Input Pin (e.g. GPIO18 via Level Shifter)Outputs 5V TTL! Use resistor voltage divider for 3.3V MCUs
GNDGNDGround
Warning

5V Echo Output Level Hazard: The Echo pin outputs a 5V TTL pulse. Connecting Echo directly to a 3.3V-only microcontroller pin (ESP32, ESP8266, Raspberry Pi) can damage the GPIO. Use a voltage divider (e.g. 1 kΩ1\text{ k}\Omega and 2 kΩ2\text{ k}\Omega resistors) to step down 5V to 3.3V.

Common mistakes

  • Directly connecting 5V Echo to 3.3V GPIO pins: Connecting Echo directly to an ESP32 or Raspberry Pi without a voltage divider or level shifter risks damaging the GPIO pin.
  • Powering from 3.3V: Standard HC-SR04 modules require 5.0 V DC. Powering at 3.3V causes erratic ranging or limits maximum range to <50 cm<50\text{ cm}. (Use 3.3V-compatible variants like RCWL-1601 or HC-SR04P for native 3.3V systems).
  • Triggering too fast (< 60 ms intervals): Triggering faster than 60 ms can cause ultrasonic echoes from previous pulses to interfere with the current measurement.
  • Soft / Angled target surfaces: Sound waves reflect away from hard surfaces angled >15>15^\circ or get absorbed by soft materials (fabrics, sponges), causing fake out-of-range timeouts.

Notes

  • Speed of sound varies with ambient temperature: vsound=331.3+0.606×T (C) m/sv_{sound} = 331.3 + 0.606 \times T\text{ }(^\circ\text{C})\text{ m/s}. Compensation can be added in software using a temperature sensor.

Assets & downloads