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.

The program uses the Rpi.GPIO library. See Lesson 4.

import time
import RPi.GPIO as io

pir_pin = 18
door_pin = 23

io.setup(pir_pin, io.IN)         # activate input
io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)  # activate input with PullUp

while True:
    if io.input(pir_pin):
        print("PIR ALARM!")
    if io.input(door_pin):
        print("DOOR ALARM!")

The program sets the pir_pin to be just a plain old input. This is because the PIR sensor has a digital output of either 3.3V or 0V. By contrast, the door_pin, since it is a switch does not generate a voltage for a digital input. So, that input pin uses the extra argument (pull_up_down=io.PUD_UP). This activates an internal resistor that makes the input HIGH (pulled-up) unless something stronger (like a switch connecting it to GND) pulls it LOW.

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.

Last updated on May 04, 2015 Published on Feb 13, 2013