Arduino LED Basics

This lesson teaches the first essential Arduino output project: turning an LED on and off, then using loops to create repeated blinking patterns.

Lesson Goals

Parts Needed

Arduino Uno or compatible board
USB cable
Breadboard
1 LED
1 resistor, usually 220 ohms or 330 ohms
2 jumper wires
Use a resistor with the LED. Connecting an LED directly to a pin can damage the LED or the board.

LED Wiring Map

Connect Arduino pin 13 to the resistor, then to the LED long leg. Connect the LED short leg to GND.

Arduino Uno Pin 13 GND Breadboard 220 ohm resistor LED long leg + short leg - Current path: Pin 13 -> resistor -> LED -> GND

Code 1: Basic On and Off

This code turns the LED on for one second, then off for one second. Arduino repeats loop() forever.

int ledPin = 13;

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

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);

  digitalWrite(ledPin, LOW);
  delay(1000);
}

Teaching Points

  1. int ledPin = 13; stores the pin number in a variable.
  2. setup() runs once when the board starts.
  3. pinMode(ledPin, OUTPUT); tells pin 13 to send electricity out.
  4. digitalWrite(ledPin, HIGH); turns the LED on.
  5. digitalWrite(ledPin, LOW); turns the LED off.
  6. delay(1000); waits for 1000 milliseconds, which is 1 second.

Code 2: Faster Blink

Changing the delay changes the rhythm. A smaller number makes the LED blink faster.

int ledPin = 13;

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

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(250);

  digitalWrite(ledPin, LOW);
  delay(250);
}

Code 3: Basic Loop Knowledge

A for loop repeats a block of code a fixed number of times. This example makes three quick flashes, pauses, and repeats.

int ledPin = 13;

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

void loop() {
  for (int count = 0; count < 3; count++) {
    digitalWrite(ledPin, HIGH);
    delay(150);

    digitalWrite(ledPin, LOW);
    delay(150);
  }

  delay(1000);
}

How the for Loop Works

  1. int count = 0 creates a counting variable that starts at 0.
  2. count < 3 means the loop keeps running while count is less than 3.
  3. count++ adds 1 after each repeat.
  4. The code inside the braces runs three times.
Student challenge: change the number 3 to 5, then change the delay values to design a new blink pattern.

Classroom Checklist

  1. Check the LED direction before uploading code.
  2. Check that the resistor is in the same row as the LED long leg.
  3. Check that the short leg connects to GND.
  4. Upload the basic blink code first.
  5. Change only one number at a time when experimenting.