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.
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
int ledPin = 13;stores the pin number in a variable.setup()runs once when the board starts.pinMode(ledPin, OUTPUT);tells pin 13 to send electricity out.digitalWrite(ledPin, HIGH);turns the LED on.digitalWrite(ledPin, LOW);turns the LED off.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
int count = 0creates a counting variable that starts at 0.count < 3means the loop keeps running while count is less than 3.count++adds 1 after each repeat.- 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
- Check the LED direction before uploading code.
- Check that the resistor is in the same row as the LED long leg.
- Check that the short leg connects to GND.
- Upload the basic blink code first.
- Change only one number at a time when experimenting.