CircuitPython Code

It's easy to use a PIR sensor with CircuitPython using simple digital inputs.  The PIR sensor looks and acts kind of like a button or switch, i.e. it's only ever a high or low logic level, so you don't need any special libraries or other code to read one from Python.  It will help to familiarize yourself with CircuitPython digital inputs and outputs before continuing though!

First make sure your PIR sensor is wired to your board as shown in the previous page.  There's no difference wiring a PIR sensor to an Arduino vs. CircuitPython board.  You must connect the power, ground, and sensor output to your board.  The sensor output should be connected to any digital I/O line on your board.  In this example we'll use pin D2 on a Trinket M0.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Run the following code to import the board and digitalio modules which lets you read digital inputs:

import board
import digitalio

Then create a simple digital input for the PIR.  Remember to use the right board pin for how you've wired your sensor to your board.  This example is using pin D2 on a Trinket M0:

pir = digitalio.DigitalInOut(board.D2)
pir.direction = digitalio.Direction.INPUT

At this point you can read the state of the sensor by reading the value property.  If the value is at a low logic level, or False, the sensor sees no movement.  If it's at a high logic level, or True, the sensor is detecting movement! 

Note you'll likely want the sensor's jumper in the H position for retriggering mode as mentioned on the previous page.

For example with no movement in front of the sensor you might see:


Then wave your hand in front of the sensor, and as you wave it run the same command again.  Notice you get a True result!


That's all there is to using a PIR sensor with CircuitPython!

Here's a complete example just like from the previous page where movement from the PIR sensor will turn on the board's LED and print a message.  This is a direct port of the previous page's Arduino example to CircuitPython.  Try saving it as a on your board and connecting to the serial terminal to see the output as it runs! (be sure to change the board pin numbers to your sensor and LED wiring!)

import board
import digitalio

LED_PIN = board.D13  # Pin number for the board's built in LED.
PIR_PIN = board.D2   # Pin number connected to PIR sensor output wire.

# Setup digital input for PIR sensor:
pir = digitalio.DigitalInOut(PIR_PIN)
pir.direction = digitalio.Direction.INPUT

# Setup digital output for LED:
led = digitalio.DigitalInOut(LED_PIN)
led.direction = digitalio.Direction.OUTPUT

# Main loop that will run forever:
old_value = pir.value
while True:
    pir_value = pir.value
    if pir_value:
        # PIR is detecting movement! Turn on LED.
        led.value = True
        # Check if this is the first time movement was
        # detected and print a message!
        if not old_value:
            print('Motion detected!')
        # PIR is not detecting movement. Turn off LED.
        led.value = False
        # Again check if this is the first time movement
        # stopped and print a message.
        if old_value:
            print('Motion ended!')
    old_value = pir_value
Last updated on 2017-10-25 at 11.57.59 AM Published on 2014-01-28 at 06.09.06 PM