The simplest way of driving a relay is use an Avalanche rated N-Channel logic level FET with enough current capability to operate the relay coil. Hook up is simple: Connect one side of the relay to the + supply connect the other side of the relay to the drain of the mosfet, connect the source of the mosfet to ground and the gate either directly to the micro pin or put a resistor in series with the gate preferably less then 50 ohms. When they tell you that you have to use a flyback diode or whatever ignore it, the fet is designed to operate without one. The flyback when the fet is in the circuit will be give or take a little about -2 Volts. You can find the UIS (Unclamped Inductive Spike) capability in the data sheet, and if it more then the solenoid load it is ok. Check this data sheet: https://www.vishay.com/docs/91017/91017.pdf Remember when an inductor is switched off the polarity reverses and the voltage will rise until quenched. Use any led you like do not forget the current limiting resistor. You can connect it directly to the MOSFET if you want.
Good Luck & Have Fun!
Gil
Surface mount shorthand! There are decoder sites but it is pretty irrelevant.
Slythy:
Also for the led will any surface mount red led work?
Pretty much!
Slythy:
I think D1 may be a 1N4007
Perfect! Or a 1N4001, EM404 etc. etc. Even a 1N914 will be fine.
Slythy:
Q1 may be a BF517? Could be way wrong
Don't know. Don't care. If it is a general-purpose NPN transistor with as many volts rating at least twice your supply voltage and current rating greater than 300 mA, (5 V relay about 70 mA) it will be just perfect.
A simple transistor and 2 resistors, diode, relay is all that is needed to drive a relay from a GPIO pin. It has been done this way since the 1960's and I'll bet there are countless numbers relays sitting critical military and medical gear that have no need for an optocoupler in a relay driver circuit.
(Ok, I'll admit... if you are an online electronics module SELLER and you are going to carry a module that works in VARIED situations... you would use that somewhat convoluted "flexible" design shown above so you avoid people saying "your board doesn't work". In a specific solution, however, just use the parts that you need.... meaning the "design" that meets your specific needs.)
Paul__B:
Don't know. Don't care. If it is a general-purpose NPN transistor with as many volts rating at least twice your supply voltage and current rating greater than 300 mA, (5 V relay about 70 mA) it will be just perfect.
Thanks so much for your help!
I was on my phone and it wasnt uploading the image so I just tried the code but it looks like it didnt work.
Thanks for going into the explanation of the Transistor, I honestly don't know much about how to utilize of spec them.
If you are driving the optocoupler version from 3.3 V logic, the combined voltage drop of the optocoupler (1.2 V) and a green LED (2.1 V) equals the logic voltage itself, so you need to connect the "Vcc" feeding the optocoupler LED from your 5 V supply, not 3.3 V in order to make it work.
Such a resistor is only necessary for use with a FET, not a BJT.