import machine
import utime
#It starts by importing necessary libraries, specifically "machine" and "utime." These libraries provide functions for interacting with hardware and handling time-related tasks on the microcontroller
sensor_pir1 = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
sensor_pir2 = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN)
#Two PIR sensors are connected to the microcontroller's pins. One is connected to pin 28, and the other to pin 22. These sensors are set up to detect motion
led = machine.Pin(15, machine.Pin.OUT)
buzzer = machine.Pin(14, machine.Pin.OUT)
#Two output pins are also defined: one for an LED (pin 15) and another for a buzzer (pin 14). These will be used to signal the alarm when motion is detected
print("Wolcome")
def pir_handler(pin):
#defines a function called "pir_handler," which is used as a callback when motion is detected by either of the PIR sensors
utime.sleep_ms(100)#Sleeps for 100 milliseconds to debounce the PIR sensor
if pin.value():
if pin is sensor_pir1:
print("ALARM! Motion detected in bedroom!")
elif pin is sensor_pir2:
print("ALARM! Motion detected in living room!")
#Checks if the pin that triggered the interrupt is active (motion detected)
for i in range(50):
led.toggle()
buzzer.toggle()
utime.sleep_ms(100)
#It then enters a loop that toggles the LED and the buzzer on and off 50 times, each lasting for 100 milliseconds. This creates a blinking and buzzing effect to signal the alarm
sensor_pir1.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_pir2.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)#Both PIR sensors are set up to trigger an interrupt (IRQ) when motion is detected (IRQ_RISING). They use the "pir_handler" function to handle these interrupts
while True:
led.toggle()
utime.sleep(5)#The script enters a continuous loop (while True) that toggles the LED every 5 seconds. This is essentially a heartbeat or status indicator to show that the system is running