Component guide · Beginner · 3 min read
HC-SR04 Ultrasonic Sensor

How It Works, Wiring and Code
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
The HC-SR04 measures distance from about 2 cm to 4 m by sending a 40 kHz ultrasonic pulse and timing the echo. You trigger it with a 10 µs pulse on TRIG, then read how long ECHO stays high: distance in cm is that time in microseconds divided by 58. It runs on 5 V, so the echo pin needs a voltage divider on 3.3 V boards.
Key specs at a glance
- Supply
- 5 V DC, about 15 mA
- Range
- 2 cm to 400 cm (reliable to about 2.5 to 3 m indoors)
- Resolution
- About 3 mm
- Beam angle
- About 15 degrees
- Frequency
- 40 kHz
- Trigger
- 10 µs high pulse on TRIG
- Output
- ECHO pulse, width proportional to distance, 5 V level
- Pins
- VCC, TRIG, ECHO, GND
How an ultrasonic distance sensor works
One transducer on the HC-SR04 acts as a speaker and the other as a microphone. When TRIG goes high for 10 µs, the module sends a burst of eight 40 kHz pulses and sets ECHO high. When the reflected sound returns, ECHO goes low. Sound travels at about 343 m/s in air at 20 °C, or 0.0343 cm/µs, and the pulse travels there and back, so:
distance (cm) = echo time (µs) x 0.0343 / 2, which simplifies to echo time / 58.
HC-SR04 wiring for Arduino and ESP32
| HC-SR04 pin | Arduino Uno | ESP32 / Pico (3.3 V) |
|---|---|---|
| VCC | 5V | 5V (VIN / VBUS) |
| TRIG | D9 | Any output GPIO (3.3 V is enough to trigger) |
| ECHO | D10 | Through a 1 kΩ / 2 kΩ voltage divider to an input GPIO |
| GND | GND | GND |
- VCC
- Arduino Uno:
- 5V
- ESP32 / Pico (3.3 V):
- 5V (VIN / VBUS)
- TRIG
- Arduino Uno:
- D9
- ESP32 / Pico (3.3 V):
- Any output GPIO (3.3 V is enough to trigger)
- ECHO
- Arduino Uno:
- D10
- ESP32 / Pico (3.3 V):
- Through a 1 kΩ / 2 kΩ voltage divider to an input GPIO
- GND
- Arduino Uno:
- GND
- ESP32 / Pico (3.3 V):
- GND
Arduino code for the HC-SR04
This sketch prints the distance twice a second. The timeout in pulseIn stops the code hanging when nothing is in range.
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000); // 30 ms timeout, about 5 m
if (us == 0) {
Serial.println("Out of range");
} else {
Serial.print(us / 58.0);
Serial.println(" cm");
}
delay(500);
}How to get accurate readings
Cloth, foam, fur and steep angles absorb or scatter the pulse.
so old echoes do not return as false readings.
to remove spikes.
in precise work: the speed of sound rises by about 0.6 m/s per °C.
or firing at the same time; they hear each other's pulses.
HC-SR04 vs HC-SR04P vs waterproof JSN-SR04T
| Sensor | Supply | Range | Choose it for |
|---|---|---|---|
| HC-SR04 | 5 V | 2 to 400 cm | Arduino Uno projects, classroom kits |
| HC-SR04P / RCWL-1601 | 3 to 5.5 V | 2 to 400 cm | ESP32, Pico and battery builds, no divider needed |
| JSN-SR04T / AJ-SR04M | 5 V | 20 to 600 cm | Water tanks, outdoor and parking sensors; waterproof probe |
| VL53L0X (laser ToF) | 2.8 to 5 V | 3 to 200 cm | Small targets, narrow beam, I2C |
- HC-SR04
- Supply:
- 5 V
- Range:
- 2 to 400 cm
- Choose it for:
- Arduino Uno projects, classroom kits
- HC-SR04P / RCWL-1601
- Supply:
- 3 to 5.5 V
- Range:
- 2 to 400 cm
- Choose it for:
- ESP32, Pico and battery builds, no divider needed
- JSN-SR04T / AJ-SR04M
- Supply:
- 5 V
- Range:
- 20 to 600 cm
- Choose it for:
- Water tanks, outdoor and parking sensors; waterproof probe
- VL53L0X (laser ToF)
- Supply:
- 2.8 to 5 V
- Range:
- 3 to 200 cm
- Choose it for:
- Small targets, narrow beam, I2C
Try it: HC-SR04 ultrasonic distance calculator
Change the values and the diagram and results update as you type.
Measure distance with an HC-SR04 and Arduino Uno
You need: Arduino Uno, HC-SR04 sensor, 4 jumper wires, Breadboard (optional) · Tools: Computer with Arduino IDE, USB cable
Get the parts3 of 4 found in our store · ₹666
Arduino UnoArduino Uno R3 Development Board with USB Cable₹385
HC-SR04 sensorHC-SR04 Ultrasonic Sensor with Acrylic Mounting Bracket₹85
4 jumper wiresDupont Jumper Wires (40 pcs)₹49- Breadboard (optional)Find this part
- 1Wire powerConnect VCC to 5V and GND to GND on the Uno.
- 2Wire signalsConnect TRIG to D9 and ECHO to D10.
- 3Upload codeUpload the sketch from this guide and open the Serial Monitor at 9600 baud.
- 4TestHold a book 10 to 100 cm in front of the sensor and check the printed distance with a ruler.
Common mistakes to avoid
- Connecting ECHO directly to an ESP32 or Pico pin; the 5 V pulse can damage it.
- Pinging too fast, which produces ghost readings from earlier echoes.
- Mounting the sensor behind a hole that is too small, so the housing reflects the pulse.
- Using pulseIn without a timeout, which freezes the loop for a second when nothing is in range.
- Expecting a reading under 2 cm; the sensor cannot hear an echo that soon.
Frequently asked questions
What is the range of the HC-SR04?
2 cm to 400 cm on paper. Indoors on typical targets, expect reliable readings to about 2.5 to 3 m.
Can I use the HC-SR04 with a 3.3 V board?
Power it from 5 V and put a voltage divider on ECHO, or buy the HC-SR04P version that runs on 3.3 V.
Can the HC-SR04 measure water level?
Yes, by pointing it down at the surface, but condensation will eventually damage it. A waterproof JSN-SR04T lasts much longer in tanks.
Why does my HC-SR04 always read 0 or a fixed value?
Check that TRIG and ECHO are not swapped, that the module has 5 V, and that you use a timeout. A reading stuck near the maximum usually means the echo is being absorbed or the target is at an angle.
Do I need a library for the HC-SR04?
No, pulseIn is enough. Libraries such as NewPing add timeouts, median filtering and support for many sensors.
Parts for this guide
Terms in this guide
Projects that use it
Put this into practice: 71 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.

