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)

Why makers choose the HC-SR04
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.
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.
Measure fill height
Point the sensor down into a tank or bin. The distance reading tells you how far the surface is below the sensor. Subtract from the total depth to get fill level.
Works for water, grain, pet food—any material with a reasonably flat surface. Log readings over time to track consumption or trigger refills.
Back-up distance alarm
Mount the sensor on the rear of a vehicle or garage wall. As the car approaches, beep faster or light up LEDs when distance falls below thresholds.
Pair with a Raspberry Pi and a small speaker for audible feedback, or wire directly to an Arduino with a buzzer for a standalone system.
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.



