print("This program integrate PIR sensor")
from machine import Pin #Pin declaration
from utime import sleep #delay
motion = False # Initialize motion to zero /false
#Create a function called handle_interrupt
#Remarks: This function will be called everytime a motion is detacted
def handle_interrupt(pin): #This is a function
global motion # Motion sentiasa dilihat
motion = True
global interrupt_pin
interrupt_pin = pin
#Declare the input and output Pin
led = Pin(2, Pin.OUT)
pir = Pin(15, Pin.IN)
# Set an interrupt on PIR sensor using irq()method
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
while True:
if motion:
print('Motion detected! LED ON, Interrupt caused by:', interrupt_pin)
led.value(1)
sleep(5)
led.value(0)
print('Motion stopped!,LED OFF')
motion = False