The HC-SR04 Ultrasonic Sensor Module: Core Specs & Sensing Principle

The HC-SR04 ultrasonic sensor module measures distance by emitting a burst of eight 40kHz acoustic pulses from its piezoelectric transmitter cylinder. When these sound waves strike a solid object, they reflect back to the adjacent receiver cylinder. The module's onboard comparator circuit (typically built around an LM324 op-amp or an EM78P153N microcontroller, depending on the specific board revision) times the precise interval between the transmit trigger and the returning acoustic echo.

This time-of-flight measurement translates to a ranging capability of 2 cm to 400 cm (roughly 0.8 to 157 inches) with a strict blind zone under 2 cm where the transmitter ringing drowns out the receiver. The acoustic beam angle is approximately 15 degrees, meaning it requires a relatively flat, hard target perpendicular to the sensor face for reliable reflections. Priced around $1.50 to $2.50 in 2026, it remains the default hobbyist rangefinder despite its limitations in harsh or acoustically complex environments.

Pinout, Wiring, and Logic Level Translation

The module operates on a simple 4-pin interface. However, the most common bench mistake is wiring the Echo pin directly to a 3.3V microcontroller like the ESP32 or Raspberry Pi Pico. The HC-SR04 outputs a strict 5V TTL signal on the Echo pin; feeding this into a 3.3V GPIO will degrade the silicon over time or cause immediate latch-up failure.

Pin Function Electrical Characteristics
VCC Power Supply 5V DC nominal (4.5V to 5.5V acceptable range). Draws ~15mA active, ~2mA standby.
Trig Trigger Input Accepts 3.3V or 5V logic HIGH for minimum 10µs to initiate measurement.
Echo Echo Output Outputs 5V logic HIGH pulse. Width corresponds to time-of-flight.
GND Ground Common ground reference. Must be shared with the microcontroller.
Bench Tip: The 3.3V Voltage Divider
When wiring to an ESP32 or Raspberry Pi, you must step down the 5V Echo pin. Use a simple resistor voltage divider: connect a 1kΩ resistor in series with the Echo pin, and a 2kΩ resistor from the microcontroller GPIO to ground. This safely drops the 5V pulse to approximately 3.33V, well within the ESP32 GPIO absolute maximum ratings.
  1. Connect the HC-SR04 VCC to the 5V rail and GND to the common ground bus.
  2. Wire the Trig pin directly to your chosen microcontroller digital output (e.g., GPIO 5).
  3. Build the 1kΩ/2kΩ voltage divider on your breadboard for the Echo pin.
  4. Wire the divided Echo signal to a microcontroller digital input (e.g., GPIO 18).
  5. Verify shared ground continuity with a multimeter before applying power.

The Math: Converting Echo Pulses to Centimeters

The output of the HC-SR04 is strictly a digital 5V pulse. It does not output analog voltage, I2C data, or UART strings. The width of this HIGH pulse in microseconds (µs) represents the total time the sound wave spent traveling to the target and back. To convert this raw time into a physical distance, we use the speed of sound.

At a standard room temperature of 20°C (68°F), the speed of sound in dry air is approximately 343 meters per second, which equates to 0.0343 centimeters per microsecond. Because the pulse width accounts for the round-trip journey (there and back), we must divide the total distance by two.

Raw-to-Unit Formula:
Distance (cm) = (Pulse Width in µs × 0.0343) / 2

In Arduino or ESP32 environments, we use the pulseIn() function to measure this width. Here is the exact implementation with a hardware timeout safeguard:

const int trigPin = 5;
const int echoPin = 18;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Clear the trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Send 10us HIGH pulse to trigger
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read echo pin, timeout after 25000us (~4.3 meters)
  long duration = pulseIn(echoPin, HIGH, 25000);
  
  // Apply raw-to-unit math
  float distance_cm = (duration * 0.0343) / 2.0;
  
  if (duration == 0) {
    Serial.println("Out of range or timeout");
  } else {
    Serial.print("Distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
  }
  delay(60); // Wait 60ms between pings to avoid acoustic cross-talk
}

Signal Output, Calibration, and Interference

While the basic math works for indoor room-temperature projects, real-world physics demands calibration and an understanding of acoustic interference. The speed of sound is not a fixed constant; it scales with air temperature according to the formula v = 331.4 + (0.6 × T), where T is temperature in Celsius. If your sensor is deployed in an unheated garage at 5°C, the speed of sound drops to 334.4 m/s (0.0334 cm/µs). Using the standard 0.0343 multiplier will introduce a 2.5% error, which translates to a 10cm miscalculation at a 4-meter distance. For outdoor or variable-temperature deployments, wire a DS18B20 digital temperature sensor alongside the HC-SR04 and calculate the multiplier dynamically in your loop.

Beyond temperature, you must account for three primary interference sources that cause erratic readings:

  • Specular Reflection: If the target surface is hard but angled (like a sloped wall or a curved plastic bin), the 40kHz sound waves will bounce off at an incident angle rather than returning to the receiver. The sensor will read a timeout or maximum distance.
  • Acoustic Absorption: Soft materials like clothing, foam, or heavy curtains absorb 40kHz frequencies rather than reflecting them. The HC-SR04 is effectively blind to a person wearing a thick winter coat beyond 1.5 meters.
  • Cross-Talk and Ringing: If you are using multiple HC-SR04 modules on a single robot chassis, firing them simultaneously will cause the receivers to pick up adjacent transmitters. You must fire them sequentially with a minimum 60ms delay between pings to allow acoustic ringing to dissipate.
Debugging the 'Stuck Echo' Bug
A notorious hardware quirk in cheap clone HC-SR04 boards is the latched Echo failure mode. If the sensor is powered on while the Trig pin is floating, or if a measurement is interrupted, the Echo pin can latch permanently HIGH. If your code hangs on pulseIn(), add a 10kΩ pull-down resistor on the Echo pin, or implement a software watchdog that toggles the microcontroller GPIO to OUTPUT/LOW to force the line down if a timeout occurs.

Decision Matrix: When to Use the HC-SR04 vs Alternatives

The HC-SR04 is a fantastic educational tool and works well for basic indoor tank-level or proximity sensing. However, it is not a universal solution. Use the decision matrix below to select the correct module for your specific environmental constraints.

Environment / Constraint Limitation of HC-SR04 Recommended Alternative Exact Part Number
Indoor, dry, flat hard targets < 4m None. Ideal use case. Standard Ultrasonic HC-SR04 (Default Pick)
Outdoor, high humidity, or liquid tanks Exposed PCB and cylinders will corrode or short out. Waterproof Ultrasonic JSN-SR04T (Sealed transducer)
Measuring through plastic/glass walls 40kHz sound cannot penetrate solid barriers. Microwave Radar RCWL-0516 (Doppler radar)
High precision (< 1mm) or narrow beam required 15° beam angle is too wide; acoustic resolution is ~3mm. Time-of-Flight Laser VL53L0X (I2C ToF sensor)
Dusty environments (woodshop, grain silo) A particulates scatter and absorb acoustic waves. Millimeter Wave Radar HLK-LD2410 (24GHz FMCW)

The Final Verdict: If your project involves an indoor, dry environment where you are measuring the distance to a hard, perpendicular surface within 4 meters, buy the HC-SR04. It is cheap, heavily documented, and requires minimal code. If you are measuring outdoor water levels, building a robot that navigates through glass doors, or need sub-millimeter precision for a 3D printer bed leveler, skip the HC-SR04 entirely and source the JSN-SR04T, RCWL-0516, or VL53L0X respectively. Match the physics of the sensor to the physics of your environment.