print("This program integrates PIR sensor with ESP32")
# Credits to https://RandomNerdTutorials.com & program edited accordingly
#1.Import necessary modules/sensor libraries
from machine import Pin #for pin declaration
from time import sleep #delay
#2. Initialize the motion value
motion = False
#3.Create a function called handle_interrupt
#remarks : This function will be called everytime a motion is detected
def handle_interrupt(pin): #def --> this is a function
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
#4.Declare the input & output pin
led_purple = Pin(27, Pin.OUT)
pir_sensor = Pin(25, Pin.IN)
#5.Set an interrupt by using irq() method
pir_sensor.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
#6.Time to test out PIR sensor
while True:
if motion == True:
print('Motion detected! Interrupt caused by:', interrupt_pin, 'LED Purple = ON' )
led_purple.on()
sleep(5)
led_purple.off()
print('Motion stopped! LED Purple = OFF')
motion = False