from machine import Pin, I2C
import ds1307
import time
# Define GPIO pins
PIR_PIN = 26
ALARM_PIN = 6
# Initialize I2C bus
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=100000)
# Initialize DS1307
rtc = ds1307.DS1307(i2c)
# Set up the PIR sensor and Alarm pin
pir = Pin(PIR_PIN, Pin.IN)
alarm = Pin(ALARM_PIN, Pin.OUT)
# Function to turn on the alarm
def alarm_on():
alarm.value(1)
# Function to turn off the alarm
def alarm_off():
alarm.value(0)
print("Home Security System Ready")
time.sleep(2) # Give the PIR sensor some time to initialize
print("Monitoring for motion...")
try:
while True:
if pir.value() == 1:
print("Motion Detected!")
alarm_on()
datetime = rtc.datetime()
print("Date: {:04}-{:02}-{:02} Time: {:02}:{:02}:{:02}".format(datetime[0], datetime[1], datetime[2], datetime[4], datetime[5], datetime[6]))
time.sleep(5) # Alarm duration
alarm_off()
else:
alarm_off()
time.sleep(1) # Adjust the delay for your needs
except KeyboardInterrupt:
print("Exiting program")
alarm_off()