Blog

Raspberry Pi to open Garage Door

Written by on 10th November 2014 in raspberry pi with Leave a comment

I came across a neat project that someone did where they wired up their garage door to be opened by their Raspberry Pi.  I thought I would give it a try and I had some issues.  I made this post in case someone else runs into the same problems.

I didn’t install the webserver as shown in their example, because my raspberry pi wasn’t going to be web-accessible.  Instead I just wrote a bash script to trigger the relay (which opens/closes the door).  In the example I linked to above, the author uses GPIO7 as the pin that is wired to the relay.  I was having trouble getting my relay to fire (I purchased the same exact Sunfounder 5V relay in their example) and I couldn’t figure out why.

In their example, they use the following command to trigger the relay that they wired in:
/usr/local/bin/gpio write 7 1
/usr/local/bin/gpio mode 7 out

And similar statements. No matter what I tried, the relay would not fire. Note that the relay produces an audible *click* when you trigger it, and a little LED comes on. So I did some debugging and discovered that the pinouts of Raspberry Pi’s are not all the same! I had to find the specific revision to make sure the wiring was the same for mine. Then if I read all the pins status I saw:
gpio readall
+----------+------+--------+------+-------+
| wiringPi | GPIO | Name | Mode | Value |
+----------+------+--------+------+-------+
| 0 | 17 | GPIO 0 | IN | Low |
| 1 | 18 | GPIO 1 | IN | Low |
| 2 | 27 | GPIO 2 | IN | Low |
| 3 | 22 | GPIO 3 | IN | Low |
| 4 | 23 | GPIO 4 | IN | Low |
| 5 | 24 | GPIO 5 | IN | Low |
| 6 | 25 | GPIO 6 | IN | Low |
| 7 | 4 | GPIO 7 | IN | High |
| 8 | 2 | SDA | ALT0 | High |
| 9 | 3 | SCL | ALT0 | High |
| 10 | 8 | CE0 | ALT0 | High |
| 11 | 7 | CE1 | ALT0 | High |
| 12 | 10 | MOSI | ALT0 | Low |
| 13 | 9 | MISO | ALT0 | Low |
| 14 | 11 | SCLK | ALT0 | Low |
| 15 | 14 | TxD | ALT0 | High |
| 16 | 15 | RxD | ALT0 | High |
| 17 | 28 | GPIO 8 | IN | Low |
| 18 | 29 | GPIO 9 | IN | Low |
| 19 | 30 | GPIO10 | IN | Low |
| 20 | 31 | GPIO11 | IN | Low |
+----------+------+--------+------+-------+

What I had as gpio7 was named as gpio4 to my raspberry pi! Yet I still couldn’t get it to trigger. So I found code from another project and realized I was triggering the GPIO in a different manner. So then I tried:
echo "setup pin gpio4"
echo "18" > /sys/class/gpio/export
echo "set it to output"
echo "out" > /sys/class/gpio/gpio4/direction
sleep 5

echo “turn on / press button”
echo “0″ > /sys/class/gpio/gpio4/value
sleep 5

echo “turn off again”
echo “1″ > /sys/class/gpio/gpio4/value
And that worked!

I am sure there is a cleaner way to do this, I hope this helps someone else.

Posted Under:
Post Written by

I am the owner of Russell Draper & Associates, this is my personal blog where I will post whatever I feel like! I generally have comments disabled, due to spam-bots. If you wish to leave a comment please contact me via RDAIT.

Leave a Reply

You must be logged in to post a comment.