print("This program integrates PIR sensor with ESP32")
# Credits to https://RandomNerdTutorials.com & program edited accordingly
#1. Import all necessary modules/library
from machine import Pin # This is to declare Pin 12 & 15
from time import sleep # utime is for upython
#2. Initializes the motion parameter
motion = False #Initially there is no movement
#3. Create function interrupt handling
#Remarks: This functio will be called every time a motion is detected!
def handle_interrupt(pin): #def is for function
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
#4. Declare input and output pin
#Remarks: LED as output and PIR sensor as Input
led_blue = Pin(12, Pin.OUT) #OUT = Output
pir = Pin(15, Pin.IN) #IN = Input
#5. Set interrupt pin on PIR sensor by using irq() method
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
#6. Time to test! Now we can compare the output of LED when there is motion or no motion
while True: #forever loop
if motion == True: #A motion has been detected
print('Motion detected! Interrupt caused by:', interrupt_pin, 'LED Blue = ON' )
for i in range (5): #repetition(LED blinking)
led_blue.on()
sleep(0.5)
led_blue.off()
sleep(0.5)
print('Motion stopped! LED Blue = OFF') #No motion
motion = False