The HC-SR04 Sensor: Direct Answer and Core Specs

The HC-SR04 sensor outputs a digital 5V pulse on its Echo pin, where the pulse width in microseconds corresponds to the round-trip time of a 40kHz sound wave. To convert this raw reading to centimeters, divide the microsecond pulse width by 58.3 (or multiply by 0.01715). It requires a 5V power supply (4.5V to 5.5V acceptable) and strictly outputs 5V logic on the Echo pin. If you are interfacing with a 3.3V microcontroller like the ESP32, Raspberry Pi Pico, or Raspberry Pi, you must use a resistor voltage divider on the Echo pin to avoid destroying the GPIO.

How Ultrasonic Time-of-Flight Actually Works

The HC-SR04 relies on a pair of piezoelectric transducers: a transmitter (TX) and a receiver (RX). When the microcontroller pulls the Trigger pin HIGH for at least 10 microseconds, the sensor's internal logic chip commands the TX crystal to vibrate, emitting an eight-cycle burst of ultrasound at 40kHz. This frequency is chosen because it sits well above human hearing and provides a tight beam angle (roughly 15 degrees) for reasonable spatial resolution.

Simultaneously, the internal logic flips the Echo pin HIGH and starts a timer. The RX transducer listens for the acoustic reflection. When the echo is detected, or when a hardware timeout of roughly 38 milliseconds occurs (equating to a maximum range of about 4 to 5 meters), the Echo pin is pulled LOW. The microcontroller measures this HIGH duration to calculate the distance.

Wiring, Pinout, and the 3.3V Logic Trap

The most common way hobbyists brick their ESP32 or Raspberry Pi is by wiring the HC-SR04 Echo pin directly to a 3.3V GPIO. The HC-SR04 is a 5V device. While the Trigger pin is generally tolerant of 3.3V logic inputs (it will reliably register a 3.3V HIGH signal from an ESP32), the Echo pin will output a full 5V pulse. Feeding 5V into a 3.3V microcontroller pin violates the absolute maximum ratings outlined in the Espressif ESP32 datasheet, leading to degraded silicon, erratic ADC readings, or immediate thermal failure of the GPIO pad.

WARNING: Never connect the HC-SR04 Echo pin directly to an ESP32, ESP8266, or Raspberry Pi GPIO. Always use a voltage divider.

Spec Sheet and Wiring Table

PinFunctionVoltage / SignalMicrocontroller Connection
VCCPower Supply5V (4.5V - 5.5V range)5V pin on Arduino, or 5V (VBUS) pin on ESP32/RPi
TrigTrigger InputDigital HIGH (10µs min)Direct to any digital GPIO (3.3V or 5V)
EchoOutput PulseDigital HIGH (5V)Via Voltage Divider to any digital GPIO
GNDGround0VCommon Ground with microcontroller

Building the 3.3V Voltage Divider

To drop the 5V Echo signal down to a safe 3.3V, use a simple two-resistor divider:

  1. Connect a 1kΩ resistor between the HC-SR04 Echo pin and the ESP32/RPi GPIO pin.
  2. Connect a 2kΩ resistor between that same GPIO pin and the common GND.
  3. This yields an output of 5V * (2000 / (1000 + 2000)) = 3.33V, which is perfectly safe for 3.3V logic.

Output Signal Math: Raw Pulse to Centimeters

The output of the HC-SR04 is strictly a digital time-domain pulse, not an analog voltage. To get a physical distance, you must measure the pulse width and apply the physics of sound.

According to Engineering Toolbox acoustic data, the speed of sound in dry air at 20°C (68°F) is approximately 343 meters per second. Let's break down the raw-to-unit math:

  • Speed of sound = 343 m/s = 34,300 cm/s = 0.0343 cm/µs.
  • The sound wave travels to the object and back, so the physical distance is half the total travel time.
  • Distance (cm) = (Pulse Width in µs × 0.0343 cm/µs) / 2.
  • Distance (cm) = Pulse Width in µs × 0.01715.
  • Conversely, Distance (cm) = Pulse Width in µs / 58.3.
Bench Tip: In Arduino/ESP32 C++ code, dividing by 58.0 is computationally faster and standard practice for quick prototyping, though multiplying by 0.01715 avoids the floating-point division overhead on slower 8-bit AVRs.

Temperature Calibration

The "divide by 58" rule assumes 20°C. If your project operates in a freezing garage (0°C) or a hot attic (40°C), your readings will drift because the speed of sound changes by roughly 0.6 m/s per degree Celsius. The formula is v = 331.4 + (0.6 * TempC). If you have a DHT22 or BME280 on your board, update your divisor dynamically in code for millimeter-level accuracy.

Interference, Failure Modes, and Calibration

The HC-SR04 is notoriously susceptible to environmental interference. Understanding these failure modes prevents hours of debugging phantom readings.

  • Specular Reflection (Angled Surfaces): If a sound wave hits a smooth wall at an angle greater than 15 degrees, it reflects away from the RX transducer like light off a mirror. The sensor will time out and return a maximum distance error (often 0 cm or 400+ cm depending on the library).
  • Acoustic Absorption: Soft materials like foam, heavy curtains, or fiberglass insulation absorb 40kHz frequencies. The sensor will fail to detect a couch or a bed, reading the wall behind it instead.
  • Crosstalk: If you mount multiple HC-SR04 sensors facing the same area, the RX of Sensor A will pick up the TX burst from Sensor B. You must fire them sequentially in code, waiting for a 50ms dead-band between readings.
  • Minimum Blanking Distance: The sensor cannot measure objects closer than ~2 cm. During the 8-cycle TX burst, the RX transducer is ringing and deaf to incoming echoes.

Decision Tree: HC-SR04 vs. The Alternatives

Do not default to the HC-SR04 for every project. Use this decision matrix to select the correct ranging technology for your specific environment. For a deeper dive into ESP32 integration patterns, refer to this comprehensive ESP32 ultrasonic guide by Random Nerd Tutorials.

Application ScenarioRequired TraitSensor PickApprox. Cost (2026)
Indoor robotics, dry environment, flat hard targets, tight budget. Low cost, basic cm-level resolution. HC-SR04 $1.50 - $2.50
Outdoor weather stations, automotive parking aids, damp basements. Waterproof, dust-proof, sealed transducer. JSN-SR04T (Waterproof variant) $4.00 - $6.00
Liquid level sensing in narrow PVC pipes, or detecting soft foam/fabric. Immune to acoustic absorption, tight beam. TF-Luna (850nm LiDAR / Time-of-Flight) $6.00 - $9.00
Detecting human presence or motion through drywall or plastic enclosures. Penetrates non-metallic solids, wide field of view. RCWL-0516 (Microwave Radar) $1.50 - $3.00

The Final Verdict

If your project involves indoor, dry, hard-surface distance measurement on a sub-$3 budget, buy the standard HC-SR04 and build the 1k/2k voltage divider. If your sensor will be exposed to humidity, condensation, or outdoor weather, buy the JSN-SR04T; the standard HC-SR04's exposed mesh will corrode and short out within weeks in high humidity. If you need to measure soft materials or require millimeter precision regardless of temperature, abandon ultrasonics entirely and buy the TF-Luna LiDAR module.