The HC-SR04 ultrasonic sensor outputs a digital 5V TTL time-pulse (not an analog voltage) on its Echo pin, representing the round-trip travel time of a 40 kHz sound burst. To convert this raw microsecond reading to centimeters, divide the pulse width by 58.3. It requires a 5V supply (4.5V–5.5V range) and a minimum 10µs HIGH trigger pulse to initiate a reading.
The HC-SR04 Sensing Principle
The HC-SR04 measures distance by emitting a 40 kHz ultrasonic burst and listening for the acoustic reflection. When the Trig pin receives a 10µs HIGH pulse, the module's internal ASIC fires eight 40 kHz cycles through the transmitter transducer and immediately pulls the Echo pin HIGH.
The Echo pin stays HIGH until the reflected sound wave hits the receiver transducer, at which point the internal comparator drops the pin LOW. The duration of this HIGH pulse is strictly proportional to the round-trip travel time of the sound wave through the air, giving you a raw time measurement that must be mathematically scaled to a physical distance.
Pinout, Supply Range, and ESP32 Wiring
A critical detail often missed in generic tutorials is that the HC-SR04 is a 5V device. While the Trig pin will reliably register a 3.3V logic HIGH from an ESP32 or Raspberry Pi, the Echo pin outputs a full 5V TTL signal. Feeding 5V directly into a 3.3V ESP32 GPIO will eventually degrade or destroy the silicon. You must use a voltage divider on the Echo line.
| HC-SR04 Pin | Function | Supply / Logic Range | ESP32 Connection |
|---|---|---|---|
| VCC | Power Supply | 4.5V to 5.5V (Draws ~15mA active) | ESP32 5V (VIN) Pin |
| Trig | Trigger Input | Accepts 3.3V or 5V HIGH (Min 10µs) | GPIO 5 (Direct) |
| Echo | Echo Output | Outputs 5V HIGH / 0V LOW | GPIO 18 (Via Voltage Divider) |
| GND | Ground | 0V Reference | ESP32 GND |
To step the 5V Echo signal down to a safe 3.3V for the ESP32, use a 1kΩ resistor in series with the Echo pin, and a 2kΩ resistor from the ESP32 GPIO to GND. This yields roughly 3.33V, well within the ESP32's 3.6V absolute maximum rating. Do not skip this step; I have seen bricked ESP32-WROOM-32 modules from developers who assumed the internal clamping diodes would save them.
Output Signal Math: Raw Pulse to Centimeters
The raw output of the HC-SR04 is a pulse width measured in microseconds (µs). To convert this to centimeters, we rely on the speed of sound. At standard room temperature (20°C / 68°F), sound travels through dry air at approximately 343 meters per second, which translates to 0.0343 centimeters per microsecond.
Because the sound wave must travel to the target and back, the measured time is exactly double the actual distance. Therefore, the base formula is:
Distance (cm) = (Pulse Width in µs × 0.0343) / 2
Simplifying the constants (1 / (0.0343 / 2)), we get the standard divisor used in almost every Arduino library:
Distance (cm) = Pulse Width in µs / 58.3
Temperature Calibration and Scaling
If your project operates in an unheated garage or an outdoor enclosure, the static 58.3 divisor will introduce errors. The speed of sound changes with ambient temperature. According to standard acoustic physics models referenced by the Physics Classroom, the speed of sound in air is calculated as v = 331.4 + (0.6 × T) where T is temperature in Celsius.
To dynamically scale your reading, replace the static divisor with this temperature-compensated formula:
Dynamic Divisor = 20000 / (331.4 + (0.6 × tempC))
At 0°C, the divisor drops to 59.1. At 35°C, it drops to 57.2. If you are measuring a 200cm distance, failing to compensate for a 35°C environment will result in a ~3cm measurement error. For high-precision applications, pair the HC-SR04 with a BME280 sensor to feed live temperature data into your scaling math.
Interference, Calibration, and Failure Modes
The HC-SR04 is a $1.50 sensor, and its failure modes reflect that price point. Understanding these edge cases will save you hours of debugging.
- Specular Reflection (Angled Surfaces): The 40 kHz transducers have a beam angle of roughly 15 degrees. If the target surface is angled more than 20 degrees away from perpendicular, the sound wave reflects away from the receiver, resulting in a timeout (returning 0 or the maximum
pulseInlimit). - Acoustic Absorption: Soft materials like foam, heavy curtains, or fiberglass insulation absorb 40 kHz frequencies rather than reflecting them. The sensor will read these as 'out of range'.
- Cross-Talk Interference: If you are using multiple HC-SR04 modules on the same robot or tank, firing them simultaneously will cause the receivers to pick up neighboring transmitters. You must fire them sequentially in code, with a minimum 50ms delay between readings to allow stray echoes to dissipate.
- The Blind Zone: The datasheet specifies a minimum range of 2cm. Physically, the transmitter ring continues to vibrate (ring down) for a few hundred microseconds after the burst. If an object is closer than 2cm, the echo returns while the receiver is still deafened by the transmitter's ring-down, resulting in a false 0cm reading.
When using the standard Arduino pulseIn() function, always set a timeout parameter. A raw pulseIn(echoPin, HIGH) without a timeout will halt your ESP32's execution for up to 1 second if an echo is never received. Always use pulseIn(echoPin, HIGH, 30000) to cap the wait at 30,000µs (roughly 5 meters).
HC-SR04 Sensor Datasheet FAQ
Does the HC-SR04 sensor datasheet specify a minimum trigger pulse width?
Yes. The internal ASIC requires the Trig pin to be held HIGH for a minimum of 10 microseconds to initiate the 8-cycle burst. If your microcontroller is running heavy interrupt loads (like WiFi stack management on an ESP32), a standard digitalWrite sequence might result in a trigger pulse shorter than 10µs due to context switching. For rock-solid reliability in RTOS environments, use hardware timer interrupts or the ESP-IDF pulse counter (pcnt) peripheral to generate the trigger and measure the echo without CPU blocking.
Can I power the HC-SR04 directly from a 3.3V Raspberry Pi or ESP32 pin?
Technically, some modern 'clone' boards with low-dropout regulators will oscillate at 3.3V, but it is strictly outside the datasheet specifications. The 40 kHz piezoelectric transducers require sufficient voltage swing to generate adequate acoustic pressure. Running the VCC at 3.3V drastically reduces the maximum range (often dropping it from 400cm to under 100cm) and increases the noise floor. Always power the VCC pin from a 5V source, and use the voltage divider on the Echo pin to protect your 3.3V logic.
Why does my HC-SR04 output randomly read 0 cm or spike to 2000 cm?
A reading of 0 cm usually means the echo returned before the receiver recovered from the transmitter's ring-down (object is < 2cm away), or the pulseIn function timed out because the sound was absorbed. A massive spike like 2000 cm (or whatever your timeout limit equates to) means the Echo pin never dropped LOW. This is almost always caused by cross-talk from another ultrasonic sensor, a missed echo due to a highly angled surface, or a missing pull-down resistor on the Echo line allowing it to float HIGH when the module's output transistor is off. Adding a 10kΩ pull-down resistor between the Echo pin and GND at the module side resolves most floating-pin ghost readings.






