HC-SR04: pinout and how to wire the ultrasonic sensor
The HC-SR04 measures distances from 2 cm to 4 m by sending an ultrasonic pulse and timing the echo. See the pinout, how to wire it to an Arduino or ESP32 and test the pulse in the simulator.
Specifications
- Measures
- Distance
- Supply
- 5 V
- Range
- 2 cm to 400 cm
- Accuracy
- ≈ 3 mm
- Measuring angle
- ≈ 15°
- Frequency
- 40 kHz
- Trigger (TRIG)
- 10 µs pulse
- Current
- ≈ 15 mA
Pinout
With the two "eyes" (transducers) facing you and the pins at the bottom, the order printed on the board is the same as in the table. Always check the silkscreen of your module before wiring.
HC-SR04 (4 pins)
| 1 | VCC | Power supply, 5 V |
| 2 | TRIG | Input: a 10 µs pulse starts a measurement |
| 3 | ECHO | Output: stays high for the duration of the echo |
| 4 | GND | Ground |
HC-SR04 simulator
Move the obstacle closer or farther and see the width of the pulse the sensor returns on the ECHO pin.
distance = time × speed / 2
The ECHO pin stays high for the whole time the sound took to reach the obstacle and come back. The code times that width with pulseIn() and divides by 2, because the path was covered twice.
Below 2 cm the sensor cannot measure; above 400 cm the echo is too weak.
How to wire it
Connect VCC to 5 V and GND to the board ground. Connect TRIG to a digital output pin (for example D9 on the Arduino Uno) and ECHO to a digital input pin (for example D10).
To measure, the code sets TRIG high for 10 µs and then times how long ECHO stays high with pulseIn(). That time is the sound's round trip.
The distance is the time in microseconds divided by 58 (result in cm). The math comes from the speed of sound, ≈ 340 m/s: distance = time × 0.034 / 2.
On the ESP32 and ESP8266 ECHO outputs 5 V while the pins accept 3.3 V at most. Use a voltage divider (for example 1 kΩ and 2 kΩ) on ECHO, or a 3.3 V HC-SR04 module.
Wait at least 60 ms between measurements so the previous echo dies out before the next trigger.
Example code (Arduino)
No library needed: the sketch below sends the pulse and converts the echo time to centimeters.
#define TRIG_PIN 9
#define ECHO_PIN 10
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); // 10 us pulse starts the measurement
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long time = pulseIn(ECHO_PIN, HIGH, 30000); // 30 ms: up to ~5 m
float cm = time / 58.0;
if (time == 0) {
Serial.println("No echo (out of range)");
} else {
Serial.print("Distance: ");
Serial.print(cm);
Serial.println(" cm");
}
delay(100);
}
Common mistakes
- Wiring ECHO straight to a 3.3 V pin (ESP32, ESP8266, Raspberry Pi): the 5 V signal can damage the pin. Use a voltage divider.
- Swapping TRIG and ECHO: the reading is always zero or hangs waiting for the echo.
- Measuring soft surfaces (fabric, foam) or steeply angled ones: they absorb or deflect the sound and no echo comes back.
- Triggering measurements less than 60 ms apart: the previous echo is read as if it were the new one.
- Forgetting the timeout in pulseIn(): with no obstacle the reading waits a full second and stalls the program.
Always check the manufacturer's datasheet for your exact model: pin order can vary between modules.
Frequently asked questions
How does the HC-SR04 ultrasonic sensor work?
One transducer emits 8 bursts of 40 kHz sound when it receives the TRIG pulse. The other listens for the echo. The ECHO pin stays high for as long as the sound took to travel out and back, and the microcontroller converts that time into distance.
What is the HC-SR04 distance formula?
distance (cm) = ECHO pulse time (µs) / 58. Or, using the speed of sound: distance = time × 0.034 / 2, divided by 2 because the sound goes out and back.
Does the HC-SR04 work at 3.3 V?
The common model needs 5 V and ECHO outputs 5 V. With an ESP32 use a voltage divider on ECHO, or buy the 3.3 V version of the HC-SR04.
What is the maximum range?
About 4 m under ideal conditions (a flat surface facing the sensor). The minimum is 2 cm: closer than that the echo arrives before the sensor finishes transmitting.
Does temperature affect the reading?
Slightly: the speed of sound rises about 0.6 m/s per degree. For precise projects, compensate with a temperature sensor.