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 Arduino Nano ESP32 — divider needed

Wire an HC-SR04 to an Arduino Nano ESP32: VCC on VBUS, Trig on D3, Echo on D2 through a 1 kΩ / 2 kΩ divider — unlike on a classic 5 V Nano.

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

Community draft — report a mistake

Moving an HC-SR04 project from a classic Nano to the Nano ESP32? The pins are the same, but the logic level isn’t. Power the sensor from VBUS, trigger from D3, and bring ECHO to D2 through a divider.

The sketch measures five times a second and prints centimetres.

Wiring

Arduino Nano ESP32Only the pins used are shown · 3.3 V logicHC-SR04 ultrasonic sensor1k│2kVBUSGNDD3D2VCCTrigEchoGND
Also needed: 1 kΩ resistor, 2 kΩ resistor.
Every connection
Arduino Nano ESP32HC-SR04 ultrasonic sensorWireNotes
VBUSVCCredUSB 5 V. VBUS is off when the Nano runs from VIN.
D3Trigpurple
D2EchogreenVia 1 kΩ / 2 kΩ divider — 1 kΩ from ECHO to D2, 2 kΩ from D2 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

  • Classic Nano tutorials wire ECHO straight to a pin. On the Nano ESP32 that puts 5 V on a 3.3 V pin — always use the divider.
  • There is no regulated 5V pin. VBUS carries USB 5 V only while USB is plugged in; powered from VIN, use an HC-SR04P on 3V3 (no divider needed).
  • pulseIn() works with the Nano pin names because the core remaps it, just like digitalWrite().

Code

hc-sr04-nanoesp32.inoArduino C++Open in PlaygroundOpen in Playground
// HC-SR04 ultrasonic sensor on an Arduino Nano ESP32.
// VCC → VBUS (USB 5 V), GND → GND, Trig → D3, Echo → D2 through a 1 kΩ / 2 kΩ divider.
// The Nano ESP32 is a 3.3 V board: unlike a classic Nano, ECHO must not go straight to a pin.
constexpr uint8_t TRIG_PIN = D3;
constexpr uint8_t ECHO_PIN = D2;

void setup() {
  Serial.begin(115200);
  uint32_t t0 = millis();
  while (!Serial && millis() - t0 < 3000) {}
  pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);
  pinMode(ECHO_PIN, INPUT);
  Serial.println("HC-SR04: Trig D3, Echo D2");
}

void loop() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);
  if (us == 0) Serial.println("Out of range (no echo)");
  else Serial.printf("Distance: %.1f cm\n", us * 0.0343f / 2.0f);
  delay(200);
}

Expected serial output

Serial Monitor · 115200Output
HC-SR04: Trig D3, Echo D2
Distance: 51.6 cm
Distance: 51.4 cm
Out of range (no echo)

Common errors

Always out of range

VCC not on VBUS, running from VIN, or divider reversed. Full fix.

Worked with a classic Nano shield, not here

The shield fed ECHO straight in at 5 V. Add the divider.

↑↓ to move · Enter to open