Wiring guide

How to wire an HC-SR04 to an ESP32-S3

To wire an HC-SR04 to an ESP32-S3, connect VCC to 5V, TRIG to GPIO 4, ECHO to GPIO 5 and GND to G. The sensor needs 5 V and its ECHO output is a 5 V signal, which a ESP32-S3 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
ESP32-S3 · HC-SR04live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

HC-SR04 to ESP32-S3 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 pinESP32-S3 pinWire colorWhy
VCC5VredThe HC-SR04 is a 5 V module; below about 4.5 V many units stop answering.
TRIGGPIO 4greenOutput from the board: a 10 microsecond pulse starts a ping.
ECHOGPIO 5, through R1 (1 kΩ)amberInput to the board, through the 1 k / 2 k divider: ECHO swings to 5 V (see below).
GNDGblueCommon ground.

Helper parts

The extra parts in the circuit above, and where their wires go:

FromToWire color
R1 (1 kΩ) pin 25amber
R2 (2 kΩ) pin 15amber
R2 (2 kΩ) pin 2Gblue
Voltage

Power and logic levels for HC-SR04 and ESP32-S3

Logic level

The ESP32-S3’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 5V. That is the ESP32-S3’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 5V, and its ECHO output is then a 5 V signal. The ESP32-S3 (Xtensa LX7)’s pins are not 5 V tolerant, so ECHO goes through a divider: 1 k from ECHO to GPIO 5, and 2 k from GPIO 5 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 19 and 20 are the USB port’s data lines, GPIO 35 to 37 belong to the PSRAM on octal-PSRAM boards such as the N8R8, and GPIO 0, 3, 45 and 46 are strapping pins.

The sketch

The working code for the ESP32-S3

Library: none, the sketch times the ECHO pulse with pulseIn(). This is the sketch the circuit above runs.

HC-SR04 on ESP32-S3
const int TRIG_PIN = 4;   // GPIO 4
const int ECHO_PIN = 5;   // GPIO 5

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);
}
Output

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 cm
Pitfalls

Common mistakes wiring an HC-SR04 to an ESP32-S3

ECHO straight into a 3.3 V pin

It reads correctly, so it looks fine. But ECHO swings to 5 V and the ESP32-S3 (Xtensa LX7) 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.