Components

HC-SR04 Ultrasonic Distance Sensor

Measure distances from 2 cm to 4 meters with sound waves. The HC-SR04 is the standard obstacle-detection module for Raspberry Pi and Arduino robots—four pins, two GPIO connections, and you're reading distance in centimeters.

  • Range 2 cm – 4 m
  • Angle 15°
  • Voltage 5V DC
  • Pins 4 (VCC, Trig, Echo, GND)
HC-SR04 ultrasonic distance sensor module with dual transducers and four-pin header

Why makers choose the HC-SR04

  • Universal compatibility

    Works with any microcontroller that has GPIO pins—Raspberry Pi, Arduino, ESP32. 5V power, digital trigger and echo signals.

  • Simple two-wire control

    Send a 10 µs pulse to the trigger pin, measure the echo pulse width, divide by 58 to get distance in centimeters. No libraries required, though many exist.

  • Reliable non-contact sensing

    Ultrasonic waves bounce off solid surfaces up to 4 meters away. No calibration needed, and it works in darkness where infrared struggles.

  • Proven in thousands of projects

    The HC-SR04 is the most documented distance sensor in robotics education. Code examples, wiring diagrams, and troubleshooting guides are everywhere.

How ultrasonic ranging works

The HC-SR04 sends a burst of 40 kHz sound, waits for the echo, and tells your microcontroller how long the round trip took. You convert time to distance.

  • Trigger the pulse

    Pull the Trig pin high for 10 microseconds. The sensor emits eight cycles of ultrasonic sound at 40 kHz.

  • Wait for the echo

    The Echo pin goes high while the sensor listens. When the reflected sound returns, the pin goes low.

  • Measure the pulse width

    Time how long the Echo pin stayed high. Divide by 58 (or multiply by 0.017) to convert microseconds into centimeters.

  • Read the distance

    Your code now has the distance to the nearest obstacle. Use it to stop before a wall, follow a target, or map a room.

Technical specifications

Model
HC-SR04
Operating voltage
5V DC
Current draw
15 mA typical
Measurement range
2 cm to 4 m
Effective angle
15° cone
Ranging accuracy
±3 mm
Trigger input
10 µs TTL pulse
Echo output
TTL signal, pulse width proportional to distance
Dimensions
45 × 20 × 15 mm (module with transducers)
Pins
4-pin header (VCC, Trig, Echo, GND)

Common applications

The HC-SR04 is a workhorse for any project that needs to know how far away something is.

Stop before you hit the wall

Mount the sensor on the front of your robot. Poll it in your main loop. When distance drops below a threshold—say, 20 cm—stop the motors, back up, and turn. This is the classic first robotics project.

Use two or three sensors (front, left, right) to detect obstacles in multiple directions and navigate around them autonomously.

Frequently asked questions

Can I use this sensor with a 3.3V microcontroller?

The HC-SR04 requires 5V power. The Trig pin accepts 3.3V logic, but the Echo pin outputs 5V, which can damage 3.3V GPIO. Use a voltage divider (two resistors) or a level shifter on the Echo line when connecting to Raspberry Pi or ESP32.

Why do I get erratic readings or no reading at all?

Check your wiring first—VCC to 5V, GND to ground, Trig and Echo to GPIO pins. Make sure the timing code sends a clean 10 µs trigger pulse. Soft surfaces (fabric, foam) absorb ultrasound and won't reflect well; angled surfaces deflect the beam away. The sensor also struggles with objects closer than 2 cm or at the edge of the 15° cone.

How fast can I take measurements?

The HC-SR04 needs about 60 ms per reading at maximum range (time for sound to travel 8 meters round trip at 340 m/s). In practice, poll every 100 ms for reliable results. Faster polling works if your target is closer, but don't exceed the physical speed of sound.

Does this sensor work outdoors or in noisy environments?

Yes, but ultrasonic sensors can be affected by wind, temperature gradients, and ambient noise at 40 kHz (though that frequency is above human hearing and rare in nature). For outdoor robotics, consider mounting the sensor where it's shielded from direct airflow, or use multiple sensors and filter outliers in software.

What's the difference between HC-SR04 and HC-SR04+?

The HC-SR04+ (note the plus) runs on 3.3V to 5.5V and has 3.3V-safe Echo output, making it directly compatible with Raspberry Pi GPIO without level shifting. If you see "HC-SR04+" in the product name, that's the version to choose for 3.3V systems. The standard HC-SR04 is 5V only.