Playing with Raspberry Pi: Door Sensor Fun

Simon Prickett
Jul 9, 2017 · 7 min read
Image for post
Image for post

I recently noticed that Adafruit sells low cost ($3.95) wired door sensors that basically act as a switch — if the magnet from one part of the sensor is placed in close proximity to the other part, the switch circuit closes. As the magnet in the part that you’d normally attach to a door moves away (because the door swings open), the switch circuit opens. If you’ve read my previous Raspberry Pi posts, you’ll know I have a few sets of traffic light LEDs, so I thought I’d see if I could do something with these and a door sensor…

Shopping List

We’ll need a few things to build this out, which are…

Image for post
Image for post
  • AdaFruit Door Sensor

We’ll also need to do a little soldering, so you’re going to want a soldering iron and solder.

Hardware Setup

As delivered, the door sensor comes with bare wires… we could solder these directly to the GPIO pins on the Pi, but for a more re-usable solution it’s better to use the jumper wires…

One end of the jumper wire provides a female connector that goes onto the pin on the Pi. The other end is a male connector — cut that off and strip the insulation back to expose the cable, then solder it to one of the exposed wires on the door sensor. Repeat with a second jumper, and use some electrical tape to cover the solder. You should end up with something that looks like this:

Image for post
Image for post
(Jumper cables with female connector on one end, soldered to the door sensor wires on the other end)

Next, we can attach the traffic lights and door sensor wires to the GPIO pins on the Pi. The traffic lights require 3 pins followed by a ground pin due to the way that their connector works. The door sensor requires one of its wires to be attached to ground, the other to any usable pin. As the door sensor is a simple switch, it doesn’t matter which wire is connected to ground.

The code sample I’ll be using will need you to connect the lights and door sensor to the pins shown below (red = traffic lights, yellow = door sensor).

Image for post
Image for post
(Pins circled in yellow = door switch sensor, pins circled in red = traffic lights, arrow indicates direction of lights) Pinout image by

Having got everything wired together, use the Fun Tak to hold the door sensor down to the desk in front of the Raspberry Pi and the door sensor magnet to the front of the car like so:

Image for post
Image for post

Code & Operating System Setup

We don’t need to do anything particularly special when setting up the Raspberry Pi — I just installed the latest Raspbian Jessie Lite operating system (install guide here) and set up my wifi. Then, we need to install a couple of extra software packages to allow downloading of my sample code from GitHub, and to give Python access to the GPIO pins on the Pi. Enter the following at the command line when logged in as the pi user:

sudo apt-get install python-dev python-rpi.gpio git

Answer “Y” when asked if you want to install additional packages.

The traffic lights operate in the same manner as in my previous posts, each of the red, yellow, green lights maps to a GPIO pin, and can be turned on and off with:

GPIO.setup(<pin>, GPIO.OUT)
GPIO.output(<pin>, <True = on, False = off>)

The door sensor is a little different. As a switch, it operates using one pin (the other wire is connected to ground). It is also an input rather than an output, and we need to set the software pull up resistor to stop the value of the pin floating:

GPIO.setup(DOOR_SENSOR_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)

What we want to do with our code is detect when the door sensor changes state, then update the traffic light LEDs accordingly — if the door is closed (magnet attached to the car is close to the sensor), we want the red light on and green light off. When the door opens (magnet attached to the car moves away from the sensor), we want to turn the green light on and red light off. We won’t be using the yellow light in this demo, other than to initially make sure that it is turned off.

The complete code to do this is as follows:

