💡 Level 1 – Fundamentals

Project 1.10: "Alarm System"

 

🚀 Project 1.10 – Alarm System


🎯 What You’ll Learn

  • ✅ Goal 1: Create simple audible alarms.
  • ✅ Goal 2: Combine LED + buzzer for visual and audible alerts.
  • ✅ Goal 3: Add activation/deactivation logic and timed alarms.
  • ✅ Goal 4: Program different alarm patterns.

Key Ideas

  • Digital output: Control LED and buzzer.
  • Digital input: Button to activate/deactivate.
  • Logic: If/else for alarm states.
  • Loops + delays: Control alarm timing.

🧱 Blocks Glossary (used in this project)

  • pinX = machine.Pin(X, machine.Pin.OUT) → LED or buzzer output.
  • pinX = machine.Pin(X, machine.Pin.IN) → Button input.
  • pinX.value(1) → Turn ON LED/buzzer.
  • pinX.value(0) → Turn OFF LED/buzzer.
  • midi = music.MIDI(X) → Buzzer setup.
  • midi.pitch_time(freq, ms) → Play tone.
  • while True: → Infinite loop.
  • time.sleep() → Delay.

🧰 What You Need

Part How many? Pin connection
D1 R32 1 USB cable
LED 1 Pin 5
Buzzer 1 Pin 26
Button 1 Pin 2

✅ Before You Start

  • USB cable connected
  • LED, buzzer, and button wired correctly
  • Test print shows:
print("Ready!")  # Confirm serial is working

🎮 Microprojects (5 Mini Missions)


🎮 Microproject 1.10.1 – Simple audible alarm

Blocks used: Digital output, External actuator (music), Delay
Block sequence:

  1. Setup buzzer
  2. Play tone repeatedly

MicroPython Code:

import machine, time, music                   # Import modules
midi = music.MIDI(26)                         # Buzzer on pin 26
while True:                                   # Infinite loop
    midi.pitch_time(440, 500)                 # Play tone 440 Hz for 500 ms
    print("Alarm tone 440 Hz")                # Serial log
    time.sleep(0.5)                           # Delay

Reflection: Buzzer plays alarm tone.
Challenge: Try different frequencies.


🎮 Microproject 1.10.2 – Visual and audible alarm

Blocks used: Digital output, External actuator, Delay
Block sequence:

  1. Setup LED + buzzer
  2. Turn ON together

MicroPython Code:

import machine, time, music                   # Import modules
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED on pin 5
midi = music.MIDI(26)                         # Buzzer on pin 26
while True:                                   # Infinite loop
    pin5.value(1)                             # LED ON
    midi.pitch_time(440, 500)                 # Play tone
    print("LED ON + Alarm tone")              # Serial log
    pin5.value(0)                             # LED OFF
    print("LED OFF")                          # Serial log
    time.sleep(0.5)                           # Delay

Reflection: LED and buzzer work together.
Challenge: Try blinking LED faster than buzzer.


🎮 Microproject 1.10.3 – Activation/deactivation

Blocks used: Digital input, Digital output, Logic
Block sequence:

  1. Setup button
  2. If pressed → alarm ON
  3. Else → alarm OFF

MicroPython Code:

import machine, time, music                   # Import modules
pin2 = machine.Pin(2, machine.Pin.IN)         # Button input
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output
midi = music.MIDI(26)                         # Buzzer output
while True:                                   # Infinite loop
    if pin2.value() == 1:                     # If button pressed
        pin5.value(1)                         # LED ON
        midi.pitch_time(440, 500)             # Play tone
        print("Alarm activated")              # Serial log
    else:                                     # If button not pressed
        pin5.value(0)                         # LED OFF
        print("Alarm deactivated")            # Serial log
    time.sleep(0.2)                           # Delay

Reflection: Button controls alarm ON/OFF.
Challenge: Add second button for reset.


🎮 Microproject 1.10.4 – Limited alarm time

Blocks used: Digital input, Digital output, Delay
Block sequence:

  1. Setup button
  2. Alarm runs for 5s
  3. Then stops

MicroPython Code:

import machine, time, music                   # Import modules
pin2 = machine.Pin(2, machine.Pin.IN)         # Button input
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output
midi = music.MIDI(26)                         # Buzzer output
while True:                                   # Infinite loop
    if pin2.value() == 1:                     # If button pressed
        print("Alarm running for 5s")         # Serial log
        for i in range(0, 5, 1):              # Loop 5 times
            pin5.value(1)                     # LED ON
            midi.pitch_time(440, 500)         # Play tone
            time.sleep(1)                     # Delay 1s
        pin5.value(0)                         # LED OFF
        print("Alarm stopped")                # Serial log

