Page 1 of 1

Simple Relay Circuit

Posted: Wed Mar 15, 2017 12:05 am
by woodystanford
Let's cover a simple relay circuit that can be used with the Pi.

The first thing we do is figure it out conceptually, ok? Now you can only draw 50ma at +3.3V off of a Pi GPIO pin, so what you have to do is 2 or 3-stage it.

What you do is you use a SSR also known as a "solid-state relay". I'm not talking about the big suckers, but there are small DIP chips that you can get for under a dollar at Mouser or Digikey that are true microminiture SSR's. Electrically (from what I understand) they are just photo-triac optoisolators.

So for a smaller mechanical relay you can just two-stage it like this. To three-stage you run your SSR to a small relay that then throws the coil on the larger relay.

The trick is to quickly select the parts you need. How you order off of Mouser is by going to Mouser and search off of the top search box on every page. Its like google (kinda) so it matters what search terms you put in so you can drill-down their catalog of parts quickly. So for SSR, I'd type in "solid state relay dip" and see what comes up. Now before you go ordering what you do is you download the datasheet for each part you are thinking will do the job and check that the specs line up.

Now a DIP package SSR chip WILL be thrown by a Pi GPIO signal, ok (I'd still check to see if the voltage on it is in look for a lower voltage on its internal LED). But check to see that the voltage and the current capability of the SSR will throw the electromechanical relay. When you are confident of this THEN order the parts.

I'll go through the process myself manually and post up on this thread the parts that WILL work together. Then breadboard and do up a custom PCB (presensitized), drill and solder in the parts. You might want to case it as well (radio shack, or just add it to your part order).

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 12:55 am
by woodystanford
OK, here is a schematic of that relay circuit I was talking about.


So get the datasheets for the parts at mouser or digikey and check the matching on the voltage and current OR you can be lazy and just wait for me to put the information up. Don't rob yourself of the self-teaching opportunity though.

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 1:06 am
by woodystanford
Ok you SSR will look something like this. You can also use an optoisolator but remember that the current only flows in one direction on an optoisolator so you have to line up the output phototransistor correctly.


You can also get them in DIP-4 style, or if you want to throw multiple relays you can get larger DIPS with ganged SSR's/optoisolators on them. Internally they are like this:


What you do is you breadboard it up first. Don't beleive everything you read on the Inet (especially me). Then we'll get to putting it on a PCB and cased.

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 1:09 am
by woodystanford
OK, (to prempt any questions) why aren't you using a transistor in switching mode where the SSR is.

The answer is simple: transistors SUCK!

The reason why your grandfather is so crazy is because of transistors. No one really knows how they work (except for a bunch of simultaneous equations). Scary huh? :D This is the 21st Century...WE don't even know what a transistor is, ok? That there may or may not be these demonic little devices in the DIP's we use, only the engineers know, only that we don't.

Just say no to transistors.

(This is just a joke ok? lmao)

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 1:36 am
by klricks
woodystanford wrote:.... Now you can only draw 50ma at +3.3V off of a Pi GPIO pin, so what you have to do is 2 or 3-stage it.......
That is the max combined total of all GPIO's. The max current that should be drawn by any single GPIO is something like 16mA if I remember correctly. Hopefully your circuit will use much less than that.

Note that there are many off the shelf relay boards available for a few $.
Don't forget the diode across the mechanical relay coil.

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 1:39 am
by woodystanford
OK here is a good SSR (checked the datasheet and everything) as it has a led *cough*forward* voltage of around 2V but its got a really strong 1A output.

Also make sure you aren't getting End of Life parts or parts that aren't in stock (doing this will hold up your order).

Here is the part on (little pricy but still within most budgets I think): ... Su8d4Un1WV

Here is the datasheet:

"Now woody, why do you always say datasheets, datasheets, datasheets?" After years of working with stuff, there is always something you NEED to know about a part that always hold you up. So save the datasheets to a specific directory on your PC and use them. None of us know everything on a datasheet, but certain values like VCC, V whatever, current, forward voltage, reverse voltage are all values that you realize you need to check while putting stuff together.

Here is a relay that will throw with 5V

**** IMPORTANT **** Remember this as earlier I said I could find one that would throw at both. Looks like you will need to energize the coil with the full 5V on this part. You can go hunting further if you want. ... iH1nr90%3d

Here is a quick link to its datasheet:

:D :D :D :D :D

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 1:49 am
by woodystanford
Ok rather than retype some stuff, here is a link to how to breadboard and do a DIY PCB.

Image ***not the board we are working on*** ... EM1QxeDgAJ

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 4:42 am
by woodystanford
OK, a synopsis, basically what you do is you breadboard out the preceding circuit after ordering the parts from Mouser or Digikey or wherever (remember to also order your battery holders, any case and discretes like resistors and diodes...a lot of time I forget).

Breadboarding critical in that you basically proofread your circuit. Once its working the process of making the working PCB is a simple matter of physical transfer. In that, from a certain point of view, all you are doing is swapping your wires for traces. So when you have it working you do up the PCB traces on your PC and make your board.

Insert the components all in one operation. A lot of people dilly dally with it, but you have already designed the working circuit. If you mess with it, you'll just muck it up. Solder all in place and cut the leads.

Should look something like this when you are done.


Almost forgot...get the terminal block for your PCB like in the image. Makes your project look so much nicer, I think.

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 5:09 am
by woodystanford
The Point

Where you can develop small function squares of circuits like this, you can develop larger subsystems and systems like robots, drones, even stuff like mock-up nanosats and the like.


You can even fairly easily develop boards like this (LIKE this) with a small store of techniques and skills:


Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 10:19 am
by tenochtitlanuk
SSRs stay in a low resistance on state until the load current/voltage drops to a low, near-zero value. That is why they are used on AC power.
Your circuit shows DC power to the SSR/relay. If the relay coil draws an appreciable current it will stay latched on until the power is removed!
Are you relying on the relay current being lower than the holding current? Or are you intending to specify an AC supply? As it stands you seem to be operating the SSR in a non-standard way...
Am I missing something? ( I've been using SSRs for many years, on Pis but also back in the days of individual transistors that you seem keen to deprecate..)

Re: Simple Relay Circuit

Posted: Wed Mar 15, 2017 8:29 pm
by woodystanford

Not tested by me, just off the Inet. As in use at own risk. You could have a PIC or an Arduino for the lone exciting chip on the far left and control serially I suppose.

Re: Simple Relay Circuit

Posted: Mon Dec 03, 2018 8:28 pm
by rjpearso
Thanks so much super helpful!

Re: Simple Relay Circuit

Posted: Sat Dec 08, 2018 9:35 pm
by Tage
here is how I go about figuring out component values for driving a common relay from a GPIO. I also add some common sense comments..

1. select a suitable relay and determine how much current is required in the relay coil
2. determine how much voltage must be applied to the relay coil to make sure it closes the contact
3. knowing the minimum current in the coil, select a transistor and minimum base current needed

I don't worry about using a solid state relay to drive the relay (why should I?), instead I just connect a npn transistor in series with the relay coil and figure out what base resistor to use to get enough base current so that the transistor turns on with low enough voltage drop, and enough current is flowing in the coil so that the contact will close as intended.

The first thing is to select a relay. If you intend to drive the relay from 5V, you just select a relay with 5Vdc coil. You then end up with about 60 ohm coil resistance, so the coil current is 5V/60ohm = 83mA. It is important to know the actual coil voltage. The 5V may actually be 4.8V, and the transistor will have some voltage drop, say 0.5V, so you are looking at (4.8V-0.5V)=4.3V. You then check the datasheet for the relay and make sure the 4.3V is enough to generate enough pull to close the contact. For example you may find that the relay needs 75% of coil voltage to close the contact. For 5V that is 0.75*5V = 3.75V. That is the minimum voltage you must apply to the coil to be sure the relay closes. In our case, it seems we have a margin.

Note that once the relay has closed the contact, you can actually drop the current in the coil to a quite low value (perhaps 10mA), and the contact may still remain closed. This can be used to reduce current draw by the relay, but for now let us forget about this feature..

Once you know the coil resistance and the minimum pull-in current you can figure out how much current the transistor must provide while also keeping its voltage drop (collector-emitter voltage) low enough so the coil current is high enough.
There is no simpler method than to look at the transistor data sheet to figure out how much base current is needed.
If we assume the minimum voltage on 5V is 4.8V, and the relay needs a minimum of 3.75V to close, we can allow 1.05V voltage drop across the driver transistor with 3.75V/60ohm = 63mA collector current.
If we select a 2N2222 transistor and look at the datasheet we see that the voltage drop from base to emitter is about 1V when base current is about 1mA and collector current is about 100mA. so we can calculate the value of the base resistor if we know the voltage of the GPIO pin at 1mA load. If we assume that the GPIO pin delivers 3.3V and the base-emitter voltage is 1V, the resistor value needed is (3.3V-1V)/1mA = 2.3k.

One important thing to understand when selecting a relay is that you really should stay away from the type of relay shown in the postings if you intend to connect the relay to 230Vac load! It is really important to understand that these relays are not intended or even certified to be used without the low voltage circuit connected to protective ground. You probably are breaking a law and subjecting users to an electrical hazard. It is much safer to use relays that have much larger creepage and clearance distances between coild and contacts. I would use relays that have datasheets that specify 8mm or 10mm distance between coil and contacts, if I think that someone would connect 230Vac loads! When making a pcb board with relays, it is important to be aware of the spacing requirements. Most of the relay boards I have seen for sale to hobbyists are totally unsafe for mains voltage loads.

Re: Simple Relay Circuit

Posted: Sun Dec 09, 2018 2:02 pm
by danjperron
Hum I'm ok with SSR, but not with your schematic showing a SSR using triac optocoupler!

There is a lot of type of SSR. Mostly two groups , the AC SSR and the DC SSR!

You create schematic with AC SSR which is completely wrong!

AC SSR are triac! Once a triac is activated it will stay ON until voltage go below a threshold. In AC there is no problem but not in DC.

So please next time use the correct SSR design in your schematic. Not the AC SSR like you did! This confuse me on first look!

If you think that transistor is complicated Well Triac are even worst! You could think that they are two transistors in head to tail mode. ... -schematic