Qwiic PIR Hookup Guide

Pages
Contributors: El Duderino, Englandsaurus
Favorited Favorite 0

Qwiic PIR Python Package

Note: This tutorial assumes you are using the latest version of Python 3. If this is your first time using Python or I2C hardware on a Raspberry Pi these tutorials will help you get started:

We've written a Python package for the Qwiic PIR for users who prefer to use something like a Raspberry Pi with their sensor. The package can be installed with the all inclusive SparkFun Qwiic Python package or independently.

We recommend installing the entire SparkFun Qwiic Package as it also installs the required I2C driver.

Note: Don't forget to double check that the hardware I2C connection is enabled on your Raspberry Pi or other single board computer. Make sure to reboot your Pi after enabling the I2C bus for changes to take effect.

SparkFun Qwiic Package

This repository is hosted on PyPi as the sparkfun-qwiic package. On systems that support PyPi installation via pip3 (use pip for Python 2) is simple using the following commands:

For all users (Note: the user must have sudo privileges):

language:bash
sudo pip3 install sparkfun-qwiic

For the current user:

language:bash
pip3 install sparkfun-qwiic

Independent Qwiic PIR Py Package Installation

If you prefer to only install the Qwiic PIR package, you can download the sparkfun-qwiic-pir Python package hosted by PyPi via pip3 following the instructions below. Alternatively, if you prefer to manually download and build the libraries you can grab them from the Qwiic PIR Py GitHub Repository or by clicking the button below:

PyPi Installation

This repository is hosted on PyPi as the sparkfun-qwiic-PIR package. On systems that support PyPi, install the sparkfun-qwiic-PIR package via pip3 (use pip for Python 2) using the following commands:

For all users (Note: the user must have sudo privileges):

language:bash
sudo pip3 install sparkfun-qwiic-PIR

For the current user:

language:bash
pip3 install sparkfun-qwiic-PIR

Local Installation

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line (use python for Python 2):

language:bash
python3 setup.py install

To build a package for use with pip3:

language:bash
python3 setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip3.

language:bash
cd dist
pip3 install sparkfun_qwiic_PIR-<version>.tar.gz

Qwiic PIR Python Package Operation

Let's take a quick look at the functions available in the Python package. For more details on how the package works, take a look at the source code and package documentation hosted on ReadTheDocs.

Dependencies

This Python package has a few dependencies in the code, listed below:

language:python
import time
import sys

Default Variables

language:python
# Define the device name and I2C addresses. These are set in the class definition
# as class variables, making them available without having to create a class instance.
# This allows higher level logic to rapidly create an index of qwiic devices at runtime.

# This is the name of the device
_DEFAULT_NAME = "Qwiic PIR"

# Some devices have  multiple available addresses - this is a list of these addresses.
# NOTE: The first address in this list is considered the default I2C address for the 
# device.
_AVAILABLE_I2C_ADDRESS = [0x12]
Note: This package differs from other SparkFun packages as the register values are declared in the object class.
language:python
# Constructor
device_name = _DEFAULT_NAME
available_addresses = _AVAILABLE_I2C_ADDRESS

# Device ID for all Qwiic PIRs
DEV_ID = 0x72

# Registers
ID = 0x00
FIRMWARE_MINOR = 0x01
FIRMWARE_MAJOR = 0x02
EVENT_STATUS = 0x03
INTERRUPT_CONFIG = 0x04
EVENT_DEBOUNCE_TIME = 0x05
DETECTED_QUEUE_STATUS = 0x07
DETECTED_QUEUE_FRONT = 0x08
DETECTED_QUEUE_BACK = 0x0C
REMOVED_QUEUE_STATUS = 0x10
REMOVED_QUEUE_FRONT = 0x11
REMOVED_QUEUE_BACK = 0x15
I2C_ADDRESS = 0x19

# Status Flags
eventAvailable = 0
objectRemove = 0
objectDetect = 0
rawObjectDetected = 0

# Interrupt Configuration Flags
interruptEnable = 0

# Queue Status Flags
popRequest = 0
isEmpty = 0
isFull = 0

Class

QwiicPIR() or QwiicPIR(address)

This Python package operates as a class object, allowing new instances of that type to be made. An __init__() constructor is used that creates a connection to an I2C device over the I2C bus using the default or specified I2C address.

Note: If the Qwiic PIR's address has been altered from the default (0x12), create the Qwiic PIR object with the new address. For example, if the address jumper is opened create the Qwiic PIR object using this format: QwiicPIR(0x13).
The Constructor

A constructor is a special kind of method used to initialize (assign values to) the data members needed by the object when it is created.

__init__(address=None, i2c_driver=None):

Input: value
The value of the device address. If not defined, the Python package will use the default I2C address (0x12) stored under _AVAILABLE_I2C_ADDRESS variable.
Input: i2c_driver
Loads the specified I2C driver; by default the Qwiic I2C driver is used: qwiic_i2c.getI2CDriver(). Users should use the default I2C driver and leave this field blank.

Functions

A function is an attribute of the class, which defines a method for instances of that class. In simple terms, they are objects for the operations (or methods) of the class. For a complete list of all the available functions, head over to the API Reference page of ReadtheDocs for the Qwiic PIR Py Python Package.

Upgrading the Package

Updating the installed packages has to be done individually for each package (i.e. sub-modules and dependencies won't update automatically and must be updated manually). For the sparkfun-qwiic-pir Python package, use the following command (use pip for Python 2):

For all users (note: the user must have sudo privileges):

language:bash
sudo pip3 install --upgrade sparkfun-qwiic-pir

For the current user:

language:bash
pip3 install --upgrade sparkfun-qwiic-pir