Using a PIR Shield to detect motion, but only reading “0”s. The LEDs turn on if led_pin.value(1). Powering the board (RT1062) with usb-c.
Tried “P10”, the same. print(pir.value()) prints zeros (0)
import time
from machine import Pin
# Turn on white LEDs
led_pin = Pin("P7", Pin.OUT)
led_pin.value(0)
PIR_PIN = "P9"
pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
print("Reading PIR on", PIR_PIN)
while True:
v = pir.value()
if v == 0:
led_pin.value(0)
else:
led_pin.value(1)
print(pir.value())
time.sleep_ms(100)
Thanks for the quick reply. I’m running the code below, but the LEDs are turning on and off sporadically, regardless of what is happening in the environment. (Ultimately, I want to use the PIR sensor to trigger data capture and put the RT1062 board to sleep when there’s no motion.) For now though, I’m just trying to understand how the PIR shield works, and I’m struggling.
As a first step, I’d like to have the LEDs reliably turn on and off based on detected motion (i.e., the PIR output). Does my current hardware setup look correct, or is there an issue with my code? Could you also point me toward some simple examples or documentation?
Thank you,
from machine import Pin
# Turn on white LEDs
led_pin = Pin("P7", Pin.OUT)
led_pin.value(0)
PIR_PIN = "P11"
pir = Pin(PIR_PIN, Pin.IN) #, Pin.PULL_DOWN)
print("Reading PIR on", PIR_PIN)
while True:
v = pir.value()
if v == 0:
led_pin.value(1)
else:
led_pin.value(0)
Hi, the PIR shield triggers and pulls low on motion. Its output is sporadic. You should latch whatever is happening and then keep the lights on for a certain amount of time. Your code right now assume the PIR sensor has hold after triggering which it does not.
Thank you. I modified the code accordingly. Turn and leave the LED on when the PIR pulls low on motion.
This seems to work now, however, the PIR seems to be very “sensitive”
# Read PIR digital value and turn lights on when there is motion
import time
from machine import Pin
# Turn off white LEDs
led_pin = Pin("P7", Pin.OUT)
led_pin.value(0)
PIR_PIN = "P11"
pir = Pin(PIR_PIN, Pin.IN)
class PIRMotion:
def __init__(self, event_length=5*1000):
self.event_length = event_length
self.start_time = time.ticks_ms() - (event_length + 1)
self.active = False
pir_motion = PIRMotion()
while True:
# detect pir motion
if time.ticks_diff(time.ticks_ms(), pir_motion.start_time) > pir_motion.event_length:
pir_motion.active = False
if pir.value() == 0:
pir_motion.start_time = time.ticks_ms()
pir_motion.active = True
# print(pir_motion.start_time)
# turn LEDs on/off
if pir_motion.active:
led_pin.value(1) # LED on
else:
led_pin.value(0) # LED off