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.setup(pin, GPIO.IN)

  while True:
    input_value = GPIO.input(pin)
    if input_value:

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

Pull-up Resistor

What is a Pull-up Resistor?

Imagine the following simple circuit:

Simple "floating" switch circuit
When the switch is closed there is a complete circuit from GPIO pin to ground.  A reading of the GPIO pin will read "low".

What happens when the switch is opened, though?  This leaves the GPIO pin in a "floating" state which is unable to determine a "high" or "low" state.  If you now read the GPIO pin, it will randomly report high and low states.

To force the GPIO pin to a "high" state, when the switch is open, we can add a very high Ohm resistor to the circuit and to the voltage source.
Simple switch circuit with Pull Up resistor

As the resistor is so high, when the switch is closed the resistor does not directly affect the rest of the circuit.  But when the switch is opened, the high resistance is better than no path and will "Pull Up" the circuit path to the voltage source, so the GPIO pin will now detect a "high" state.

Depending on if you want the opened switch circuit to be pulled up to the voltage source, or pulled down to the ground, different circuits can be built:
Input Circuits with "Pull Up" (top) and "Pull Down" (bottom) configurations

"Pull-up resistors are used in electronic logic circuits to ensure that inputs to logic systems settle at expected logic levels if external devices are disconnected or high-impedance is introduced. When the switch is open the voltage of the gate input is pulled up to the level of Vin. When the switch is closed, the input voltage at the gate goes to ground.  A pull-up resistor weakly "pulls" the voltage of the wire it is connected to towards its voltage source level when the other components on the line are inactive. A pull-down resistor works in the same way but is connected to ground." (source)