print("This program intergrates PIR sensor with ESP32")
#1. import all necessary modules/library
from machine import Pin #declare pin 14 & 12
from utime import sleep #small u for micropython
#2. initialised motion parameter
motion = False #initially there is no movement
#3. create function interrupt handling
#Remarks: this function will be called everytime 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
#Reamrks: LED as output, PIR sensor as input
led = Pin(12, Pin.OUT)
pir = Pin(14, Pin.IN)
#5. Set interrupt pin on PIR sensor by using irp() method
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
#6. Time to test. Now we can compare theoutput of LED when there is motion or not
while True:
if motion == True: #A motion has been detected
print('Motion detected! Interrupt caused by:', interrupt_pin)
for i in range (10): #for loop - blinking (repitition) {while loop also for repitition}
led.on()
sleep(0.2)
led.off()
sleep(0.2)
print('Motion stopped!')
motion = False