Project 1.8: "Lighting Patterns"
๐ Project 1.8 โ Lighting Patterns
๐ฏ What Youโll Learn
- โ Goal 1: Create LED patterns with timing and logic.
- โ Goal 2: Use PWM for effects like breathing.
- โ Goal 3: Synchronize LEDs with buzzer and serial control.
Key Ideas
- Digital output: Turn LEDs ON/OFF.
- PWM: Control brightness.
- Loops: Repeat patterns.
- Serial input: Control patterns interactively.
๐งฑ Blocks Glossary (used in this project)
pinX = machine.Pin(X, machine.Pin.OUT)โ LED output.pwmX = machine.PWM(machine.Pin(X))โ LED brightness control.pinX.value(1)โ LED ON.pinX.value(0)โ LED OFF.pwmX.duty()โ Set brightness.pwmX.freq()โ Set PWM frequency.time.sleep()โ Delay.
๐งฐ What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable |
| LEDs | 5 | Pins 2, 4, 5, 12, 13 |
| Buzzer | 1 | Pin 26 |
๐ Wiring tip: Connect LEDs to pins 2, 4, 5, 12, 13. Buzzer to pin 26.
๐ Pin map snapshot: LEDs = pins 2/4/5/12/13, buzzer = pin 26.
โ Before You Start
- USB cable connected
- LEDs wired correctly
- Test print shows:
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 1.8.1 โ LED Race
Blocks used: Digital output, Loop, Delay
Block sequence:
- Setup LEDs as outputs
- Turn ON one by one
- Repeat
MicroPython Code:
import machine, time # Import modules
pin2 = machine.Pin(2, machine.Pin.OUT) # LED on pin 2
pin4 = machine.Pin(4, machine.Pin.OUT) # LED on pin 4
pin5 = machine.Pin(5, machine.Pin.OUT) # LED on pin 5
pin12 = machine.Pin(12, machine.Pin.OUT) # LED on pin 12
pin13 = machine.Pin(13, machine.Pin.OUT) # LED on pin 13
while True: # Infinite loop
pin2.value(1) # LED pin 2 ON
print("LED pin2 ON") # Serial log
time.sleep(0.3) # Delay
pin2.value(0) # LED pin 2 OFF
print("LED pin2 OFF") # Serial log
pin4.value(1) # LED pin 4 ON
print("LED pin4 ON") # Serial log
time.sleep(0.3) # Delay
pin4.value(0) # LED pin 4 OFF
print("LED pin4 OFF") # Serial log
pin5.value(1) # LED pin 5 ON
print("LED pin5 ON") # Serial log
time.sleep(0.3) # Delay
pin5.value(0) # LED pin 5 OFF
print("LED pin5 OFF") # Serial log
pin12.value(1) # LED pin 12 ON
print("LED pin12 ON") # Serial log
time.sleep(0.3) # Delay
pin12.value(0) # LED pin 12 OFF
print("LED pin12 OFF") # Serial log
pin13.value(1) # LED pin 13 ON
print("LED pin13 ON") # Serial log
time.sleep(0.3) # Delay
pin13.value(0) # LED pin 13 OFF
print("LED pin13 OFF") # Serial log
Reflection: LEDs light up one after another.
Challenge: Try reversing the order.
๐ฎ Microproject 1.8.2 โ Breathing effect with PWM
Blocks used: PWM, Duty cycle, Loop
Block sequence:
- Setup LED with PWM
- Increase duty gradually
- Decrease duty gradually
MicroPython Code:
import machine, time # Import modules
pwm2 = machine.PWM(machine.Pin(2)) # LED on pin 2 with PWM
pwm2.freq(1000) # Set frequency to 1 kHz
while True: # Infinite loop
for duty in range(0, 1024, 32): # Increase brightness
pwm2.duty(duty) # Set duty cycle
print("Brightness:", duty) # Serial log
time.sleep(0.05) # Delay
for duty in range(1023, -1, -32): # Decrease brightness
pwm2.duty(duty) # Set duty cycle
print("Brightness:", duty) # Serial log
time.sleep(0.05) # Delay
Reflection: LED brightness fades in and out.
Challenge: Try faster breathing by reducing delay.
๐ฎ Microproject 1.8.3 โ Random pattern
Blocks used: Digital output, Delay
Block sequence:
- Setup LEDs
- Randomly choose ON/OFF manually (no Python random)
- Blink pattern
MicroPython Code:
import machine, time # Import modules
pin2 = machine.Pin(2, machine.Pin.OUT) # LED pin 2
pin4 = machine.Pin(4, machine.Pin.OUT) # LED pin 4
while True: # Infinite loop
pin2.value(1) # LED pin 2 ON
pin4.value(0) # LED pin 4 OFF
print("Pattern: pin2 ON, pin4 OFF") # Serial log
time.sleep(0.5) # Delay
pin2.value(0) # LED pin 2 OFF
pin4.value(1) # LED pin 4 ON
print("Pattern: pin2 OFF, pin4 ON") # Serial log
time.sleep(0.5) # Delay
Reflection: LEDs alternate ON/OFF.
Challenge: Add more LEDs to the alternating pattern.
๐ฎ Microproject 1.8.4 โ Buzzer synchronization
Blocks used: Digital output, External actuator (music.MIDI), Delay
Block sequence:
- Setup LED and buzzer
- Turn ON LED + play tone
- Synchronize timing
MicroPython Code:
import machine, time, music # Import modules
pin2 = machine.Pin(2, machine.Pin.OUT) # LED pin 2
midi = music.MIDI(26) # Buzzer on pin 26
while True: # Infinite loop
pin2.value(1) # LED ON
midi.pitch_time(440, 500) # Play tone 440 Hz for 500 ms
print("LED ON + Tone 440 Hz") # Serial log
time.sleep(0.5) # Delay
pin2.value(0) # LED OFF
print("LED OFF") # Serial log
time.sleep(0.5) # Delay
Reflection: LED and buzzer work together.
Challenge: Try different tones for each LED.
ย
๐ฎ Microproject 1.8.5 โ Serial pattern control
Blocks used: Digital output, Serial input, Logic
Block sequence:
- Setup LEDs as outputs
- Read serial input
- Control LEDs ON/OFF
MicroPython Code:
import machine, time # Import modules
pin2 = machine.Pin(2, machine.Pin.OUT) # LED pin 2
pin4 = machine.Pin(4, machine.Pin.OUT) # LED pin 4
while True: # Infinite loop
cmd = input("Enter command (on/off): ") # Read serial input
if cmd == "on": # If user types "on"
pin2.value(1) # LED pin 2 ON
pin4.value(1) # LED pin 4 ON
print("LEDs ON") # Serial log
elif cmd == "off": # If user types "off"
pin2.value(0) # LED pin 2 OFF
pin4.value(0) # LED pin 4 OFF
print("LEDs OFF") # Serial log
time.sleep(0.2) # Delay for stability
Reflection: User controls LEDs via serial input.
Challenge: Add more commands like โraceโ or โblink.โ
โจ Main Project โ Lighting Patterns
๐ง Blocks Steps (with glossary)
- Digital output: Control LEDs.
- PWM: Control brightness.
- Serial input: Interactive control.
Block sequence:
- Setup LEDs and buzzer.
- Create race pattern.
- Add breathing effect.
- Synchronize with buzzer.
- Control via serial.
MicroPython Code (mirroring blocks):
# Project 1.8 โ Lighting Patterns
import machine, time, music # Import required modules
# Setup LEDs
pin2 = machine.Pin(2, machine.Pin.OUT) # LED pin 2
pin4 = machine.Pin(4, machine.Pin.OUT) # LED pin 4
pin5 = machine.Pin(5, machine.Pin.OUT) # LED pin 5
# Setup buzzer
midi = music.MIDI(26) # Buzzer on pin 26
# Setup PWM LED
pwm2 = machine.PWM(machine.Pin(2)) # PWM LED on pin 2
pwm2.freq(1000) # Frequency for breathing effect
while True: # Infinite loop
# LED race
pin2.value(1); print("Race: pin2 ON") # LED pin 2 ON
time.sleep(0.3)
pin2.value(0); print("Race: pin2 OFF") # LED pin 2 OFF
pin4.value(1); print("Race: pin4 ON") # LED pin 4 ON
time.sleep(0.3)
pin4.value(0); print("Race: pin4 OFF") # LED pin 4 OFF
pin5.value(1); print("Race: pin5 ON") # LED pin 5 ON
time.sleep(0.3)
pin5.value(0); print("Race: pin5 OFF") # LED pin 5 OFF
# Breathing effect
for duty in range(0, 1024, 64): # Increase brightness
pwm2.duty(duty)
print("Breathing brightness:", duty)
time.sleep(0.05)
for duty in range(1023, -1, -64): # Decrease brightness
pwm2.duty(duty)
print("Breathing brightness:", duty)
time.sleep(0.05)
# Buzzer sync
pin2.value(1) # LED pin 2 ON
midi.pitch_time(440, 500) # Play tone 440 Hz for 500 ms
print("LED pin2 ON + Tone 440 Hz")
pin2.value(0) # LED pin 2 OFF
print("LED pin2 OFF")
time.sleep(0.5)
๐ External Explanation
This project teaches how to create lighting effects with LEDs.
- Race pattern shows sequential control.
- Breathing effect uses PWM for smooth brightness changes.
- Synchronization links LEDs with buzzer tones.
- Serial input allows interactive control.
โจ Story Time
Your robot becomes a performer ๐. It can race lights, breathe softly, blink randomly, and even play music while flashing. With serial commands, you become the DJ controlling the show.
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 1.8.A โ Pattern stops
Problem: LEDs stop midโsequence.
Clues: Missing loop or delay too long.
Broken code:
pin2.value(1)
time.sleep(5) # Delay too long
Fixed code:
pin2.value(1)
time.sleep(0.3) # Correct delay
Why it works: Short delays keep the sequence flowing.
Avoid next time: Always use small delays for patterns.
๐ Debugging 1.8.B โ Unsynchronized LEDs
Problem: LEDs blink out of sync with buzzer.
Clues: LED and buzzer not triggered together.
Broken code:
pin2.value(1)
time.sleep(0.5)
midi.pitch_time(440, 500)
Fixed code:
pin2.value(1)
midi.pitch_time(440, 500)
Why it works: LED and buzzer start at the same time.
Avoid next time: Trigger both actions together.
โ Final Checklist
- LED race works.
- Breathing effect works.
- Random pattern alternates.
- Buzzer synchronized.
- Serial control implemented.
๐ Extras
- ๐ง Student tip: Try combining race + buzzer for a โlight show.โ
- ๐งโ๐ซ Instructor tip: Show how PWM changes brightness smoothly.
- ๐ Glossary: PWM, duty cycle, breathing effect, synchronization.
- ๐ก Mini tip: Always test one LED before adding more.