Software

The Setup

The program for this project just loops round printing a message every time motion is detected, or the magnet is moved away from the door.

We are using the CircuitPython Libraries that are part of adafruit-blinka. See CircuitPython Libraries on Raspberry Pi to get a fresh Raspberry Pi setup.

If you have a running Raspberry Pi with an up to date copy of Raspbian you can simply run the following command to install adafruit-blinka. 

Download: file
$ sudo pip3 install adafruit-blinka

The Code

import time
import board
import digitalio

# set up motion sensor
pir_sensor = digitalio.DigitalInOut(board.D18)
pir_sensor.direction = digitalio.Direction.INPUT

# set up door sensor
door_sensor = digitalio.DigitalInOut(board.D23)
door_sensor.direction = digitalio.Direction.INPUT

while True:

    if pir_sensor.value:
        print("PIR ALARM!")

    if door_sensor.value:
        print("DOOR ALARM!")

    time.sleep(0.5)

The program sets the pir_sensor and door_sensor to be plain old inputs.

The loop then reads each of the inputs in turn and prints a message appropriately. Remember that the door switch warning will be activated when the magnet is removed from the sensor rather than the other way around.

This guide was first published on Feb 13, 2013. It was last updated on Feb 13, 2013. This page (Software) was last updated on Jul 18, 2019.