๐Ÿ’ก Level 1 โ€“ Fundamentals

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

PartHow many?Pin connection
D1 R321USB cable
LEDs5Pins 2, 4, 5, 12, 13
Buzzer1Pin 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:

  1. Setup LEDs as outputs
  2. Turn ON one by one
  3. 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:

  1. Setup LED with PWM
  2. Increase duty gradually
  3. 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:

  1. Setup LEDs
  2. Randomly choose ON/OFF manually (no Python random)
  3. 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:

  1. Setup LED and buzzer
  2. Turn ON LED + play tone
  3. 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:

  1. Setup LEDs as outputs
  2. Read serial input
  3. 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:

  1. Setup LEDs and buzzer.
  2. Create race pattern.
  3. Add breathing effect.
  4. Synchronize with buzzer.
  5. 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.
On this page