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.
$ sudo pip3 install adafruit-blinka
# SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries # # SPDX-License-Identifier: MIT 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 door_sensor.pull = digitalio.Pull.UP 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.