Sunday, September 8, 2013

Driving a Relay using GPIO

The next Raspberry Pi project I am working on is controlling my garage door.  To control the garage door I will need to use a relay.  Unfortunately, I used my one and only, easy to use, and very compact, "Relay Module" with my sprinkler project.  I could order another, but that would take time, and I wanted to get something working over the weekend.

Luckily, I had a pack of 5V relays in my electronics component collection.  I had ordered a 10 pack for $6 off of Amazon some time ago.  (do a quick search for "5V Relay", there are numerous options)
QIANJI JQC-3F(T73) Relay

Controlling a relay from a Raspberry Pi is not as easy as simply connecting it to a GPIO port.  First, the GPIO port is only 3.3V and most small relays require 5V.  Luckily the Raspberry Pi does have a 5V rail we can use, but we need a way to switch that on and off.  That is where the transistor comes in.  Our GPIO port will turn the transistor on and off, which will in turn turn the 5V lead to the relay on and off.

In theory this GPIO -> Transistor -> Relay are all that is needed, but of course we need a bit of protection to not fry the components (or the Pi).  This involves a few resistors and a diode.  The resistors will limit the current, and the diode will wrap the relay to dissipate the back current (a relay is just a coil with a magnet, and coils store energy).  This mess is why the compact pre-built Relay Modules are so very nice!

I found an excellent article, by Kevin Sangeelee, that provides a great explanation and a sample schematic for driving a simple relay.  Kevin describes the use of the diode as:  "The diode in the circuit is there to conduct the current generated by the de-energising coil back across the coil (e.g. when switched off), allowing the power to dissipate more gradually, avoiding a voltage spike." (source)
Schematic for a relay via GPIO on the Raspberry Pi (source)

I did not have all of the exact components listed in Kevin's schematic, but I did have several close components that I traded with, from my electronic component box.  (If you don't have a box for all these various small components, you really should, as you never know when you will need one).  For the transistor I used a 2N2222 A338.  For the relay I used the 5V QIANJI JQC-3F(T73) relay from Amazon.  For the diode I used a generic, nondescript, one I also had in the component box.  With my setup, the 1K resistor was too high to drive my transistor, so I reduced it to a 100 Ohm resistor, and this seemed to work fine.  I also connected up a resistor and LED on the load side, to verify that the relay was functioning, as if the noisy relay clicking wasn't enough.

I then put together a little bit of Python code to produced this fun clicking light show:
video

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)

try:
    while True:
        print "On"
        GPIO.output(25, GPIO.HIGH)
        time.sleep(.1)
        print "Off"
        GPIO.output(25, GPIO.LOW)
        time.sleep(.1)
finally:
    print "Cleaning up..."
    GPIO.cleanup()

The next step is to connect it to the garage door opener, and then get some open/close sensors so I can tell when the door is open or closed.  Eventually, I am going to write up a proximity application for my cell phone to automatically open the garage door on my way home from work.

1 comment:

reginald surict said...

Nice post. You should also navigate over here to learn more about cell phone tracking app.