# Switching relay with NPN BJT

I am trying to switch a SPST-NO relay that is rated to handle up to 227VAC. The coil is powered by 5V, has a 100 Ohm coil resistance, and its contacts are rated for 16A max.

I would be using the GPIO pin to power the base of the transistor, which is 3.3v and the 5Vout to power the relay coil.

To make your life easier I will list the relevant data from the 2n3904's datasheet:

Ic(max) = 200mA Vce(sat) = .2v with 50mA Ic & 5mA Ib Vbe(sat) = .65-.95v with 50mA Ic & 5mA Ib hFE = 60 with 50mA Ic & 1v Vce

Relay can be found here: http://www.newark.com/panasonic-electric-works/adj23005/relay-spst-no-277vac-16a/dp/12N3389

I have the base resistor at 520 Ohms: R = V / I = 3.3v - .7v / 5mA = 2.6v / 5mA = 520 Ohms

The Ic is found by multiplying the current gain(hFE) by Ib. The test case for Vbe(sat) shows Ib should be 5mA:

Ic = hFE * Ib = 60 * 5mA = 300mA

The 2n3904 can only handle 200mA max. I need Ic to be at 50mA according to the datasheet if I want to saturate the base. I would need a resistor to retard the current a whole 250mA.

Am I missing anything? Does this all look good?? Meaning will it work? Please explain anything I am missing conceptually or anything at all I would very much appreciate it!

EDIT: I have researched this topic as best as I could and these are just concepts and question I need help wrapping my head around.Ultimately I would like to be able to do this with out the help of this forum/group and move to mosfets next.

• please, please, please put a (reverse) diode across the coil to prevent damage to the transistor by back e.m.f. – JIm Dearden Jan 6 '17 at 15:00
• ...and use a separate 5V to power the relay or expect constant resets when it pulls the supply low. – JIm Dearden Jan 6 '17 at 15:02
• oops forgot about the flyback diode, sorry Jim! I will use a separate power 5v supply now. Thank you! How does the circuit look? Are the calculations correct? Is 520 Ohms correct for the base resistor? Are all my conceptual understandings correct? Have I missed anything. Please enlighten me so I dont have to ask again. Thank you! – Aguevara Jan 6 '17 at 15:39
• So much text for so simple question!!!! By the way, diode is not for BEMF. – Gregory Kornblum Jan 6 '17 at 20:20
• @GregoryKornblum We often confuse the meanings of motion back EMF and flyback inductive effects, where some engineers restrict BEMF to only motion generated EMF, but in fact both fixed inductive and motion induced kick back are Back EMF. Faraday's law is a single equation describing two different phenomena: the motional EMF generated by a magnetic force on a moving wire (see Lorentz force), and the transformer EMF generated by an electric force due to a changing magnetic field (due to the Maxwell–Faraday equation).**proved by Maxwell. – Tony Stewart Sunnyskyguy EE75 Jan 7 '17 at 23:47

I am trying to switch a SPST-NO relay that is rated to handle up to 227VAC. The coil is powered by 5V, has a 100 Ohm coil resistance, and its contacts are rated for 16A max.

I would have wanted to also consider the use of a mains-powered relay and the use of a MOC30x3 device (MOC3063 if you want zero-crossing behavior or a MOC3023, if not.) These guarantee operation when provided with at least $5\:\textrm{mA}$.

simulate this circuit – Schematic created using CircuitLab

This provides opto-isolation, requires a driving current that is routinely available in typical I/O pins from a microcontroller, and powers the relay directly from the mains supply instead of your DC supply rail. And since the relay is AC mains powered and isolated from your DC rail, a simple connection without snubbers works well enough. Just to add still one more useful point, it can be driven directly from your $3.3\:\textrm{V}$ I/O pin and there's no particular need for a separate $5\:\textrm{V}$ rail.

An OMRON G2R provides some mains powered options and might be such a relay choice.

However, if you must use a separate $5\:\textrm{V}$ rail and a compatible relay, then you should operate the switching BJT in saturated mode (active, saturated.)

An early thing to consider is the size of the BJT. In this case, you need a collector current of $I_C=\frac{5\:\textrm{V}}{100\:\Omega}=50\:\textrm{mA}$. A saturated BJT will have a $V_{CE}\approx 200\:\textrm{mV}$. So that means $200\:\textrm{mV}\cdot 50\:\textrm{mA}\approx 10\:\textrm{mW}$. But there's more. The base current isn't accounted for, yet. This will be roughly 10% of the collector current (over-driving the BJT is how you get it into saturation), or about $5\:\textrm{mA}$. This will probably require about $V_{BE}\approx 700\:\textrm{mV}$. So, another $700\:\textrm{mV}\cdot 5\:\textrm{mA}\approx 4\:\textrm{mW}$, for a total of $14\:\textrm{mW}$. This is easily within the capability of almost any package, so a small signal BJT like the one you picked out will work just fine.

Note here, by now, that you don't need a base current more than about $5\:\textrm{mA}$. So, your base resistor needs to be only about $\frac{3.3\:\textrm{V}-0.7\:\textrm{V}}{5\:\textrm{mA}}= 520\:\Omega$. Because this is based on an over-driven 10% figure and because you can rely on the fact that small signal BJTs will saturate well before reaching that figure, it's just fine to relax the base resistor to the next standard value above that figure, or $560\:\Omega$. (Probably would work fine with a $1\:\textrm{k}\Omega$, but whose counting?)

