I promised previously to go through how I setup control of the Garage door with the Raspberry Pi and to interface with Siri, well, first let’s go through how to wire it up to the Pi and control it with a simple Python script.
What you’ll need:
- Raspberry Pi (I’m using version 1 model B+)
- 2 Channel Relay
- Some small gauge wire for connecting the Garage Door motor with the relay (I used leftover wire from the garage door install)
- USB Wifi dongle for your Pi, I like the TP Link WN725N
- Male to Male Jumper wire, like you can find in a set (see here or Amazon)
I started by getting the Pi ready with a fresh install of Raspbian:
- Go to https://www.raspberrypi.org/downloads/raspbian/ and download the latest Raspbian image, which will give you a zip file
- You can find a link to installation instructions for Windows/Mac/Linux on the same page.
Once I had an image, I made sure I had Wifi working and I could SSH onto the Pi for remote configuration and management. While Node JS comes pre-installed, it’s an older version. If you would like to update it for your distro (this is optional), on the Raspberry Pi 2 and 3 you can do the following from a terminal:
$ curl -sL https://deb.nodesource.com/setup_4.x | sudo bash - $ sudo apt-get install nodejs
This will install a more recent version of Node for ARM processors. If you have an older Raspberry with the ARMv6 processor (RPi 1 or Zero), you can install an updated version of Node:
$ wget http://node-arm.herokuapp.com/node_latest_armhf.deb $ sudo dpkg -i node_latest_armhf.deb $ node -v && npm -v
- That last step just confirms the version of Node and NPM installed. If you run into issues, or you have an earlier version of Raspbian (eg Wheezy), you can try the archive package. Additional instructions here.
Wiring up the Pi:
Now that you have a Pi with a working OS, Wifi, and Node installed let’s go ahead and look at wiring things up. Here is how the devices look, wired up with the automatic garage door opener:
Let’s start with an overview of how this will work:
- We will connect wires from some specific pins on the Raspberry Pi to the relay; This will create a circuit. When we draw power LOW on the pin the relay is connected, it will switch the relay. There’s an LED on the relay that indicates when the switch is active.
- When we trigger or switch the relay on, it in turn will send an electrical signal to the garage door opener. If we have the relay connected to the door properly, it will simulate pressing the button on the wall.
- While at this point we can’t tell whether the door is open or closed (except by looking), it will automatically move in the appropriate direction because the motor knows whether it’s at its limit and closed or open, and go in the other direction.
To wire up the Pi to the relay, we’ll take a few of the male-to-male jumper wires, and connect one (Black) to the ground (GND), one (Green) to the input pin (IN2), and finally the last (Yellow wire) to the power (VCC), as seen above on the left.
Then, we’ll connect the other ends to the Pi. This is a good time to pause and take a moment to understand how these pins work, and there’s a great explanation here.
We’ll now connect the black wire to the ground on the Pi, the green wire to a pin we’ll control, and the yellow wire to power from the Pi. If you look at the picture on the right, you’ll see the black wire is plugged into the ground 3 pins in from the left, the green pin connected to pin 18 according to GPIO numbering, and the yellow wire connected to the 5v power in the first pin on the top row:
Now that we have the Pi wired to a relay, we can conduct some simple unit tests to determine if we can trigger the relay. You can use a simple python script I wrote and put on Github (just download or copy/paste garage.py), or open an editor, and paste the following, giving it a name like testgarage.py:
import RPi.GPIO as GPIO ## Import GPIO library from time import sleep GaragePin = 18 GPIO.setmode(GPIO.BCM) ## Use BCM vs BOARD pin numbering try: GPIO.setup(GaragePin, GPIO.OUT) ## Setup GPIO Pin 18 to OUT GPIO.output(GaragePin,0) ## Pull the pin low sleep(2) ## Allow the instruction to pass the relay to the motor GPIO.output(GaragePin,1) ## Reset the state to high except: print "exception occurred!" finally: GPIO.cleanup()
From a command line, you can then execute:
$ python testgarage.py
Now, you should hear an audible “click” from the relay switch and see the red led come on briefly before we reset it.
If this worked for you, then you are basically ready to connect it to the garage door motor!
In Part Two, we’ll complete the wiring into the garage door motor, and start controlling the door from our Node JS project.