All code examples

Blink an LED — Arduino code

Blink is the first sketch everyone writes. It switches an LED on, waits, switches it off, and repeats forever — the timing pattern almost every other Arduino project reuses.

What you need

  • Arduino Uno
  • 1 × LED
  • 1 × 220 Ω resistor
  • Breadboard + 2 jumper wires

Wiring

  1. 1Long leg (+) of the LED to a breadboard row, short leg to another row.
  2. 2220 Ω resistor from the short leg row to the Arduino GND rail.
  3. 3Jumper wire from digital pin 13 to the long leg row.

The code

blink-an-led.ino
// Blink an LED on pin 13
const int LED_PIN = 13;

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

void loop() {
  digitalWrite(LED_PIN, HIGH);  // on
  delay(1000);                  // wait 1 second
  digitalWrite(LED_PIN, LOW);   // off
  delay(1000);                  // wait 1 second
}

Line by line

const int LED_PIN = 13;
Names the pin once so you only change it in one place.
pinMode(LED_PIN, OUTPUT);
Tells the board this pin will push power out, not read it.
digitalWrite(LED_PIN, HIGH);
Sends 5 V to the pin — the LED lights up.
delay(1000);
Pauses for 1000 milliseconds (one second).
digitalWrite(LED_PIN, LOW);
Drops the pin to 0 V and the LED goes out.

When it does not work

Why does my LED stay dark?
It is almost always in backwards. The long leg must face the Arduino pin and the short leg goes through the resistor to GND.
Do I need the resistor?
Yes. Without it the LED pulls too much current and both the LED and the pin can be damaged. 220 Ω is the safe default on 5 V.
How do I blink faster?
Lower both delay values, for example delay(200) for five blinks a second.

Want the theory behind it?

More code examples