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 ESP32 | HC-SR04 ultrasonic sensor | Wire | Notes |
|---|---|---|---|
| VBUS | VCC | red | USB 5 V. VBUS is off when the Nano runs from VIN. |
| D3 | Trig | purple | |
| D2 | Echo | green | Via 1 kΩ / 2 kΩ divider — 1 kΩ from ECHO to D2, 2 kΩ from D2 to GND → about 3.3 V. |
| GND | GND | black |
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 likedigitalWrite().
Code
// 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);
}// 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
HC-SR04: Trig D3, Echo D2
Distance: 51.6 cm
Distance: 51.4 cm
Out of range (no echo)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.