Blink an LED
The "hello world" of embedded systems: switch an LED on and off from a digital pin.
Output · Beginner
In a real product: Device heartbeat LED (Consumer electronics): A blinking LED shows the firmware is alive.
An LED connected to pin 12 through a 220 Ω resistor, switched on and off every half second. It is the first program everyone writes on a new board, because it proves the toolchain, the upload and the pin all work.
What you'll learn
- Setting a pin as an output with
pinMode() - Driving it HIGH (5 V) and LOW (0 V) with
digitalWrite() - Why an LED needs a series resistor
How it works
An LED only lights when current flows from its anode (A, the long leg) to its cathode (C, the short leg). Pin 12 drives the anode through the resistor, and the cathode goes to GND.
digitalWrite(LED_PIN, HIGH) puts 5 V on the pin, so current flows and the LED lights. LOW puts 0 V on it and the LED goes dark. delay(500) waits half a second between the two.
The resistor limits the current. Without it the LED would try to draw far more than the 20 mA an Arduino pin can safely give: (5 V − 2 V for the LED) / 220 Ω ≈ 14 mA.
Try this
- Change the delays to make a heartbeat: two quick flashes, then a pause.
- Move the LED to pin 13 and watch the board's own L LED blink with it.
- Replace
delay()withmillis()so the loop could do other work while it waits.
sketch.ino
// Blink an LED on pin 12: on for half a second, off for half a second.
const int LED_PIN = 12;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}