In this lesson, you will learn how to use the digital inputs on the GPIO connector with a door sensor and a PIR motion detector.

In this lesson, we will concentrate on sensing movement and activation of the door switch. In Lesson 13 we will build on this security sensing to have the Pi use a digital output to control the power to an electrical appliance when movement is detected.

We are going to connect both sensors to the Raspberry Pi at the same time. Neither sensor requires any extra components. This tutorial works with all versions of Raspberry Pi (rev 1, 2, A, B, B+ and Zero) except the compute module which has no headers. 

40-Pin (A, B, B+ and Zero) Cobbler Plus Schematic

20-Pin (Rev 1 and Rev2) Cobbler Schematic

The PIR sensor comes with a socket and lead. Make sure that the socket is the right way around (use the picture below) and that the red lead goes to 5V, the black to GND and the yellow to 18 on the Cobbler.

Although the PIR sensor requires a 5V supply, its output is a Pi-friendly 3.3V, so it can be connected directly to a GPIO input.

The Door Switch, uses what is called a reed switch. These are two contacts inside a glass tube, that is then encased in plastic. When a magnet (the other white block) is placed near the reed switch, the contacts are drawn together and the switch closes. Since this is just a switch, the leads can be connected either way around.

We will use the Pi's ability to create an internal pull-up resistor on the reed-switch pin, so we don't need an external pull-up resistor.

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. 

$ sudo pip3 install adafruit-blinka

The Code

# 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!")


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.

Let's put this file right in your home directory for simplicity. The wget command makes things easy as this can be run from an internet connected Raspberry Pi.

    $ wget

Running the Code

To start with, place the magnet next to the switch and cover the PIR sensor with something.

Confirm that the Pi connected to the Cobbler (or Cobbler Plus) and run the script.

The following command will start the program:

    $ sudo python3 ./ 

..and you should see some trace appear in the terminal, when you move the magnet, or take the cover off the PIR sensor.

A good exercise might be to place your kids in-front of the PIR sensor and see how long they can keep still!

This guide was first published on Feb 13, 2013. It was last updated on Feb 13, 2013.