# Activate 12 V Relay with a 5 V GPIO

What is the reason of using 10 kΩ resistor in series to the control signal? Is it related to the base current according to the NPN transistor's datasheet?

For example:

• My CONTROL/GPIO is 5 V.
• The maximum base current according to the NPN transistor's datasheet is 120 mA.

Does it mean I need to have minimum 42 Ω resistor? The base current, if the GPIO input is actually at +5V, will be approximately (5-0.7V)/R.

You should pick the R to provide 1/10 to 1/20 of the DC current that the relay draws, according to the relay datasheet. That's a rule-of-thumb with a typical jellybean NPN transistor. If you study the transistor datasheet you can usually find Vce(sat) values at Ic/Ib = 10.

For example, a 360mW 12V relay will draw approximately 30mA at room temperature. That means you should have 1.5 to 3mA of base current. So the resistor should be between about 1.5K and 3K.

If the GPIO does not give you quite 5V under that loading you might have to go a bit lower. In this case, assuming the GPIO gets within (say) 0.5V of Vdd, 2K could be a good value.

There is no need to supply excessive current to the base, it will result in needless power dissipation (much of it in the MCU at higher current levels) and the GPIO "5V" output will drop as a result of the excessive loading. Excessive current will negatively affect the reliability of the MCU chip.

The current guaranteed to switch a relay is amplified by a saturated NPN here that only has about 10% of it’s maximum linear hFE and current drive capacity far greater than the load to support a low Rce/Rcoil ratip to dissipate the same ratio of load power e.g. a PN2222A has an Rce = Vce(sat)/Ic ~2 Ohms , so a 40 Ohm coil means the transistor uses 5% of the load power.

A PN2222A has a mean max hFE of 200 (100 to 300 @ 150mA) so the base current needs to be chosen using a ratio of Ic/Ib of roughly 20 thus Rb must be calculated from Vcc-Vbe/Iout * 20

e.g. if I coil is only 50mA then Ib can be 5% or 2.5mA thus Rb = (5-0.65V) /2.5mA=~1k8. It may switch with less current when the hFE is high unsaturated with 5k to 10k but then the Vce rises and thus the temperature. For low power relays of 12Vx50mA = 600mW your 10k can work if you must minimize drive current but if your relay coil is >1W then the transistor temp rise would be significant with 200’C/W thermal resistance.

Yet a 5V uC has a ~ 50 Ohm driver so a load of 100 Ohms is possible but not necessary here.

Let us study the circuit design of a real High level trigger 5V relay KY019 shown below. The relay characteristics, operation, and photo are shown in Appendices A ~ C below.

The KY019 relay uses a Songle 5V relay switch, with nominal current of 90mA (measured 70mA). GPIO High is 3V3. For the OP's 12V relay switch with smaller nominal current of 40mA, and GPIO High of 5V, it is easy to just modify the base resistor for a smaller collector current.

The NPN BJT SS8050 is used in this relay. The circuit analysis is shown in the right hand side of the schematic.

The calculation suggests a base resistance of 1kΩ. However for dirt cheap toy relays usually without any power saving requirements, the vendor usually takes a big safety margin to allow for the big variety of device parameters, a much strong base resistance 150Ω is used, to make very sure that the relays do switch, no mater how poor quality control are the transistors. References

Appendices

Appendix A - 5V/12V/24V Relay Datasheet Summary Appendix B - Relay switching I-V Summary Appendix C - KY019 Relay Circuit Design Parameters 