Shop OBEX P1 Docs P2 Docs Learn Events
PIR and motion detection — Parallax Forums

PIR and motion detection

The Wide Angle PIR Sensor with LED Signal SKU 28032 is currently on sale (thanksgiving 2023 until Nov 27) 50% off so it's $7.50 at the Parallax store and in my opinion a pretty good deal for this particular sensor. If you miss the sale it's still worth the $14.99.

I have one thats been sitting around for a month or two so this seemed like a good opportunity to test it with a Micro:bit. I looked for a PIR Micro:bit example but could not find one on the forum so I apologize if this post duplicates any other information on the Parallax web site and here is my take.

The code contains a few print statements which are only there to help with the initial set up and can be removed at any time. I tried the PIR sensor with a couple of different micro controllers and each gave a slightly different behavior so to account for this there are a couple of variables in case you need to fine tune things. Initial set up is done with the PIR shielded from movement (cover it with a small box)

At the top of the listing we have our imports followed by a zero_cnt variable. I found that the PIR needed time to initialize/warm-up so the way I monitored the warm up was by looking at the signal from the PIR and when I saw the signal was zero for a period of 2.5 seconds I considered it to be in its ready state, the 2.5 second time period (0.5 x 5) can be modified by increasing or decreasing by 0.5 seconds using the zero_cnt variable. Warm up was quick with the Micro:bit but with an ESP32 I used it could take as long as 20 seconds.

Below the zero_cnt variable we define our two pin objects pin0 is an input for the PIR signal, I found there were cases when it worked better with the PULL_DOWN. Next is pin1 which is an output attached to the enable pin of the PIR and set at a value of logic 1 in this code, if you wished you could add code and use the enable to switch the sensor detection on or off.

from microbit import *
from time import ticks_ms

zero_cnt=5

pin0.set_pull(pin0.PULL_DOWN) # input signal
pin1.write_digital(1) # enable pin

Lets skip the sub routine a second and look at the following while loop, this loop looks for logic zero sent from the PIR for 2.5 seconds during warm up. If you find the sensor is activating the alarm sub routine before you remove the shield/box from over the sensor then increasing the value of the zero_cnt variable may help. The controllers I tried the 2.5 seconds was ample.

while zero_cnt > 0:
    if pin0.read_digital()==0:
        print(zero_cnt)
        zero_cnt -=1
        sleep(500)
    else:
        zero_cnt=0

Below that we have a "forever loop" reading the value of the PIR signal, if the PIR sends a logic 1 signal the alarm sub routine will be called.

while True:
    if pin0.read_digital()==1:
        alarm(3000)
        sleep(1000)

Finally the alarm sub routine takes a time value in milliseconds as a paramater, which I have at 3000 (3 seconds), and the alarm will remain active for the duration of the time paramater. Here is the full listing including the alarm sub routine.

from microbit import *
from time import ticks_ms

zero_cnt=5

pin0.set_pull(pin0.PULL_DOWN) # input signal
pin1.write_digital(1) # enable pin

#----------------------------------------------------    
def alarm(active_time):
    start=ticks_ms()
    elapsed=0

    print("Activated")

    while elapsed < active_time:
        elapsed=ticks_ms()-start
        # control a timed output here

    print("Active for ",int(elapsed/1000)," ","seconds\n")
    print("Ready")
#----------------------------------------------------

print("Initializing")

while zero_cnt > 0:
    if pin0.read_digital()==0:
        print(zero_cnt)
        zero_cnt -=1
        sleep(500)
    else:
        zero_cnt=0

print("Ready")

while True:
    if pin0.read_digital()==1:
        alarm(3000)
        sleep(1000)

Comments

  • bruceebrucee Posts: 239
    edited 2023-12-06 18:24

    Just got my sensors as well. One thing that nearly stumped me was the output needs a pulldown resistor. I used 10K.

    I'm connecting it to a different ARM input, and that input needs 10 nA or less to pull it low, the PIR seems not capable of that.

    Hence I see your code enabling pulldown on the input. That really is a bit out of the ordinary.

  • @brucee good job, I hope you got the sensors at the sale price if not it is still a reasonable buy.

    IMO its always a good idea to have a pullup or pulldown on input pins to rule out the possibility of floating values. The thing that got me with these PIR's was the warm up time, during that period the output would go hi and lo erratically and I wanted a way to determine the settling time hence waiting for a set number of zero values in a predetermined time period

Sign In or Register to comment.