print("Hello, ESP32!")
print("This program integrates PIR sensor with ESP32")
# Credits to https://RandomNerdTutorials.com & program edited accordingly
# 1.import all necessary module / library
from machine import Pin # to declare Pin 14 & 12
from utime import sleep
# 2. INITIALIZE THE MOTION
motion = False #initialize there is no movement
# 3. create function interrupt handling
# THIS FUNC WILL BE CALL EVERY TIME A MOTION IS DETECTED
def handle_interrupt(pin): #def = function
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
# 4. declare input output pin
# LED as output and PIR sensor as input
led_yellow = Pin(14, Pin.OUT) # OUT = output
pir = Pin(12, 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 how 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 Red = ON' )
for i in range (10): # akan blinking for 10 times
led_yellow.on()
sleep(0.5)
led_yellow.off()
sleep(0.5)
print('Motion stopped! LED Red = OFF') # no motion
motion = False