Home CodeMicropython Micropython PIR example for the Raspberry Pi Pico

Micropython PIR example for the Raspberry Pi Pico

by rp2040guy71

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, I have tested the bigger one at the top and the SR602 one at the bottom with the same results.

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).

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

Name Link
Pico Raspberry Pi Pico Development Board
PIR HC-SR501 HC-SR505 SR602 AM312 PIR Motion Sensor Detector Module
Connecting cables Aliexpress linkAmazon.com link

Ebay link

 

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

Code Example

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

[codesyntax lang=”python”]

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)

[/codesyntax]

This has a simple 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 easier to read output

[codesyntax lang=”python”]

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')

[/codesyntax]

Output

Here is what I saw in Thonny REPL window

Motion detected!
Motion stopped
Motion detected!
Motion stopped

Links

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More