Tutorial

Lesson 2 — Blink an LED 💡

🧠 Concept

Digital output + basic code logic. We turn a pin HIGH (on) and LOW (off) with a delay between.

💡 By the end: you’ll make an LED blink and tweak the speed.

🔌 What is a digital pin?

A digital pin can be either 0 (LOW) or 1 (HIGH). Setting a pin HIGH sends ~5V (Arduino Uno) to whatever is connected.

🧰 You’ll need

  • Arduino Uno + USB cable
  • LED (any colour)
  • 220 Ω resistor
  • Breadboard + jumper wires

🧭 Wiring diagram

LED Blink Wiring Diagram

  • Arduino GND → resistor → LED short leg (–)
  • Arduino D13 → LED long leg (+)
⚠️ LED has polarity. Long leg = + (anode). Short leg = – (cathode).

💻 Code (Arduino C/C++)

// Blink an LED on pin 13
const int LED_PIN = 13;

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

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);               // 0.5s on
  digitalWrite(LED_PIN, LOW);
  delay(500);               // 0.5s off
}

🐍 Code (Raspberry Pi – Python)

💡 Use a 330–470 Ω resistor. If using a Pi, place LED on a safe GPIO (e.g., BCM 18). Power from 3.3V only.
# Blink LED on Raspberry Pi using RPi.GPIO
import RPi.GPIO as GPIO, time

LED_PIN = 18  # BCM numbering
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)

try:
    while True:
        GPIO.output(LED_PIN, GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(LED_PIN, GPIO.LOW)
        time.sleep(0.5)
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

⚡ Code (ESP32 – MicroPython)

# Blink LED on ESP32 (MicroPython)
from machine import Pin
from time import sleep

LED_PIN = 2  # On-board LED on many ESP32 dev boards
led = Pin(LED_PIN, Pin.OUT)

while True:
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)

🛠️ Common errors & fixes

  • LED not lighting: Check polarity, resistor value, and pin number.
  • Upload fails: Wrong board/port selected.
  • Too dim: Try 220 Ω instead of 1kΩ; ensure correct wiring.

🖨️ Worksheet — LED Blink Checklist

  • [ ] LED orientation correct (+ to D13 via resistor)
  • [ ] Code uploaded with no errors
  • [ ] Blink speed changed to 200 ms and 1 s
  • [ ] Explain what HIGH and LOW do
RoboRider Labs • Classroom Lesson 2