How to wire an HC-SR04 to an ESP32
To wire an HC-SR04 to an ESP32, connect VCC to VIN (5 V from USB), TRIG to GPIO 4 (D4), ECHO to GPIO 5 (D5) and GND to GND. The sensor needs 5 V and its ECHO output is a 5 V signal, which a ESP32 pin cannot take, so ECHO goes through a 1 kilohm and 2 kilohm divider. The sketch below uses no library beyond what the board includes. It sends a ping every 100 ms and prints the distance in centimeters.
- 3.3 V logic
- Library: none
- 4 wires, 2 helper parts
HC-SR04 to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| HC-SR04 pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | VIN (5 V from USB) | red | The HC-SR04 is a 5 V module; below about 4.5 V many units stop answering. |
| TRIG | GPIO 4 (D4) | green | Output from the board: a 10 microsecond pulse starts a ping. |
| ECHO | GPIO 5 (D5), through R1 (1 kΩ) | amber | Input to the board, through the 1 k / 2 k divider: ECHO swings to 5 V (see below). |
| GND | GND | blue | Common ground. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| R1 (1 kΩ) pin 2 | D5 | amber |
| R2 (2 kΩ) pin 1 | D5 | amber |
| R2 (2 kΩ) pin 2 | GND | blue |
Power and logic levels for HC-SR04 and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The HC-SR04 takes 5 V from VIN (5 V from USB). That is the ESP32’s 5 V rail, so the part shares the current the board can supply.
This pair
The HC-SR04 needs 5 V, so VCC goes to VIN (5 V from USB), and its ECHO output is then a 5 V signal. The ESP32 (Xtensa LX6)’s pins are not 5 V tolerant, so ECHO goes through a divider: 1 k from ECHO to GPIO 5 (D5), and 2 k from GPIO 5 (D5) to GND, which brings 5 V down to 3.3 V. TRIG needs nothing; 3.3 V is enough to trigger it.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: none, the sketch times the ECHO pulse with pulseIn(). This is the sketch the circuit above runs.
const int TRIG_PIN = 4; // GPIO 4 (D4)
const int ECHO_PIN = 5; // GPIO 5 (D5)
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// A pulse of at least 10 us on TRIG starts one measurement.
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// ECHO stays high for the sound's round trip; 38 ms means nothing in range.
unsigned long us = pulseIn(ECHO_PIN, HIGH, 38000UL);
if (us == 0) {
Serial.println("Out of range");
} else {
Serial.print("Distance: ");
Serial.print(us / 58);
Serial.println(" cm");
}
delay(100);
}
What you should see
Open the serial monitor at 115200 baud. Each ping prints one line. Set the sensor’s distance to 150 cm and the line reads Distance: 150 cm; the sketch divides the ECHO pulse in microseconds by 58 to get centimeters.
When nothing is in range, or ECHO is not connected, the pulse never arrives and the sketch prints Out of range after its 38 ms timeout.
The serial monitor prints this, recorded from the circuit above with the distance set to 150 cm after the first 300 ms:
Distance: 100 cm
Distance: 150 cmCommon mistakes wiring an HC-SR04 to an ESP32
ECHO straight into a 3.3 V pin
It reads correctly, so it looks fine. But ECHO swings to 5 V and the ESP32 (Xtensa LX6) is rated to 3.6 V, so the pin is overstressed on every ping. Add the 1 k and 2 k divider, or use an HC-SR04P, the 3.3 V version.
pulseIn() without a timeout
With nothing in front of the sensor, pulseIn() with no third argument waits a whole second for a pulse that never comes, and the sketch looks frozen. Pass a timeout of about 38000 microseconds.
Pinging too fast
Fire the next ping before the last echo has died away and the module hears its own previous ping, so short random distances appear. Leave at least 60 ms between measurements.
Build your own HC-SR04 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.