reading PIR sensor/pin via interrupt
Posted: Wed Jan 19, 2022 4:20 am
TW: TOTAL NOOB question. I'm working through "Getting Started with Micropython on Raspberry Pi Pico", there's a Burglar Alarm project using the HC-SR501 PIR sensor. The example code works:
and prints out the message whenever the PIR is triggered. What I want to do is print one message on trigger, and then another when the PIR untriggers (pin28 returns to low). I tried this:
and multiple other permutations of if and elif; I still get the Danger message but never the clear. I may not understand, in the textbook example, what exactly that if is evaluating... is that "if pin.value is not empty", or?
I also tried adding second .irq with an IRQ_FALLING condition, but by then I was just randomly flailing and not getting anywhere. Thanks in advance for your help!
Code: Select all
import machine
import utime
sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
def pir_handler(pin):
utime.sleep_ms(100)
if pin.value():
print("Danger Will Robinson!!!")
sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
Code: Select all
def pir_handler(pin):
utime.sleep_ms(100)
if pin.value()==1:
print("Danger Will Robinson!!!")
elif pin.value()==0:
print("coast is clear")
sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
I also tried adding second .irq with an IRQ_FALLING condition, but by then I was just randomly flailing and not getting anywhere. Thanks in advance for your help!