DEV Community

Hedy
Hedy

Posted on

How to create flashing leds on an Arduino Nano?

Here’s the simplest way to make flashing LEDs on an Arduino Nano, plus a “multi-LED chase” version.

1) One LED flashing (built-in LED)

Most Nano boards have a built-in LED on D13.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // on
  delay(500);                       // 0.5 s
  digitalWrite(LED_BUILTIN, LOW);   // off
  delay(500);                       // 0.5 s
}
Enter fullscreen mode Exit fullscreen mode

Upload this and the onboard LED should blink.

2) One external LED flashing (recommended wiring)

Parts: LED + 220Ω–1kΩ resistor + jumper wires
Wiring:

  • Nano D2 → resistor → LED anode (long leg)
  • LED cathode (short leg) → GND

Code:

const int LED_PIN = 2;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(300);
  digitalWrite(LED_PIN, LOW);
  delay(300);
}
Enter fullscreen mode Exit fullscreen mode

3) Multiple LEDs “flashing/chasing” pattern

Wire LEDs (each with its own resistor) to D2, D3, D4, D5, cathodes to GND.

const int leds[] = {2, 3, 4, 5};
const int n = sizeof(leds) / sizeof(leds[0]);

void setup() {
  for (int i = 0; i < n; i++) pinMode(leds[i], OUTPUT);
}

void loop() {
  // chase forward
  for (int i = 0; i < n; i++) {
    digitalWrite(leds[i], HIGH);
    delay(150);
    digitalWrite(leds[i], LOW);
  }

  // chase backward
  for (int i = n - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    delay(150);
    digitalWrite(leds[i], LOW);
  }
}
Enter fullscreen mode Exit fullscreen mode

Common gotchas (quick checks)

  • LED direction matters: long leg = anode (+), short leg = cathode (–).
  • Always use a resistor (220Ω–1kΩ) to avoid burning the LED or stressing the pin.
  • Arduino Nano I/O pins are 5V logic; don’t drive high-power LEDs directly—use a transistor/MOSFET if needed.

Top comments (0)