from machine import Pin, I2C
from time import sleep
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
import utime
sda = Pin(0)
scl = Pin(1)
i2c = I2C(0,sda=sda, scl = scl, freq = 400000)
print(i2c.scan())
I2C_ADDR = 39
I2C_ROWS = 2
I2C_COLS = 16
lcd = I2cLcd(i2c, I2C_ADDR, I2C_ROWS, I2C_COLS)
# Ensure that the system is ready by printing a message at the very start.
lcd.move_to(0,0) #line 1
lcd.putstr("*****Press******")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr("*****button*****")
sleep(0.1)
# Initialize PIR sensor, LED, Buzzer, Button, and second LED
pir_sensor = machine.Pin(2, machine.Pin.IN) # PIR sensor input pin
led = machine.Pin(4, machine.Pin.OUT) # LED output pin (used for alarm)
buzzer = machine.Pin(5, machine.Pin.OUT) # Buzzer output pin
button = machine.Pin(6, machine.Pin.IN, machine.Pin.PULL_UP) # Button input pin with internal pull-up resistor
led2 = machine.Pin(7, machine.Pin.OUT) # Second LED output pin (used for flashing when no motion)
# Track the previous motion state and the trigger count
previous_state = False # Initial state is no motion (False)
trigger_count = 0 # Count the number of motion triggers
alarm_active = False # Flag to track whether the alarm is active or not
flashing_led_state = False # Track if the flashing LED should be on or off
delayed_start_active = False # Flag to track if the delayed start process is active
# Function to handle motion detection
def motion_detected():
global previous_state, trigger_count, alarm_active, flashing_led_state
# Read the current state of the PIR sensor
current_state = pir_sensor.value()
# Debugging output to check PIR sensor state
print(f"PIR sensor state: {current_state}")
# Check if motion was detected (state change from no motion to motion)
if current_state == 1 and previous_state == 0:
lcd.move_to(0,0) #line 1
lcd.putstr("*****Motion*****")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr("****Detected****")
sleep(0.1)
trigger_count += 1 # Increment the trigger count
led.value(1) # Turn on the LED when motion is detected
buzzer.value(1) # Turn on the buzzer when motion is detected
alarm_active = True # Set the alarm state to active
flashing_led_state = False # Turn off flashing LED when motion is detected
led2.value(0) # Turn off second LED when motion is detected
utime.sleep(1) # Optional: Add a small delay to prevent multiple triggers
lcd.move_to(0,0) #line 1
lcd.putstr("trigger count:")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr("****************") # Makes sure nothing is on the screen
sleep(0.1)
lcd.move_to(0,0) #line 1
lcd.putstr("trigger count:")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr(str(trigger_count)) # Prints the trigger count
sleep(0.1)
# Check if motion was not detected (no motion)
if current_state == 0:
flashing_led_state = True # Set the flashing LED state to True (for flashing)
if not alarm_active: # If alarm is not active, we can flash the LED
flash_led2() # Call the flashing function
# Update the previous state for the next cycle
previous_state = current_state
# Function to check if the button is pressed to turn off the alarm
def button_pressed():
# Read the button state (active low)
return button.value() == 0 # Button press is detected when the pin is low
# Function to flash LED2 (the second LED)
def flash_led2():
if flashing_led_state:
led2.value(1) # Turn on the second LED
utime.sleep(0.5) # Wait for 0.5 seconds
led2.value(0) # Turn off the second LED
utime.sleep(0.5) # Wait for 0.5 seconds
# Main loop to check for motion and button press
while True:
# Start the delayed start process if the button is pressed
if not delayed_start_active and button_pressed():
lcd.move_to(0,0) #line 1
lcd.putstr("*****system*****")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr("**Arming......**")
sleep(0.1)
utime.sleep(5) # This is okay for allowing the PIR to stabilize
lcd.move_to(0,0) #line 1
lcd.putstr("*****System*****")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr("******Armed*****")
sleep(0.1)
delayed_start_active = True # Mark that the delayed start is now active
# After the delayed start, proceed with normal motion detection and alarm functionality
if delayed_start_active:
motion_detected() # Check for motion and update state
# Check if the button is pressed to disable the alarm
if button_pressed() and alarm_active:
lcd.move_to(0,0) #line 1
lcd.putstr("******Alarm*****")
sleep(0.1)
lcd.move_to(0,1) #line 2
lcd.putstr("****disabled****")
sleep(0.1)
led.value(0) # Turn off the LED
buzzer.value(0) # Turn off the buzzer
led2.value(0) # Turn off the second LED
alarm_active = False # Set the alarm state to inactive
flashing_led_state = True # Ensure flashing LED is enabled when no alarm is active