Project 2.8: "Basic Security System"
๐ Project 2.8 โ Basic Security System
ย
๐ฏ What Youโll Learn
- โ Goal 1: Activate and deactivate a security system with an IR remote.
- โ Goal 2: Detect intruders using touch sensors.
- โ Goal 3: Detect impacts with crash sensors.
- โ Goal 4: Trigger different alarms depending on the sensor.
- โ Goal 5: Record events in the serial log.
Key Ideas
- Short definition: Security systems combine sensors and alarms to protect spaces.
- Realโworld link: Used in homes, cars, and robots to detect unauthorized access.
๐งฑ Blocks Glossary
- Touch sensor:
tc2 = machine.TouchPad(machine.Pin(2))โ Detects touch input. - Crash sensor:
pin5 = machine.Pin(5, machine.Pin.IN)โ Detects impact. - IR remote:
ir_rx = irremote.NEC_RX(18,8)โ Reads remote codes. - Buzzer:
midi = music.MIDI(26)โ Plays alarm sounds. - Serial print:
print("...")โ Logs events. - Loop:
while True:โ Continuous monitoring. - if/else: Decide actions based on sensor input.
๐งฐ What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable |
| IR receiver | 1 | Pin 18 |
| Touch sensor | 1 | Pin 2 |
| Crash sensor | 1 | Pin 5 |
| Buzzer | 1 | Pin 26 |
๐ Wiring tip: Connect IR receiver to pin 18, touch sensor to pin 2, crash sensor to pin 5, buzzer to pin 26.
โ Before You Start
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 2.8.1 โ Remote activation/deactivation
Goal: Use IR remote to turn system ON/OFF.
MicroPython Code:
# Remote activation/deactivation
# This program uses IR remote to activate or deactivate the security system
import irremote, time # Import IR library and time
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver on pin 18
system_active = False # Start with system OFF
print("Security system ready") # Serial start message
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read code
if code == 0xA90: # Example ON button
system_active = True # Activate system
print("System activated") # Serial confirmation
elif code == 0xA91: # Example OFF button
system_active = False # Deactivate system
print("System deactivated") # Serial confirmation
time.sleep(0.1) # Delay for stability
Reflection: You can now control system ON/OFF with remote.
Challenge: Add LED indicator for system status.
๐ฎ Microproject 2.8.2 โ Touch intruder detection
Goal: Detect intruder touch when system active.
MicroPython Code:
# Touch intruder detection
# This program detects touch input as intruder alert
import machine, time # Import machine and time libraries
tc2 = machine.TouchPad(machine.Pin(2)) # Setup touch sensor on pin 2
system_active = True # Assume system is active
print("Touch detection ready") # Serial start message
while True: # Infinite loop
if system_active: # Only check if system active
value = tc2.read() # Read touch sensor value
if value < 300: # Threshold for touch detection
print("Intruder detected by touch!") # Serial alert
time.sleep(0.1) # Delay for stability
Reflection: Touch sensor detects intruder when system active.
Challenge: Add buzzer alarm when intruder detected.
๐ฎ Microproject 2.8.3 โ Impact detection
Goal: Detect crash sensor input.
MicroPython Code:
# Impact detection
# This program detects impact using crash sensor
import machine, time # Import machine and time libraries
pin5 = machine.Pin(5, machine.Pin.IN) # Setup crash sensor on pin 5
system_active = True # Assume system is active
print("Impact detection ready") # Serial start message
while True: # Infinite loop
if system_active: # Only check if system active
if pin5.value() == 1: # If crash sensor triggered
print("Impact detected!") # Serial alert
time.sleep(0.1) # Delay for stability
Reflection: Crash sensor detects impact events.
Challenge: Add buzzer alarm when impact detected.
๐ฎ Microproject 2.8.4 โ Differentiated alarm by sensor
Goal: Trigger different alarms for touch vs impact.
MicroPython Code:
# Differentiated alarm by sensor
# This program uses buzzer to play different alarms for touch and impact
import machine, time, music # Import machine, time, and music libraries
tc2 = machine.TouchPad(machine.Pin(2)) # Touch sensor setup
pin5 = machine.Pin(5, machine.Pin.IN) # Crash sensor setup
midi = music.MIDI(26) # Buzzer setup
system_active = True # Assume system is active
print("Differentiated alarm ready") # Serial start message
while True: # Infinite loop
if system_active: # Only check if system active
if tc2.read() < 300: # Touch detected
midi.pitch_time(440, 200) # Play beep for touch
print("Touch alarm triggered") # Serial alert
if pin5.value() == 1: # Impact detected
midi.pitch_time(523, 200) # Play different beep for impact
print("Impact alarm triggered") # Serial alert
time.sleep(0.1) # Delay for stability
Reflection: Different alarms help identify type of intrusion.
Challenge: Add LED ON for touch, OFF for impact.
๐ฎ Microproject 2.8.5 โ Serial event log
Goal: Record all events in serial monitor.
MicroPython Code:
# Serial event log
# This program records all events in serial monitor
import machine, time, music # Import machine, time, and music libraries
tc2 = machine.TouchPad(machine.Pin(2)) # Touch sensor setup
pin5 = machine.Pin(5, machine.Pin.IN) # Crash sensor setup
midi = music.MIDI(26) # Buzzer setup
system_active = True # Assume system is active
print("Event log ready") # Serial start message
while True: # Infinite loop
if system_active: # Only check if system active
if tc2.read() < 300: # Touch detected
midi.pitch_time(440, 200) # Play beep
print("LOG: Touch intrusion detected") # Log event
if pin5.value() == 1: # Impact detected
midi.pitch_time(523, 200) # Play beep
print("LOG: Impact intrusion detected") # Log event
time.sleep(0.1) # Delay for stability
Reflection: Serial monitor now records all intrusion events.
Challenge: Add timestamps to log entries.
ย
โจ Main Project โ Basic Security System (continuaciรณn y completo)
Goal: Combine all microprojects into one complete security system.
MicroPython Code:
# Project 2.8 โ Basic Security System (Main Project)
# This program combines IR remote, touch sensor, crash sensor, and buzzer into a full security system
import machine, time, music, irremote # Import libraries for pins, time, buzzer, and IR
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver on pin 18
tc2 = machine.TouchPad(machine.Pin(2)) # Setup touch sensor on pin 2
pin5 = machine.Pin(5, machine.Pin.IN) # Setup crash sensor on pin 5
midi = music.MIDI(26) # Setup buzzer on pin 26
system_active = False # Start with system OFF
print("Basic Security System ready") # Print start message to serial monitor
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read the first code from buffer
if code == 0xA90: # Example ON button code
system_active = True # Activate system
print("System activated") # Serial confirmation
elif code == 0xA91: # Example OFF button code
system_active = False # Deactivate system
print("System deactivated") # Serial confirmation
if system_active: # Only check sensors if system is active
if tc2.read() < 300: # If touch sensor detects intrusion
midi.pitch_time(440, 200) # Play buzzer alarm for touch
print("LOG: Touch intrusion detected") # Log event in serial
if pin5.value() == 1: # If crash sensor detects impact
midi.pitch_time(523, 200) # Play buzzer alarm for impact
print("LOG: Impact intrusion detected") # Log event in serial
time.sleep(0.1) # Small delay for stability
๐ External Explanation
- What it teaches: How to combine IR remote, touch, and crash sensors into a complete security system.
- Why it works: The IR remote activates/deactivates the system, and when active, sensors trigger alarms and log events.
- Key concept: Inputs (IR, touch, crash) control outputs (buzzer, serial log).
โจ Story Time
Imagine your robot as a guardian ๐ก๏ธ. With the remote, you arm or disarm it. If someone touches it or bumps into it, alarms sound and events are recorded. Itโs like having a mini alarm system protecting your robot.
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 2.8.A โ False alarms
Problem: System triggers alarms even when no intrusion.
Clues: Touch sensor values fluctuate.
Broken code:
if tc2.read() < 600: # Threshold too high
print("Intruder detected")
Fixed code:
if tc2.read() < 300: # Correct threshold
print("Intruder detected")
Why it works: Lower threshold avoids false triggers.
Avoid next time: Calibrate sensor values before setting threshold.
๐ Debugging 2.8.B โ It does not detect real events
Problem: System ignores actual intrusions.
Clues: Crash sensor wired incorrectly.
Broken code:
pin5 = machine.Pin(5, machine.Pin.OUT) # Wrong mode
Fixed code:
pin5 = machine.Pin(5, machine.Pin.IN) # Correct mode
Why it works: Crash sensor must be input, not output.
Avoid next time: Always check sensor wiring and pin mode.
โ Final Checklist
- IR remote activates/deactivates system.
- Touch sensor detects intruder when active.
- Crash sensor detects impact when active.
- Buzzer plays different alarms for touch vs impact.
- Serial monitor logs all events.
๐ Extras
- ๐ง Student tip: Test thresholds for touch sensor to avoid false alarms.
- ๐งโ๐ซ Instructor tip: Encourage students to simulate intrusions and observe system response.
- ๐ Glossary: Touch sensor, crash sensor, IR remote, buzzer, intrusion log.
- ๐ก Mini tips:
- Always calibrate sensors before use.
- Use IR Configuration project to map button codes.
- Keep delays short for responsive alarms.