Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

HC-SR04 with ESP32-S3-DevKitC-1 — divider and median filter

Measure distance with an HC-SR04 on an ESP32-S3-DevKitC-1: 5V supply, Trig GPIO6, Echo GPIO7 through a 1 kΩ / 2 kΩ divider, median of five pings.

Arduino C++Verified — core esp32 3.3.12, 3 Nov 2026Compile-only

Community draft — report a mistake

The HC-SR04 is cheap and accurate to about a centimetre, but it is a 5 V part. On the S3-DevKitC-1, power it from the 5V pin and protect GPIO7 with a two-resistor divider on ECHO.

The sketch fires five pings per reading and prints the middle value, a simple filter that makes the output steady enough for tank levels and parking sensors.

Wiring

ESP32-S3-DevKitC-1Only the pins used are shown · 3.3 V logicHC-SR04 ultrasonic sensor1k│2k6 · 67 · 75VGNDVCCTrigEchoGND
Also needed: 1 kΩ resistor, 2 kΩ resistor (or two 1 kΩ in series).
Every connection
ESP32-S3-DevKitC-1HC-SR04 ultrasonic sensorWireNotes
5VVCCred5 V from USB.
6Trigpurple
7EchogreenVia 1 kΩ / 2 kΩ divider — 1 kΩ from ECHO to GPIO7, 2 kΩ from GPIO7 to GND → about 3.3 V.
GNDGNDblack

5 V (HC-SR04P / RCWL-1601: 3–5.5 V) part, 3.3 V board. The classic HC-SR04 needs 5 V and its ECHO pin outputs 5 V. On a 3.3 V board (ESP32, Pico) send ECHO through a 1 kΩ / 2 kΩ divider. A 3.3 V trigger signal is enough for TRIG.

Board quirks

  • The S3’s GPIOs are 3.3 V only. ECHO is a 5 V pulse, so it goes through the divider; TRIG works fine at 3.3 V.
  • GPIO6/7 are on the same header as 3V3 and 5V, so all four wires land close together. They have no boot role.
  • The sketch takes the median of five pings, 60 ms apart — that removes the odd wild reading from a stray echo, which matters for a water-tank level sensor.

Code

hc-sr04-esp32s3.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// HC-SR04 ultrasonic sensor on an ESP32-S3-DevKitC-1.
// VCC → 5V, GND → GND, Trig → GPIO6, Echo → GPIO7 through a 1 kΩ / 2 kΩ divider
// (ECHO is 5 V; the S3's pins are 3.3 V only).
constexpr uint8_t TRIG_PIN = 6;
constexpr uint8_t ECHO_PIN = 7;

float readCm() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);
  return us ? us * 0.0343f / 2.0f : -1;
}

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);
  pinMode(ECHO_PIN, INPUT);
  Serial.println("HC-SR04: Trig GPIO6, Echo GPIO7");
}

void loop() {
  // Median of 5 pings rejects the odd stray echo.
  float r[5];
  for (int i = 0; i < 5; i++) {
    r[i] = readCm();
    delay(60);
  }
  for (int i = 1; i < 5; i++)
    for (int j = i; j > 0 && r[j] < r[j - 1]; j--) std::swap(r[j], r[j - 1]);
  if (r[2] < 0) Serial.println("Out of range (no echo)");
  else Serial.printf("Distance: %.1f cm\n", r[2]);
}

Expected serial output

Serial Monitor · 115200Output
HC-SR04: Trig GPIO6, Echo GPIO7
Distance: 87.4 cm
Distance: 87.3 cm
Out of range (no echo)

Common errors

Always out of range

VCC on 3V3, or the divider reversed. Full fix.

Works on the bench, fails in a water tank

Condensation on the transducers. Use the sealed JSN-SR04T for tanks.

↑↓ to move · Enter to open