print("Project: Smart Surveillance System for Public Safety")
print("Developed by: NOOR ARIQAH BINTI MOHAMAD")
print("Date: 20/4/2025")
print("TITLE THEME SDG 16: Peace, Justice and Strong Institutions")
# Import libraries
from machine import Pin, ADC, PWM, I2C
import time
# OLED Setup (I2C Pins)
i2c = I2C(scl=Pin(22), sda=Pin(21)) # GPIO14=SCL, GPIO12=SDA
oled_width = 128
oled_height = 64
oled = None
try:
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(oled_width, oled_height, i2c)
except ImportError:
# Fallback for Wokwi (if ssd1306 not available)
class SSD1306_I2C:
def __init__(self, width, height, i2c):
self.width = width
self.height = height
self.buffer = bytearray(width * height // 8)
def fill(self, color): pass
def text(self, text, x, y): print(f"OLED: {text}")
def show(self): pass
oled = SSD1306_I2C(oled_width, oled_height, i2c)
# Pin Declaration
pir_sensor = Pin(4, Pin.IN) # PIR motion sensor on GPIO4 (D4)
potentiometer = ADC(Pin(13)) # Potentiometer on GPIO13
led = Pin(2, Pin.OUT) # LED on GPIO2 (D2)
buzzer = PWM(Pin(5, Pin.OUT)) # Buzzer on GPIO5 (D5)
# Thresholds
POT_THRESHOLD = 2000 # Adjust based on potentiometer readings (0-4095)
DEBOUNCE_TIME = 0.5 # Seconds
def update_display(status, details=""):
"""Update OLED display with status message"""
oled.fill(0)
oled.text("Surveillance System", 0, 0)
oled.text("Status: " + status, 0, 16)
if details:
oled.text(details, 0, 26)
oled.text("Pot:" + str(potentiometer.read()), 0, 50)
oled.show()
def trigger_alarm(level):
"""Trigger different alarm levels"""
if level == "low":
update_display("MOTION DETECTED", "Low risk")
led.on()
time.sleep(0.5)
led.off()
elif level == "medium":
update_display("HIGH ACTIVITY", "Check area")
buzzer.freq(2000)
buzzer.duty(512)
time.sleep(0.3)
buzzer.duty(0)
elif level == "high":
for _ in range(5):
update_display("INTRUDER ALERT!", "CALL SECURITY")
led.on()
buzzer.freq(3000)
buzzer.duty(512)
time.sleep(0.2)
led.off()
buzzer.duty(0)
time.sleep(0.2)
# Main program
while True:
motion = pir_sensor.value()
pot_value = potentiometer.read()
if motion and pot_value > POT_THRESHOLD:
trigger_alarm("high")
elif pot_value > POT_THRESHOLD:
trigger_alarm("medium")
elif motion:
trigger_alarm("low")
else:
update_display("Area Secure", "All normal")
led.off()
buzzer.duty(0)
time.sleep(DEBOUNCE_TIME)