Sunday, October 6, 2013

Garage Door Part 2 - Monitor Door (GPIO Input)

Project Garage Door
Now that I can trigger our garage door, I needed a way to determine if the garage door is open or closed.  I purchased a Magnetic Reed Switch (commonly used for windows Theft Deterrence) to detect this change.
Magnetic Reed Switch
The way a reed switch works is as the magnetic block nears the switch block, the sensitive switch is pulled closed, and the circuit will be completed.

This particular magnetic switch is really nice, as it includes terminals for both Normally Closed (NC) and Normally Opened (NO), depending on your needs.  Most cheaper reed switches only include a Normally Opened option.  For my setup I choose to use the Normally Opened (NO), so the triggered switch would close the circuit loop.  Either configuration could be used, and would just need to be adjusted in the code logic.  This magnetic switch also includes the option of being taped (included) or screwed/nailed to a surface.

I ran a couple of tests with the switch and Raspberry Pi, to make sure the switch would work as expected, and the code would detect the changes.  Once I was satisfied with the results, it was time to mount the components to their final resting places.

I tried finding a position on the wall that I could mount the magnetic switch, but no position appeared to get close enough to the door, but yet far enough away to not get stuck.  I finally settled on attaching the switch block to the garage floor and the magnetic block to the bottom of the garage door, within the groove.  I found that the door does not actually touch the ground as there is weather stripping in the way, which gave excellent clearance for the switch.

Magnetic switch - switch block
Magnetic switch - magnetic block












Then I ran a pair of 20 gauge "bell" wires from the magnetic switch, up the wall, across the ceiling and down to my Raspberry Pi.  I left a bit of slack on each end, just in case I need to make adjustments in the future.  To keep the wires from falling, I used a simple stapler, as the wires were very light weight.  The Raspberry Pi, breadboard, and USB Hub are all bundled within the plastic blue box, to keep the dust (and cat) away.
Wires running to Raspberry Pi

Wires running along the ceiling












To finish the circuit I then needed to connect the switch to power, ground and GPIO pins.  The following schematic is the recommended circuits for a simple switch input circuit, and includes Pull Up and Pull Down resistor configurations.  The most commonly used layout is the Pull Up resistor so this is what I will use for my circuit (either could be used).  (Please read Pull-up Resistor for more information, if curious)
GPIO Input Circuits with both Pull Up (top) and Pull Down (bottom) configurations
 
My "Pull Up" input circuit that matches the above schematic

The whole bundled mess, found within the protective plastic box:


Raspberry Pi, breadboard and USB Hub within protective plastic box
I use the following Python code, running on the Raspberry Pi to test and report if the switch is opened or closed.  This code is the foundation for a much larger bit of code used for monitoring, opening and closing the garage door.
#!/usr/bin/env python

import sys
import time

import RPi.GPIO as GPIO

pin = 25

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)

try:
  while True:
    input_value = GPIO.input(pin)
    if input_value:
      sys.stdout.write('-')
    else:
      sys.stdout.write('.')
    sys.stdout.flush()
    time.sleep(.01)
finally:
  GPIO.cleanup()

Next, we will look at a simple web interface to control the garage door: Garage Door Part 3 - Web Interface

2 comments:

Unknown said...

did this code work of yours?
I'm busy with the same Idea but is not good with programming and to get the status on my web page.
please let me know. you help wil mean alot to me.

thanks

Anonymous said...

Did you ever get around to "Garage Door Part 3"?