Lesson 2 of 13
Blink an LED
Objective: Use digitalWrite() and delay() to switch an LED on and off at a chosen speed.
Stage 1 of 7 · What am I building?
Explore
You are building the classic first circuit: pin 13 switches an LED on and off. Select each part on the circuit, or below, to see what it does. Follow the current: D13 → 220 Ω → LED (+ to −) → GND.
Pick a part. 0/5 inspected.
Before you start (optional refreshers)
Pins, inputs and outputs
Each pin is configured as an input (the program reads it) or an output (the program drives it). pinMode() chooses.
setup() and loop()
setup() runs once after power-up or reset. loop() then runs again and again, as fast as its statements allow.
New ideas in this lesson
Closed circuits
Current only flows around a complete loop: from a supply, through the parts, and back to GND. Break the loop anywhere and everything in it stops.
Voltage, current and resistance
Voltage pushes, resistance restricts, current is the flow. Ohm's law ties them together: I = V ÷ R.
Current-limiting resistors
An LED barely limits its own current. A series resistor sets it: (5 V − about 2 V) ÷ 220 Ω ≈ 14 mA, safe for the LED and the pin.
Component polarity
Some parts only work one way round. An LED's longer leg (anode) goes toward the positive side; reversed, it blocks current and stays dark.
Digital HIGH and LOW
A digital pin is either HIGH (about 5 V on an Uno) or LOW (0 V). digitalWrite() sets an output; digitalRead() reports an input.
Timing and delay()
delay(ms) pauses the whole program. Nothing else happens while it waits, so a blink lasts ON delay + OFF delay.
Sequences and multiple outputs
Statements run in order, top to bottom. A pattern across several outputs is just writes and delays in the right order.
Systematic debugging
Describe the symptom, decide whether it points to the code or the wiring, check one thing at a time, then confirm the fix.
Build it for real · Arduino Uno R3
You need
- Arduino Uno R3 (or compatible) and its USB cable
- Breadboard and jumper wires
- 5 mm LED (red is easiest)
- 220 Ω resistor (red-red-brown)
Wiring
- D13 → 220 Ω resistor → LED long leg (anode, +).
- LED short leg (cathode, −, flat edge) → GND.
- Upload the sketch. The onboard L LED (also on D13) blinks along with yours.
Before you power up: Always include the resistor. Unplug USB while you change wiring, and check the LED's long leg faces the resistor before you plug back in.
The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.
// Blink an LED on pin 13
// (pin 13 → 220 Ω resistor → LED → GND)
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // LED on
delay(1000); // stay on for 1000 ms
digitalWrite(ledPin, LOW); // LED off
delay(1000); // stay off for 1000 ms
}Press ▶ Run to step through the sketch line by line, in time with the circuit.