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 — ultrasonic distance on a breadboard

Wire an HC-SR04 to an Arduino Nano on D2/D3 at 5 V and print distance in centimetres, lighting the on-board LED when something is close.

Arduino C++Verified — core avr 1.8.8, 3 Nov 2026Compile-only

Community draft — report a mistake

The Nano is the breadboard-friendly twin of the Uno, which makes it the usual brain for blind sticks, parking sensors and small robots built around the HC-SR04.

Everything runs at 5 V: Trig on D2, Echo on D3. The sketch prints the distance five times a second and lights the on-board LED when something is closer than 20 cm.

Wiring

Arduino NanoOnly the pins used are shown · 5 V logicHC-SR04 ultrasonic sensor5VGNDD3D2VCCTrigEchoGND
Every connection
Arduino NanoHC-SR04 ultrasonic sensorWireNotes
5VVCCred
D2Triggreen
D3Echoblue
GNDGNDblack

5 V (HC-SR04P / RCWL-1601: 3–5.5 V) part, 5 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 Nano is a 5 V ATmega328P board like the Uno, so Echo needs no divider.
  • Clone Nanos use a CH340 USB chip and the old bootloader: if upload fails, pick Tools → Processor → ATmega328P (Old Bootloader).
  • The Nano sits on a breadboard; keep the HC-SR04 at the board edge so the transducers aren’t blocked by wires.

Code

hc-sr04-nano.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// HC-SR04 ultrasonic distance sensor on an Arduino Nano.
// VCC → 5V, Trig → D2, Echo → D3, GND → GND. The Nano is a 5 V board, so no divider is needed.
const uint8_t TRIG_PIN = 2;
const uint8_t ECHO_PIN = 3;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  digitalWrite(TRIG_PIN, LOW);
}

void loop() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);  // ~5 m max
  if (us == 0) {
    Serial.println(F("Out of range or no echo"));
  } else {
    float cm = us * 0.0343 / 2.0;
    Serial.print(F("Distance: "));
    Serial.print(cm, 1);
    Serial.println(F(" cm"));
    digitalWrite(LED_BUILTIN, cm < 20 ? HIGH : LOW);  // on-board LED when something is close
  }
  delay(200);
}

Expected serial output

Serial Monitor · 9600Output
Distance: 31.2 cm
Distance: 31.3 cm
Distance: 14.8 cm

Common errors

avrdude: stk500_getsync() not in sync

Wrong bootloader setting on a clone Nano. Full fix.

Always “no echo”

Trig/Echo swapped or no 5 V on VCC. Full fix.

↑↓ to move · Enter to open