"""
Face Recognition Access Control System - Wokwi Simulator
=========================================================
Raspberry Pi Pico Version
This simulates the GPIO behavior of a face recognition door lock system.
Since Wokwi doesn't have a camera module, we use the button to simulate
face detection events.
Hardware Connections:
- GPIO 16: Relay (Door Lock)
- GPIO 17: Green LED (Access Granted)
- GPIO 18: Red LED (Access Denied)
- GPIO 19: Buzzer (Alert)
- GPIO 20: Push Button (Simulate Face Scan / Manual Trigger)
- GPIO 21: Second Button (Simulate Known Face)
How to Test:
1. Press the BLUE button (GP20) to simulate "Unknown Face" → Red LED + Buzzer
2. Press the GREEN button (GP21) to simulate "Known Face" → Green LED + Relay (Unlock)
3. System auto-resets after 3 seconds
"""
from machine import Pin
import time
# ==================== PIN CONFIGURATION ====================
# Output pins
RELAY_PIN = 16 # Door lock relay
GREEN_LED_PIN = 17 # Access granted indicator
RED_LED_PIN = 18 # Access denied indicator
BUZZER_PIN = 19 # Alert buzzer
# Input pins
SCAN_BUTTON_PIN = 20 # Simulate face scan (unknown face)
KNOWN_FACE_PIN = 21 # Simulate known face detection
# ==================== INITIALIZE HARDWARE ====================
# Outputs
relay = Pin(RELAY_PIN, Pin.OUT)
green_led = Pin(GREEN_LED_PIN, Pin.OUT)
red_led = Pin(RED_LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)
# Inputs (with internal pull-up resistors)
scan_button = Pin(SCAN_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
known_face_button = Pin(KNOWN_FACE_PIN, Pin.IN, Pin.PULL_UP)
# ==================== SYSTEM STATE ====================
class SystemState:
IDLE = 0
SCANNING = 1
ACCESS_GRANTED = 2
ACCESS_DENIED = 3
current_state = SystemState.IDLE
state_timer = 0
# ==================== HELPER FUNCTIONS ====================
def all_off():
"""Turn off all outputs"""
relay.value(0)
green_led.value(0)
red_led.value(0)
buzzer.value(0)
def access_granted():
"""Handle successful face recognition"""
global current_state, state_timer
print("✅ ACCESS GRANTED - Face Recognized!")
print(" 🔓 Door Unlocked")
# Turn on green LED and relay (unlock door)
green_led.value(1)
relay.value(1)
red_led.value(0)
buzzer.value(0)
current_state = SystemState.ACCESS_GRANTED
state_timer = time.ticks_ms()
def access_denied():
"""Handle failed face recognition"""
global current_state, state_timer
print("❌ ACCESS DENIED - Unknown Face!")
print(" 🔒 Door Remains Locked")
# Turn on red LED and buzzer (alert)
red_led.value(1)
buzzer.value(1)
green_led.value(0)
relay.value(0)
current_state = SystemState.ACCESS_DENIED
state_timer = time.ticks_ms()
def scanning_effect():
"""Visual feedback during scanning"""
print("🔍 Scanning face...")
for i in range(3):
green_led.value(1)
red_led.value(1)
time.sleep(0.1)
green_led.value(0)
red_led.value(0)
time.sleep(0.1)
def idle_state():
"""Return to idle state"""
global current_state
print("💤 System Ready - Waiting for face scan...")
all_off()
current_state = SystemState.IDLE
def flash_red_led():
"""Flash red LED for denied state"""
red_led.value(not red_led.value())
# ==================== STARTUP SEQUENCE ====================
def startup_sequence():
"""Visual startup test"""
print("\n" + "="*50)
print("🚀 Face Recognition Access Control System")
print(" Raspberry Pi Pico - Wokwi Simulator")
print("="*50)
print("\nRunning startup test...")
# Test each component
print(" Testing Green LED...")
green_led.value(1)
time.sleep(0.3)
green_led.value(0)
print(" Testing Red LED...")
red_led.value(1)
time.sleep(0.3)
red_led.value(0)
print(" Testing Buzzer...")
buzzer.value(1)
time.sleep(0.2)
buzzer.value(0)
print(" Testing Relay...")
relay.value(1)
time.sleep(0.3)
relay.value(0)
print("\n✅ All components OK!")
print("\n" + "-"*50)
print("INSTRUCTIONS:")
print(" 🔵 Press BLUE button → Simulate UNKNOWN face")
print(" 🟢 Press GREEN button → Simulate KNOWN face")
print("-"*50 + "\n")
# ==================== MAIN LOOP ====================
def main():
global current_state, state_timer
startup_sequence()
idle_state()
last_flash = 0
flash_interval = 200 # ms
while True:
current_time = time.ticks_ms()
# Check for button presses in IDLE state
if current_state == SystemState.IDLE:
# Check for unknown face simulation (blue button)
if scan_button.value() == 0:
scanning_effect()
time.sleep(0.5) # Simulate processing
access_denied()
time.sleep(0.3) # Debounce
# Check for known face simulation (green button)
elif known_face_button.value() == 0:
scanning_effect()
time.sleep(0.5) # Simulate processing
access_granted()
time.sleep(0.3) # Debounce
# Handle ACCESS_GRANTED state
elif current_state == SystemState.ACCESS_GRANTED:
# Auto-reset after 3 seconds
if time.ticks_diff(current_time, state_timer) > 3000:
print("🔒 Door Auto-Locked")
idle_state()
# Handle ACCESS_DENIED state
elif current_state == SystemState.ACCESS_DENIED:
# Flash red LED
if time.ticks_diff(current_time, last_flash) > flash_interval:
flash_red_led()
last_flash = current_time
# Auto-reset after 3 seconds
if time.ticks_diff(current_time, state_timer) > 3000:
idle_state()
time.sleep(0.01) # Small delay to prevent tight loop
# Run the main program
if __name__ == "__main__":
main()