# Import required modules for hardware control
from machine import Pin, ADC, I2C, PWM
from time import sleep
import ssd1306 # Our custom driver with extended drawing functions
# Initialize I2C for OLED: SCL on GPIO 5, SDA on GPIO 4
i2c = I2C(0, scl=Pin(5), sda=Pin(4))
# Initialize OLED display: width 128, height 64
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Setup RGB LED pins (Common Cathode)
red_led = Pin(21, Pin.OUT)
green_led = Pin(22, Pin.OUT)
blue_led = Pin(23, Pin.OUT)
# Setup Buzzer pin using PWM for tones
buzzer = PWM(Pin(18))
buzzer.duty(0) # Initially off
# Setup potentiometer on ADC input
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB) # Full voltage range 0-3.3V
# Setup buttons with internal pull-up resistors
button_green = Pin(19, Pin.IN, Pin.PULL_UP)
button_red = Pin(27, Pin.IN, Pin.PULL_UP)
button_yellow = Pin(32, Pin.IN, Pin.PULL_UP)
# Function to set RGB LED color
def set_color(r, g, b):
red_led.value(r)
green_led.value(g)
blue_led.value(b)
# Function to turn off RGB LED
def turn_off_rgb():
set_color(0, 0, 0)
# Function to make the buzzer beep a tone
def play_tone(frequency, duration):
if frequency == 0:
buzzer.duty(0)
return
buzzer.freq(frequency)
buzzer.duty(512) # 50% duty cycle
sleep(duration)
buzzer.duty(0)
# Function to display a text message on OLED
def display_message(message):
oled.fill(0) # Clear screen
oled.text(message, 0, 0) # Write text at position (0,0)
oled.show() # Refresh display
# Function to draw a siren icon on OLED
def draw_siren(oled, x, y):
"""
Draw a siren icon at (x, y)
"""
oled.fill_rect(x, y + 10, 20, 6, 1) # Siren base
oled.circle(x + 10, y + 10, 8, 1) # Siren outer light
oled.circle(x + 10, y + 10, 4, 0) # Reflection inner circle
oled.hline(x + 6, y, 8, 1) # Top horizontal flash line
oled.vline(x + 2, y + 3, 4, 1) # Left vertical flash
oled.vline(x + 18, y + 3, 4, 1) # Right vertical flash
oled.pixel(x + 5, y + 1, 1) # Top-left dot
oled.pixel(x + 15, y + 1, 1) # Top-right dot
# Function to beep buzzer multiple times
def beep(times, frequency=1500, duration=0.2, pause=0.2):
for _ in range(times):
play_tone(frequency, duration)
sleep(pause)
# Display startup message
display_message("System Ready")
sleep(1)
# === Main Loop ===
while True:
# Read potentiometer value (0-4095)
pot_value = pot.read()
# Default state: show potentiometer value
oled.fill(0)
oled.text("Pot: {}".format(pot_value), 0, 0)
oled.show()
# Turn off RGB and buzzer by default
turn_off_rgb()
buzzer.duty(0)
# Green Button → Safe Mode
if button_green.value() == 0:
print("Green Button: Safe Mode")
set_color(0, 1, 0) # Turn on green LED
oled.fill(0)
oled.text("System Safe", 0, 0)
oled.show()
sleep(0.5)
# Red Button → Intrusion Detected
elif button_red.value() == 0:
print("Red Button: Intrusion Detected")
set_color(1, 0, 0) # Turn on red LED
oled.fill(0)
oled.text("Surveillance!", 0, 0)
draw_siren(oled, 54, 20) # Draw siren at (54,20)
oled.show()
play_tone(2000, 0.5)
sleep(0.5)
# Yellow Button → Manual Scan Mode
elif button_yellow.value() == 0:
print("Yellow Button: Manual Scan")
set_color(1, 1, 0) # Yellow color (Red + Green)
oled.fill(0)
oled.text("Manual Scan", 0, 0)
oled.show()
beep(2, 1200, 0.2, 0.2) # 2 quick beeps
sleep(0.5)
# Short debounce delay
sleep(0.1)