Thursday, August 25, 2016

Raspberry Pi GPIO Part 2: Relays

The relays arrived.  They are neat.


These are uxcell HK19F relays, which came in a pack of 10x for $6.89 and free shipping from Amazon.  Just make sure you pick a seller that is in your local country unless you want to wait a month because there are so many Chinese sellers on amazon now.  They're DPDT which means Dual Pole Dual Terminal, so there are two individually switched inputs that each go to two different outputs.  One is normally open, and one is normally closed.  If you saw my previous blog post about the hotwheels radar gun, I need this to simulate the trigger which bridges one pair of wires when the trigger is pulled and bridges the other pair (to show top speed) when the trigger is released.



Here I will give you poorly constructed explanation of what I perceive to be going on.  I've also traced the diagram since it's really hard to see.
When the coil is not engergized Input 1 is bridged to Output 1A and Input 2 is bridged to Output 2A
When the coil is energized Input 1 is bridged to Output 1B and Input 2 is bridged to output 2B
It's possible that I have normally open/normally closed swapped because I did not verify, but am assuming that the image shows the relay in the "unpowered" state.

Increasing the 3.3v GPIO output to run a 5V relay coil

So unfortunately the output of the GPIO pins is 3.3v and very low current, so we're going to need to amplify it somehow.  We're going to use a transistor to do this.  You've probably heard about how transistors revolutionized technology and made computers smaller than buildings and radios smaller than refrigerators by replacing vacuum tubes.  Me too, but I had no idea how they worked.

This is a transistor.  It's main function is to act like a switch that's toggled by a very low amount of power (sort of like how a relay works).  However a transistor and be activated by a lower amount of power than a relay, but cannot handle as much current as a relay.  In some cases you can use a MOSFET which is sort of like a beefy transistor with a heatsink which can kind of do both jobs reasonably well.

The Emitter pin (bottom) goes to ground.
The Collector pin (top) is what your circuit should ground into.
The Base pin (middle) is what you give a tiny bit of power to and it opens the flow from Collector to Emitter.  Think of a transistor as a valve and by applying power to the Base pin you're opening the flow of electrons from Collector to Emitter.

These rules Assume you are using an NPN transistor.  A PNP transistor is totally different, it does something like positive power supply to Collector, Emitter to your circuit, and power can flow when base is grounded.

Ok, so +5V supply red wire, goes to one side of the coil on the relay.  The other contact on the relay coil goes to the collector pin on the transistor.  The emitter pin on the transistor goes to ground via the black wire.  The grey wire is a 3.3v GPIO output from the pi going to the base pin on the transistor.  Use a resistor where gpio enters the transistor, not shown.  You dont want to pull more than 16mA from the pi or you can damage it. 330 ohm worked for me and dropped draw from 40mA to 7mA When a small 3.3v current is applied to the base pin, the transistor allows the 5 volts to pass through the coil on the relay which toggles which outputs are connected to the inputs.

Here's a closer look:

The Brown wire is going from a 3.3v output (rail, not GPIO) on the pi to input2 I talked about above (remember the relay is flipped over now so input2 is on the left now).  I have output2B going to the positive lead of an LED, and the other lead going to ground.  The LED is on the normally open output but the reason it's lit up is because the program is already running in this picture.  I ran a simple python script that toggles the GPIO output on and off every second (which I covered in the GPIO part 1 post).  If I connected a second LED to output2A then the LEDs would alternate as the relay is cycled and would never be on at the same time.  There's just really one thing that's missing here...


This is a diode.  A diode only lets power flow one way.  Apparently when driving a relay with a transistor you're supposed to put a diode across the coil IN REVERSE.  Basically the positive lead on the diode needs to go to the negative side of the relay coil and the negative lead on the diode needs to go to the positive side of the relay coil.  The reason for this is that you are creating a magnetic field in the coil to open it, and when you stop powering the field that coil can output power as the magnetic field collapses and damage your transistor (sort of like how a motor spins when you power it but then outputs some power when spun).  Allegedly this is the reason why, and I might not even be getting that right, so you might want to look up the reason yourself.  Anyways I recommend you put a diode in backwards because every example I've seen says to.  It will be obvious if you have it "wrong" (not backwards, and yes this is super counter-intuitive) because your relay will never cycle and your transistor might burn out (as there is no resistance in the diode so it just lets you bypass the coil and dump all that power from the +5v rail to the ground straight through your diode (which will get hot from that much current) when you introduce a current on the gate pin).

So there you go.  Relays are kind of fun because you can hear them cycle as they're mechanical.  As my relay turned on and off it sounded like the turn signal of a car (which coincidentally operate off of... relays!).

So we're taking about 15mA at 3.3v from GPIO and using a transistor to enable a 5 volt circuit.
That 5 volt circuit opens and closes circuits on the relay.
That relay can handle 1 Amp at 125VAC or 2 Amps at 30VDC.

Go big or go home

Now I don't believe there is a reason you can't "stack" relays, and this is probably actually a thing and there is probably actually a name for it.  So in theory you could use this not very big relay to toggle a 12v circuit that powers the coil of a huge ass relay, effectively toggling hundreds of amps with your wee little GPIO output.

No comments:

Post a Comment