Project 1.2: "Hello World LED"
ย
๐ Project 1.2 โ Hello World LED
๐ฏ What Youโll Learn
- โ Goal 1: Blink a LED on and off.
- โ Goal 2: Control blink speed.
- โ Goal 3: Create simple patterns like SOS Morse code.
Key Ideas
- Digital output: Send ON/OFF signals to a pin.
- Delay: Pause for a set time.
- Loop: Repeat actions forever.
- Pattern: Combine timing to make rhythms.
๐งฑ Blocks Glossary (used in this project)
- Digital output: Turn a pin ON (HIGH) or OFF (LOW).
- Delay: Pause the program for a set time.
- Loop (while True): Repeat actions forever.
- Loop (for): Repeat a fixed number of times.
๐งฐ What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable (30 cm) |
| LED (10mm) | 1 | Pin 2 โ GND |
| Shield | 1 | For easy wiring |
๐ Wiring tip: Connect LED long leg to Pin 2, short leg to GND.
๐ Pin map snapshot: Pin 2 will be our LED control pin.
โ Before You Start
- USB cable is plugged in
- LED connected correctly (long leg = Pin 2, short leg = GND)
- Test print shows:
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 1.2.1 โ Basic LED Blink (1s)
Goal: Blink LED every 1 second.
Blocks used: Digital output, Delay, Loop
Block sequence:
- Setup Pin 2 as output
- LED ON โ Delay 1s
- LED OFF โ Delay 1s
- Repeat forever
MicroPython Code:
import machine, time # Importa librerรญas para controlar pines y tiempo
pin2 = machine.Pin(2, machine.Pin.OUT) # Configura el pin 2 como salida digital
while True: # Bucle infinito
pin2.value(1) # LED ON
print("LED ON") # Mensaje serial
time.sleep(1) # Espera 1 segundo
pin2.value(0) # LED OFF
print("LED OFF") # Mensaje serial
time.sleep(1) # Espera 1 segundo
Reflection: Your robot can now control light!
Challenge: Change delay to 0.5s for faster blinking.
๐ฎ Microproject 1.2.2 โ LED Flashing Rapidly (0.5s)
Goal: Blink LED every 0.5 seconds.
Blocks used: Digital output, Delay, Loop
Block sequence:
- Setup Pin 2 as output
- LED ON โ Delay 0.5s
- LED OFF โ Delay 0.5s
- Repeat forever
MicroPython Code:
import machine, time # Importa librerรญas
pin2 = machine.Pin(2, machine.Pin.OUT) # Configura pin 2 como salida
while True: # Bucle infinito
pin2.value(1) # LED ON
print("LED ON") # Mensaje serial
time.sleep(0.5) # Espera medio segundo
pin2.value(0) # LED OFF
print("LED OFF") # Mensaje serial
time.sleep(0.5) # Espera medio segundo
Reflection: Speed changes the rhythm.
Challenge: Try 0.2s for super-fast blinking.
๐ฎ Microproject 1.2.3 โ LED Slow Blink (2s)
Goal: Blink LED every 2 seconds.
Blocks used: Digital output, Delay, Loop
Block sequence:
- Setup Pin 2 as output
- LED ON โ Delay 2s
- LED OFF โ Delay 2s
- Repeat forever
MicroPython Code:
import machine, time # Importa librerรญas
pin2 = machine.Pin(2, machine.Pin.OUT) # Configura pin 2 como salida
while True: # Bucle infinito
pin2.value(1) # LED ON
print("LED ON") # Mensaje serial
time.sleep(2) # Espera 2 segundos
pin2.value(0) # LED OFF
print("LED OFF") # Mensaje serial
time.sleep(2) # Espera 2 segundos
Reflection: Longer delays make the LED look calm.
Challenge: Try 3s for extra slow blinking.
๐ฎ Microproject 1.2.4 โ Brightness Control with Delay
Goal: Simulate brightness by changing ON/OFF timing.
Blocks used: Digital output, Delay, Loop
Block sequence:
- LED ON โ Delay short
- LED OFF โ Delay long
- Repeat forever
MicroPython Code:
import machine, time # Importa librerรญas
pin2 = machine.Pin(2, machine.Pin.OUT) # Configura pin 2 como salida
while True: # Bucle infinito
pin2.value(1) # LED ON (bright)
print("LED ON (bright)") # Mensaje serial
time.sleep(0.1) # Corto tiempo encendido
pin2.value(0) # LED OFF (dim)
print("LED OFF (dim)") # Mensaje serial
time.sleep(0.9) # Largo tiempo apagado
Reflection: Timing can simulate brightness.
Challenge: Reverse ON/OFF times for opposite effect.
๐ฎ Microproject 1.2.5 โ SOS Morse
Goal: Blink LED in SOS pattern (… — …).
Blocks used: Digital output, Delay, For loop
Block sequence:
- Setup Pin 2 as output
- Blink 3 short (dot)
- Blink 3 long (dash)
- Blink 3 short (dot)
- Pause and repeat
MicroPython Code:
import machine, time # Importa librerรญas
pin2 = machine.Pin(2, machine.Pin.OUT) # Configura pin 2 como salida
while True: # Bucle infinito
for i in range(0, 3, 1): # Tres puntos
pin2.value(1); print("DOT") # LED ON corto + mensaje
time.sleep(0.3) # Espera 0.3 segundos
pin2.value(0) # LED OFF
time.sleep(0.3) # Espera 0.3 segundos
for i in range(0, 3, 1): # Tres rayas
pin2.value(1); print("DASH") # LED ON largo + mensaje
time.sleep(0.9) # Espera 0.9 segundos
pin2.value(0) # LED OFF
time.sleep(0.3) # Espera 0.3 segundos
for i in range(0, 3, 1): # Tres puntos
pin2.value(1); print("DOT") # LED ON corto + mensaje
time.sleep(0.3) # Espera 0.3 segundos
pin2.value(0) # LED OFF
time.sleep(0.3) # Espera 0.3 segundos
time.sleep(2) # Pausa antes de repetir
Reflection: Your robot can send signals like a lighthouse.
Challenge: Try another Morse code letter.
โจ Main Project โ Hello World LED
๐ง Blocks Steps (with glossary)
- Digital output: Control LED ON/OFF
- Delay: Control timing
- Loop: Repeat pattern
Block sequence:
- Setup LED pin
- Blink LED with different delays
- Create SOS pattern
๐ MicroPython Code (mirroring blocks)
# Project 1.2 โ Hello World LED
import machine, time # Importa librerรญas para controlar pines y tiempo
pin2 = machine.Pin(2, machine.Pin.OUT) # Configura el pin 2 como salida digital
while True: # Bucle infinito
pin2.value(1) # LED ON
print("LED ON") # Mensaje serial
time.sleep(1) # Espera 1 segundo
pin2.value(0) # LED OFF
print("LED OFF") # Mensaje serial
time.sleep(1) # Espera 1 segundo
๐ External Explanation
- What it teaches: Robots can control light patterns with code.
- Why it works: Digital output sends ON/OFF signals to the LED pin, and delays control timing.
- Key concept: Timing + repetition = visible patterns.
โจ Story Time
Imagine your robot is a lighthouse. It blinks to guide ships safely. By changing timing, you can send secret codes like SOS!
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 1.2.A โ LED does not light up (incorrect pin)
Problem: LED stays off.
Clues: Wrong pin or reversed legs.
Broken code:
pin5 = machine.Pin(5, machine.Pin.OUT) # Pin incorrecto
Fixed code:
pin2 = machine.Pin(2, machine.Pin.OUT) # Pin correcto para LED
Why it works: Pin 2 is the correct connection for the LED.
Avoid next time: Always check wiring and pin numbers.
๐ Debugging 1.2.B โ Irregular blinking (improperly configured delay)
Problem: LED blinks too fast or too slow.
Clues: Delay values are wrong.
Broken code:
time.sleep(0.05) # Demasiado rรกpido
Fixed code:
time.sleep(1) # Valor correcto para 1 segundo
Why it works: Proper delay values control blink speed.
Avoid next time: Match time.sleep() to the intended rhythm.
โ Final Checklist
- LED blinked once with test code
- LED blinked at different speeds
- SOS pattern worked
๐ Extras
- ๐ง Student tip: Try spelling your initials with blink counts.
- ๐งโ๐ซ Instructor tip: Watch for reversed LED legs.
- ๐ Glossary: Digital output, Delay, Loop.
- ๐ก Mini tips: Always test with a simple blink before complex patterns.