Qwiic PIR Hookup Guide

Contributors: El Duderino, Englandsaurus
Favorited Favorite 0

Python Examples

The SparkFun Qwiic PIR Python Library includes four examples to help users get started with the board and library. In this section we'll go over the examples and highlight how they work.

To use the examples, open them from the Python library's location or copy the code into your preferred Python interpreter.

Example 1 - Simple Example (Raw PIR Readings)

The first example shows how to set up the Qwiic PIR on the I2C bus and retrieve raw data from the PIR's output signal. Because we are reading raw PIR output the code manually sets up a debounce time (in milliseconds) to wait for the PIR output signal to stabilize. Adjust the debounce time by changing this value:

debounce_time = .20

The main example loop sets up the PIR object, attempts to initialize it on the I2C bus and, if successful, waits 30 seconds for the PIR to stabilize:

def run_example():

        print("\nSparkFun Qwiic PIR Example1\n")
        my_PIR = qwiic_pir.QwiicPIR()

        if my_PIR.begin() == False:
                print("The Qwiic PIR isn't connected to the system. Please check your connection", \ file=sys.stderr)

        print ("Waiting 30 seconds for PIR to stabilize")
        for i in range(0,30):

        print("Device Stable")

Once the PIR has initialized and stabilized, the code begins to take readings and print out whether an object was detected or removed, pausing for the value set for debounce_time after each reading:

while True:
        if my_PIR.raw_reading() is True:
                print("Object Detected")
                print("Object Removed")

Example 2 - Debounced Readings

The second example demonstrates how to read the debounced output signals from the Qwiic PIR using the object_detected() and object_removed() functions. Just like with the Arduino library, the Qwiic PIR Python Library includes functions for both raw PIR readings as well as automatically debounced readings.

Using the object_detected()/removed() functions allows you to set a debounce time with the set_debounce_time() function and the PIR will always wait for that amount of time before taking another reading. The default value for set_debounce_time() is 750ms.

The code initializes the Qwiic PIR on the bus and waits for 30 seconds for the PIR to stabilize prior to taking readings for object detections:

while True:
        if my_PIR.available() is True:
                if my_PIR.object_detected():
                        print("Object Detected")
                if my_PIR.object_removed():
                        print("Object Removed")

Example 3 - Queue Usage

The third example shows how to read values stored for the Object Detected and Object Removed queues. After initializing the sensor and waiting for it to stabilize the code then prints out values stored in Detected Queue & Removed Queue for both time (in seconds) since the last (most recent) object detection or removal event as well as the time since the oldest stored object detection or removal event. If either queue is empty, the code prints out which queue is empty.

while True:
        if my_PIR.is_detected_queue_empty() is False:
                last_detect = my_PIR.time_since_last_detect() / 1000.0
                first_detect =  my_PIR.time_since_first_detect() / 1000.0
                print("s since last PIR detect   ")
                print("s since first PIR detect   ")
                print("Detected queue is empty")

        if my_PIR.is_removed_queue_empty() is False:
                last_remove = my_PIR.time_since_last_remove() / 1000.0
                first_remove =  my_PIR.time_since_first_remove() / 1000.0
                print("s since last PIR detect   ")
                print("s since first PIR detect   ")
                print("Removed queue is empty")

Example 4 - Pop Queue

Example 4 demonstrates how to pop values from both the object_detected() and object_removed() queues by sending the appropriate characters over serial.

Just like the other examples, the Qwiic PIR is initialized on the I2C bus and the code waits for 30 seconds for the PIR to stabilize it's readings. After waiting, the code prints out to enter either "d" or "r" to pop values from either the detected (d) or removed (r) queues:

while True:
    print("\mType 'd' to pop from the detected queue.")
    val = raw_input("Type 'r' to pop from the removed queue: ")
    # If the character is 'd' or 'D', then pop a value off the detected queue
    if val == 'd' or val == 'D':
        print("\nPopped detected queue! The first timestamp in detected queue was: ")
        print(str(my_PIR.pop_detected_queue() / 1000.0))

    # If the character is 'r' or 'R', then pop a value off the removed queue
    if val == 'r' or val == 'R':
        print("\nPopped removed queue! The first timestamp in removed queue was: ")
        print(str(my_PIR.pop_removed_queue90 / 1000.0))


If the correct value is entered the code prints out over serial the respective queue has been popped and prints out the timestamp for the removed value in seconds.