HC-SR04 echo pin 5 V into ESP32 GPIO: use a voltage divider

The echo pin of a standard HC-SR04 gives about 5 V, which is above the safe limit of an ESP32 input. Reduce it to 3.3 V.

What the message means

ESP32 GPIO pins are meant for 3.3 V. A 5 V signal can stress the pin over time or damage it.

Fix it in this order

  1. Put two resistors in series from echo to ground, for example 1 kohm from echo to the pin, and 2 kohm from the pin to ground.
  2. Or use a level shifter module on the echo line.
  3. Or buy a 3.3 V version of the sensor if available.
  4. Power the sensor from 5 V and join the grounds.
  5. The trigger pin can usually take the 3.3 V signal from the ESP32 directly.

Why it happens

The HC-SR04 runs on 5 V and its Echo pin outputs a 5 V pulse. ESP32 pins are 3.3 V and are not 5 V tolerant, so connecting Echo directly can damage the pin over time. Trigger is fine: a 3.3 V output is normally enough to start the sensor.

The divider

Use a resistor divider on Echo: 1 kΩ from Echo to the GPIO, and 2 kΩ from the GPIO to ground. That gives about 5 V × 2 / 3 ≈ 3.3 V. Two 1 kΩ in series for the lower leg also work. A 3.3 V-capable module version avoids the divider.

If you see this, do this

You seeDo this
Random distances on ESP32Check power and grounds, then the divider.
Reads zeroSee the HC-SR04 returns zero page.

HC-SR04 returns zero, wiring checker.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

The HC-SR04 has four pins and is built for a 5 V supply. Its Echo output swings between 0 V and about 5 V. An ESP32 input is designed for 3.3 V, and its absolute maximum rating is a little above that supply. A 5 V signal can push current through the input protection diodes inside the chip. This may not fail at once; it can age the pin or shorten its life.

A divider works by sharing the voltage between two resistors in series, and the output is Vout = Vin x R2 / (R1 + R2). With a 1 kohm top resistor and 2 kohm bottom resistor, Vout = 5 x 2 / 3 = 3.33 V. The ESP32 reads a high input at roughly 0.75 times its supply, about 2.5 V, but check the datasheet. The divider must be low enough in resistance to keep the edge fast, so values in the 1 to 10 kohm range are normal.

How to confirm the cause

  • Before connecting the divider to the ESP32, power it from 5 V alone and measure the middle point with a multimeter. You should see about 3.3 V.
  • Check that the 1 kohm resistor is between Echo and the GPIO, and the 2 kohm is between the GPIO and ground. If they are swapped, the output is 5 x 1 / 3 = 1.67 V, too low to read as high.
  • Check with a ruler. Place a flat wall at 20 cm and compare the reading. A steady, correct reading shows the divider works.
  • Measure the 5 V supply at the sensor while it runs. A USB port under load can sag.
  • Confirm the ground of the sensor joins the ESP32 ground.

Worked example

Reading the distance on an ESP32 with a divider on Echo. Pins 5 and 18 are examples. Sound travels at about 343 m/s, which is 0.0343 cm per microsecond. An echo of 1166 microseconds gives 1166 x 0.0343 / 2 = 20.0 cm.

const int TRIG = 5, ECHO = 18;      // ECHO through the divider
void setup() {
  Serial.begin(115200);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}
void loop() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000);
  Serial.println(us ? us * 0.0343 / 2 : -1);   // -1 means no echo
  delay(60);
}

Common mistakes

  • Connecting Echo straight to the GPIO because “it worked once”.
  • Putting resistors in the wrong order in the divider.
  • Choosing very large values such as 100 kohm and 200 kohm, which slow the signal edge and give wrong timing.
  • Powering the sensor from the 3.3 V pin and expecting a stable reading.
  • Treating a reading of -1 or 0 as a distance, when it means that no echo returned.

If this did not fix it

  • Check the sensor supply. A sensor on a weak 5 V line gives a short or missing echo.
  • Check for a 3.3 V variant of the sensor, or a level shifter module, if the divider gives unreliable edges.
  • Check the object in front of the sensor. Soft or angled surfaces return little sound.