The HC-SR04 is an ultrasonic distance sensor that measures from 2 to 400 cm by timing a 40 kHz sound pulse. It needs no library or calibration: one pin starts a reading and another returns a pulse whose length is the distance. It runs on 5V and wires straight to an Arduino; on 3.3V boards such as the ESP32 or Raspberry Pi, add two resistors to the Echo pin.
Specifications
Datasheets
What's in the box
4-pin header fitted. Jumper wires not included.
Great for
Wiring & getting started
- Find the pins
With the two round transducers facing you and the pins pointing down, the pins are VCC, Trig, Echo, GND from left to right. The names are also printed on the back.
- Wire to an Arduino
VCC to 5V, GND to GND, Trig to D9 and Echo to D10. Our female-to-male jumper wires plug straight onto the header.
- On an ESP32, Pico or Raspberry Pi
Keep VCC on the 5V pin. Trig can go straight to a 3.3V output. Run Echo through a 1k resistor to your input pin, with a 2k resistor from that pin to GND.
- Take a reading
Send a 10 µs HIGH pulse on Trig, then read the Echo pulse with pulseIn(echoPin, HIGH, 30000). Divide the microseconds by 58 for centimetres. Point it at a wall about a metre away and the number should hold steady.
