HC-SR04 — Ultrasonic Distance Sensor Module
A popular 40 kHz ultrasonic ranging module capable of measuring distances from 2 cm to 450 cm with up to 3 mm precision. Sends a timed echo pulse and lets your microcontroller calculate distance using the speed of sound. Works directly with Arduino, ESP32, and Raspberry Pi GPIO pins with no extra components required.
- 2 cm Blind-Zone Minimum
- 450 cm Maximum Range
- ±15° Sensing Angle
- Arduino Compatible
- ESP32 Compatible
- Raspberry Pi Compatible
- Only 2 Signal Pins
- 7.5g Lightweight
The HC-SR04 TRIG and ECHO pins operate at 5V logic. The TRIG pin is fine driven from 3.3V GPIO, but the ECHO pin outputs 5V which can damage ESP32 or Raspberry Pi inputs. Use a simple two-resistor voltage divider (1kΩ and 2kΩ) to drop ECHO to 3.3V before connecting to a 3.3V board.
Electrical & Physical Specifications
Full specifications for the HC-SR04 ultrasonic distance sensor module. All values at DC 5V supply unless stated.
- Model Number
- HC-SR04 (HCSR04)
- Working Voltage
- DC 5V
- Quiescent Current
- Less than 2 mA
- Level Output (High)
- 5V
- Level Output (Low)
- 0V
- Detection Range
- 2 cm – 450 cm
- Precision
- Up to 3 mm (0.3 cm)
- Sensing Angle
- Not more than 15°
- Ultrasonic Frequency
- 40 kHz
- Trigger Input Signal
- 10 µs TTL pulse minimum
- Distance Formula
- Distance = (Echo high time × 340 m/s) ÷ 2
- Pins
- VCC, TRIG, ECHO, GND
- Weight
- 7.5 g
- MCU Compatibility
- Arduino, ESP32 (with divider on ECHO), Raspberry Pi
After the 10 µs trigger pulse, the module fires eight 40 kHz bursts and raises ECHO HIGH for the duration the sound takes to travel to the target and back. Divide the echo time in microseconds by 58 to get centimetres, or by 148 for inches.
Pinout & Wiring Instructions
The HC-SR04 has four pins in a single row: VCC, TRIG, ECHO, GND — left to right when the two transducer cylinders face you.
HC-SR04 Pinout
VCC · TRIG · ECHO · GND
-
1VCC → 5V
Connect VCC to the 5V pin of your Arduino. Do not use 3.3V — the module requires 5V to operate.
-
2GND → GND
Common ground with your Arduino or ESP32.
-
3TRIG → Digital Pin (e.g. D9)
Send a 10 µs HIGH pulse to trigger a reading. 3.3V GPIO is sufficient to trigger the module.
-
4ECHO → Digital Pin (e.g. D10)
Reads the pulse duration. Arduino: connect directly. ESP32 / RPi: use a 1kΩ + 2kΩ voltage divider to drop 5V to 3.3V before connecting.
-
5Calculate distance
Use
pulseIn(ECHO, HIGH)to measure µs. Divide by 58 for cm or 148 for inches. The NewPing library simplifies this.
Wait at least 60 ms between trigger pulses to allow ultrasonic echoes to clear. Triggering too quickly causes false short-range readings from the previous cycle's echo.
Applications & Use Cases
The HC-SR04 is one of the most widely used sensors in hobbyist and maker projects. Its low cost, simple two-wire interface, and 4.5 m range make it ideal for anything from obstacle avoidance to liquid level monitoring.
Mount on a servo for sweeping detection or fixed for forward collision avoidance on Arduino and ESP32 rovers.
DIY garage or vehicle parking aid — beeps when an object is detected within a set distance.
Measure water tank, sump, or container fill levels without contact with the liquid.
Detect when someone passes through a doorway or breaks the beam between two points.
Non-contact height gauging for conveyor systems, sorting machines, or interactive installations.
Trigger lights, fans, or alarms based on detected presence or proximity with ESPHome or Home Assistant.
The HC-SR04 is natively supported in ESPHome with the ultrasonic sensor platform. Define TRIG and ECHO pins and get distance readings directly in Home Assistant with no extra code.
Frequently Asked Questions
- The module itself needs 5V on VCC to function. The TRIG pin can be driven from 3.3V GPIO. The ECHO pin outputs 5V however, which can damage 3.3V microcontrollers — use a 1kΩ/2kΩ voltage divider or a logic level shifter on the ECHO line before connecting to an ESP32 or Raspberry Pi.
- The blind zone starts at 2 cm. Objects closer than 2 cm may not be reliably detected. For very close-range sensing consider an IR proximity sensor instead.
- Measure the ECHO HIGH duration in microseconds using
pulseIn(echoPin, HIGH). Divide by 58 to get centimetres, or by 148 for inches. This accounts for the round-trip travel of the sound pulse. The NewPing Arduino library handles all of this automatically. - Wait at least 60 ms between trigger pulses. Faster polling risks detecting the echo from the previous cycle, giving spuriously short readings. In practice 100 ms (10 readings per second) is a safe rate for most applications.
- Soft or angled surfaces absorb or scatter ultrasonic energy, reducing effective range and accuracy. For best results aim at flat, hard surfaces such as walls, water, or solid objects. Angled surfaces should be within roughly 15° of perpendicular to the sensor face.
- Yes — the NewPing library by Tim Eckel is the most popular choice. Install it via the Arduino Library Manager. It simplifies triggering, echo timing, and includes median filtering to reduce noise. For ESPHome users, the built-in
ultrasonicplatform works out of the box.