import RPi.GPIO as GPIO
import time
import sys
import signal
# Set Broadcom mode so we can address GPIO pins by number.
# This is the GPIO pin number we have one of the door sensor
# wires attached to, the other should be attached to a ground pin.DOOR_SENSOR_PIN = 18
# These are the GPIO pin numbers we have the lights attached to
# Initially we don't know if the door sensor is open or closed...
isOpen = None
oldIsOpen = None
# Clean up when the user exits with keyboard interrupt
def cleanupLights(signal, frame):
GPIO.output(RED_LIGHT, False)
GPIO.output(YELLOW_LIGHT, False)
GPIO.output(GREEN_LIGHT, False)
# Set up the door sensor pin.
GPIO.setup(DOOR_SENSOR_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Set up the light pins.
# Make sure all lights are off.
GPIO.output(RED_LIGHT, False)
GPIO.output(YELLOW_LIGHT, False)
GPIO.output(GREEN_LIGHT, False)
# Set the cleanup handler for when user hits Ctrl-C to exit
signal.signal(signal.SIGINT, cleanupLights)
while True:
oldIsOpen = isOpen
isOpen = GPIO.input(DOOR_SENSOR_PIN)
if (isOpen and (isOpen != oldIsOpen)):
print "Space is unoccupied!"
GPIO.output(RED_LIGHT, False)
GPIO.output(GREEN_LIGHT, True)
elif (isOpen != oldIsOpen):
print "Space is occupied!"
GPIO.output(GREEN_LIGHT, False)
GPIO.output(RED_LIGHT, True)

We detect a change in the door sensor state by remembering its state from the previous loop iteration and comparing it to that of the current iteration. The sensor should “close” and “open” as the car (our door here) is moved towards then away from the sensor:

We’re now able to monitor whether or not the car’s “parked” and set the lights to show the space as occupied or available. This isn’t how we’d do a real parking system as it requires the car to have a magnetic tag on it, but it makes for an easier demo of how to work with the door sensor than attaching it to a door :)

Double Down!

The Pi has lots of GPIO ports, enough to support adding a second door sensor and set of traffic lights, so we can monitor two of them…

Adding a second door sensor requires another ground pin, and any other GPIO. We could solder the ground wires for both door sensors together and use three pins not four, but as we have plenty here I didn’t do that.

Adding another traffic light requires a sequence of one ground pin followed by three GPIOs, as the traffic light connector is set up to be attached to that order of pins.

Moving things around from the single switch demo, we can fit two sensors and two lights onto the Pi using the following arrangement (again, yellow highlighting shows where the door sensor pins go, and red is for the traffic lights with the arrows showing the direction in which the lights face):

Image for post
Image for post
(Pins circled in yellow = door sensor, pins circled in red = traffic lights, arrows show direction of lights) Pinout image by

Now we need to essentially duplicate the code we have to run two independent setups, which we’ll name “left” and “right”. So the main code loop would then look like:

# Not shown, additional definitions for LEFT and RIGHT pin numberswhile True: 
leftOldIsOpen = leftIsOpen
rightOldIsOpen = rightIsOpen
if (leftIsOpen and (leftIsOpen != leftOldIsOpen)):
print "Left space is unoccupied!"
GPIO.output(LEFT_RED_LIGHT, False)
elif (leftIsOpen != leftOldIsOpen):
print "Left space is occupied!"
if (rightIsOpen and (rightIsOpen != rightOldIsOpen)):
print "Right space is unoccupied!"
elif (rightIsOpen != rightOldIsOpen):
print "Right space is occupied!"

The end result of this is that we now have two independently monitored parking spaces…

Hopefully you found this a fun use of some simple hardware components. It has been helpful for me to play with the concepts as in my day job at Conectric Networks we’re using wireless door and other sensors to create an energy management platform for commercial buildings.

I’d love to hear what you’re up to with the Raspberry Pi — find me on Twitter or via the responses here. If you enjoyed this article, please hit the ❤ and share it far and wide! If you want to grab the complete code and use it yourself, it’s available on GitHub.

Conectric Networks

Conectric Networks eliminates wasted energy consumption…

Simon Prickett

Written by

Software Professional, San Diego CA.

Conectric Networks

Conectric Networks eliminates wasted energy consumption from heating and air conditioning in hotel buildings using our patented occupancy monitoring and HVAC control system.

Simon Prickett

Written by

Software Professional, San Diego CA.

Conectric Networks

Conectric Networks eliminates wasted energy consumption from heating and air conditioning in hotel buildings using our patented occupancy monitoring and HVAC control system.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store