Home » A Micropython PIR example for the Raspberry Pi Pico

A Micropython PIR example for the Raspberry Pi Pico

by 2b4a3pico71

In this example we connect a PIR module up to our Raspberry PI Pico, this is quite a simple module to connect as it requires only 3v3, Gnd and the output is ok to connect to a Pico  and does not require any level shifting.

A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors.

Here is a typical collection of PIR detectors which can be commonly found on the internet and can be used in many projects. They all should work Ok,we have personally tested 2 of these – the large one at the top and the smaller PIR module in the middle.

There was no difference in how they worked.

pir modules

Interestingly the sensor can be adjusted using the 2 pots on it which you can see underneath

Adjust the distance potentiometer clockwise rotation, increased sensing distance (about 7 meters), on the contrary, the sensing distance decreases (about 3 meters).

Adjust the delay potentiometer clockwise rotation sensor the delay lengthened (300S), on the contrary, shorten the induction delay (5S).

pir adjust

Induction module needs a minute or so to initialize. During initializing time, it will output 0-3 times. One minute later it comes into standby.

Parts Required

The PIR is low cost and you should be able to get one for under $1

 

Schematic/Connection

Black for GND
Red for V+
Yellow for Output

So color coded for ease of use, this layout shows a connection to the module

rp2040 and pir

 

Code Example

I used Thonny for development, you can use any GPIO pin to connect to the PIR but you would need to alter the code below.

from machine import Pin
import utime
from time import sleep

# Connect GP16 to PIR sensor's OUT pin), please use 3.3V connect to VCC on PIR sensor.
pir = Pin(16, Pin.IN, Pin.PULL_UP)


while True:
    print(pir.value())
    sleep(0.1)

 

This example has very basic output, if no object is detected then 0 is output, if an object is detected then a 1 will be outputted

Here is an adapted example with has easier to read output

from machine import Pin
import utime
from time import sleep

# Connect GP16 to PIR sensor's OUT pin), please use 3.3V connect to VCC on PIR sensor.
pir = Pin(16, Pin.IN, Pin.PULL_UP)


while True:
  if pir():
    print('Motion detected!')
    sleep(5)
    if not pir():
        print('Motion stopped')

 

Output

Here is what I saw in the REPL window

Motion detected!
Motion stopped
Motion detected!
Motion stopped

Motion detected!
Motion stopped

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.