Tony's suggested circuit with the diode is just fine, by the way, and you should include something like that included diode in order to allow the relay coil a method to de-energize itself when turned off. The time required to de-energize will depend upon the voltage developed across the relay coil, however. And a simple diode presents only a small voltage across the coil, so the time will be longer than it might otherwise be. If time matters to you for reasons you didn't mention, you could consider the idea of including a series zener, as well, in order to jack up the de-energizing voltage and thereby reduce the required time for that phase of operation.

Note that both the AC-powered relay and also the DC-powered option require about $5\:\textrm{mA}$ from your I/O pin. The AC-powered method is just an alternative approach to consider and it may expand your options (if not this time then perhaps another time and another place.)

• for OP's info only, the high voltage isolation of the Relay vs the MOC3xxx devices are similar, 1kVrms Impulse 6kV Opto vs 10kV relay, AC relays tend to be much less efficient than DC – Tony Stewart Sunnyskyguy EE75 Jan 7 '17 at 22:49
• @TonyStewart.EEsince'75 Yes. The AC relays will burn about $2\:\textrm{W}$ and the OP's DC relay is about $250\:\textrm{mW}$, by comparison. However, not needing a separate $5\:\textrm{V}$ power supply may be worth the difference. The MOC30x3 devices are not particularly boutique parts, so readily, widely, and cheaply available. – jonk Jan 7 '17 at 23:20
• @jonk would you be willing to chat and answer some questions and concepts I still lack understanding on/with? – Aguevara Jan 24 '17 at 17:21
• @Aguevara Yes, I suppose so. – jonk Jan 24 '17 at 17:27
• @jonk how do we move this to a chat? Would you mind initiating the chat? – Aguevara Jan 24 '17 at 18:12

Am I missing anything?

YES

• hFE drops to <10% when Vce(sat) <1V at max current,
• but you only need 50mA out, so base current (ideal°) is 10% or 5mA
• The load V/R determines the Ic value, not hFE
• but Ib can limit Ic as well if Vce is not saturated

° Note: ideal meaning guaranteed specs for Vce(sat) , although if you know margins and operating temp range, Ideal may be most efficient Ib that does not compromise Relay speed vs Vout for arc suppression and MTBF (advanced design principles)

. .

Does this all look good??

NO Completely missing understanding of BJT as a saturated switch

• examine all these graphs below then in future search/Look for another OEM semi datasheet that shows graphs (typ only)until you realize hFE=$\beta$ or Ic:Ib=10 is de facto standard for reliable switching, not the linear range used for Vce>1~2V(min)
• ONSemi is better in this case, But everyone agrees in tables for hFE=10 when Vce= Vce(sat) for worst case specs. These are "gold" standards for interchangeability.
• Using hFE= 10 is a conservative "de facto' stanadard for high power but you can often get away with hFe=30 (maybe 50) over temp if you can tolerate higher Vce. (Ic*Vce(sat)=Pd)
• Diodes Inc and TI do make special ultralow Vce(sat) ultra high hFe BJT's with very low ESR or rCE values in milliohms but \$

So if you look at graphs below and locate Ib= 5 mA for a load of 50mA, what is real Vce(sat) @25'C then recalculate coil current and compare with worst case needed. ( it should be < 50mA ) in order to estimate design margin and relative speed of contact switching. note relay current gain at max contact rating is better than a transistor but slower. Icontact=16A (resistive) Icoil=50mA therefore current gain of relay= 16A/50mA =320 ( Power gain is even more)

simulate this circuit – Schematic created using CircuitLab

simulate this circuit Relay spec for 5V is 100 Ω

Thus Rb from 3.3 to Vbe = 50 %V drop/Vload reduces 1000 Ω to 500 Ω

• Would you be willing to explain the formulas and necessary steps to get this working? I would really appreciate it a great deal! I just want to gain a solid understanding so I can do this on my own! I am do not seem to grasp what you're laying down here. I would need base everything off of the test conditions for Vbe(sat) right? – Aguevara Jan 6 '17 at 20:00
• Not sure how the Vce(sat) has any significance. How I would properly choose the correct hFE. So to my understanding I would start with a BJT that has a max Ic(200mA) greater than the current the load draws(SPST relay). Which is 50mA(Coil current) if the coil resistance is 100 Ohms and the coil voltage is 5v. I = V/R. – Aguevara Jan 6 '17 at 20:00
• the base current according to test conditions for Vbe(sat) is 5mA with a 50mA Ic. 3.3v -.7(Vbe(sat) voltage) / Ib = 2.6/ 5mA = 520 Ohms. The relay can handle the wall current. Its max voltage rating is 227VAC. – Aguevara Jan 6 '17 at 20:07
• hFE @ 30 for the 2n3904 would yield : IC = 100 mA & VCE = 1V That would mean that Vbe(sat)'s Ic would also have to be at 100mA but the highest Ic for Vbe(sat) would be 50mA. – Aguevara Jan 6 '17 at 20:16
• Figure 17 shows Vbe and Vce for Ib,Ic but all we really need is the Ic/Ib=10 for 99% of designs. For better efficiency in small currents Ic/Ib=30 is often used and sometimes 50:1 Remember hFe only applies if your collector voltage is high enough and NOT saturated then you ignore hFE and use tables for Vce(sat) at Ic/Ib ratio in spec,l – Tony Stewart Sunnyskyguy EE75 Jan 6 '17 at 20:20