Reflection: Alarm runs for limited time.
Challenge: Try 10s duration.


🎮 Microproject 1.10.5 – Different alarm patterns

Blocks used: Digital output, External actuator, Loop
Block sequence:

  1. Setup LED + buzzer
  2. Alternate fast/slow patterns

MicroPython Code:

import machine, time, music                   # Import modules
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output
midi = music.MIDI(26)                         # Buzzer output
while True:                                   # Infinite loop
    # Fast pattern
    for i in range(0, 3, 1):                  # Repeat 3 times
        pin5.value(1)                         # LED ON
        midi.pitch_time(880, 200)             # High tone
        print("Fast alarm")                   # Serial log
        pin5.value(0)                         # LED OFF
        time.sleep(0.2)                       # Delay
    # Slow pattern
    for i in range(0, 2, 1):                  # Repeat 2 times
        pin5.value(1)                         # LED ON
        midi.pitch_time(440, 800)             # Low tone
        print("Slow alarm")                   # Serial log
        pin5.value(0)                         # LED OFF
        time.sleep(0.8)                       # Delay

Reflection: Alarm alternates between fast and slow.
Challenge: Add a third pattern.


✨ Main Project – Alarm System

🔧 Blocks Steps (with glossary)

  • Digital input: Button control.
  • Digital output: LED + buzzer.
  • Logic: Activation/deactivation.
  • Loops: Timed alarms.

Block sequence:

  1. Setup LED, buzzer, button.
  2. Simple alarm.
  3. Visual + audible alarm.
  4. Activation/deactivation.
  5. Limited time + patterns.

📖 External Explanation

This project shows how alarms combine sensors and actuators.

  • Button input activates alarm.
  • LED + buzzer outputs create alerts.
  • Logic and loops add timing and patterns.

✨ Story Time

Imagine your robot as a security guard 🚨. When the button is pressed, it sounds alarms with lights and tones. You can design patterns to warn of danger or celebrate events.


 

🕵️ Debugging (2 Common Problems)

🐞 Debugging 1.10.A – Alarm does not deactivate

Problem: Alarm keeps running even when button is not pressed.
Clues: Button state not checked in loop.
Broken code:

import machine, time, music                   # Import modules
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output
midi = music.MIDI(26)                         # Buzzer output
while True:                                   # Infinite loop
    pin5.value(1)                             # LED always ON
    midi.pitch_time(440, 500)                 # Tone always plays

Fixed code:

import machine, time, music                   # Import modules
pin2 = machine.Pin(2, machine.Pin.IN)         # Button input
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output
midi = music.MIDI(26)                         # Buzzer output
while True:                                   # Infinite loop
    if pin2.value() == 1:                     # If button pressed
        pin5.value(1)                         # LED ON
        midi.pitch_time(440, 500)             # Play tone
        print("Alarm ON")                     # Serial log
    else:                                     # If button not pressed
        pin5.value(0)                         # LED OFF
        print("Alarm OFF")                    # Serial log

Why it works: Button state decides alarm ON/OFF.
Avoid next time: Always check input state before activating outputs.


🐞 Debugging 1.10.B – False positive

Problem: Alarm triggers without pressing button.
Clues: Floating input pin.
Broken code:

pin2 = machine.Pin(2, machine.Pin.IN)         # Input without pull-up/down

Fixed code:

pin2 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)  # Correct pull-up

Why it works: Pull‑up ensures stable input state.
Avoid next time: Always use pull‑up or pull‑down for buttons.


✅ Final Checklist

  • Simple audible alarm works.
  • LED + buzzer alarm implemented.
  • Activation/deactivation logic added.
  • Limited alarm time tested.
  • Different alarm patterns programmed.

📚 Extras

  • 🧠 Student tip: Try combining alarm patterns with LED race for a “light + sound show.”
  • 🧑‍🏫 Instructor tip: Demonstrate floating inputs by removing pull‑up/down and showing false positives.
  • 📖 Glossary: Pull‑up, false positive, activation, pattern.
  • 💡 Mini tip: Always test button input with print(pin2.value()) before connecting to outputs.
On this page