Beginner10 min50 XP base

Blink an LED

Your first Arduino sketch. Learn the setup/loop structure, digital output, and the basics of electrical circuits.

1How Arduino Programs Work

Every Arduino sketch is built around two required functions that the board runs automatically. Understanding these two functions is the foundation of everything you'll ever build with Arduino.

  • setup() — runs exactly once when the board powers on or resets. Use it to configure pins, start communications, or initialize variables.
  • loop() — runs continuously after setup() finishes, repeating from top to bottom forever until you cut power.
void setup() {
  // This runs once
}

void loop() {
  // This runs forever
}

Note: Unlike most programming languages, Arduino has no main() function. The runtime calls setup() and loop() for you.

2Digital Pins and OUTPUT Mode

Arduino has 14 digital pins numbered 0–13. Each pin can be configured as either an OUTPUT (you control the voltage) or an INPUT (you read the voltage). Before using any pin, you must declare its mode in setup() using pinMode().

Once a pin is set to OUTPUT, you control it with digitalWrite(). Writing HIGH applies approximately 5V to the pin. Writing LOW applies 0V (ground).

Note: Pin 13 has a built-in LED on most Arduino boards (Uno, Nano, Mega). You can test your sketch without any wiring at all.

3Building the Circuit

Arduino Uno
LED (any color)
220Ω resistor
Breadboard + jumper wires

LEDs are polarized — they only work in one direction. The longer leg (anode, +) connects through a resistor to your output pin. The shorter leg (cathode, −) connects to GND.

  • Connect pin 13 → 220Ω resistor → LED anode (long leg)
  • Connect LED cathode (short leg) → GND pin on Arduino

Note: The resistor is critical — without it, too much current flows and the LED burns out within seconds.

4Writing the Blink Sketch

Now let's write the actual code. The sketch turns the LED on, waits 1 second, turns it off, waits another second, and repeats.

void setup() {
  pinMode(13, OUTPUT);  // Configure pin 13 as output
}

void loop() {
  digitalWrite(13, HIGH);  // Turn LED on (~5V)
  delay(1000);             // Wait 1000 milliseconds (1 second)
  digitalWrite(13, LOW);   // Turn LED off (0V)
  delay(1000);             // Wait 1 second
}

Try changing the delay values to make the LED blink faster or slower. delay(100) gives 5 blinks per second. delay(50) gives 10.

5Challenges

  • Make the LED blink in an SOS pattern (3 short, 3 long, 3 short).
  • Use two LEDs on pins 12 and 13 that alternate — one on while the other is off.
  • Make the LED blink faster over time by reducing the delay in each loop iteration.

Skill Check

8 questions · Up to 180 XP

Q1.What function runs exactly once when the Arduino powers on?

Q2.What does digitalWrite(13, HIGH) do?

Q3.What does delay(500) pause execution for?

Q4.Why is a resistor required when connecting an LED?

Q5.Which pin has a built-in LED on most Arduino Uno boards?

Q6.What does pinMode(13, OUTPUT) do?

Q7.What happens if you remove both delay() calls from the blink sketch?

Q8.What is the correct anode/cathode wiring for an LED?

Sign in to earn XP for your answers.