# Circuit for 12V relay with an optocoupler using 3V3 GPIO

I am trying to design a circuit to control a G6S-2 12V relay that powers on/off a 12V device using a GPIO from an ESP32-WROVER and one PC817 optocoupler.

This is the circuit that I have come up after checking many others:

I have seen multiple circuits using, for example, a BJT but I am not sure how to calculate the resistances and which BJT to choose. Would the BC860 be a good option, given that the relay needs 11,7mA ?

As far as I can tell, an ESP32 GPIO could supply around 12mA through its 3V3 GPIO and checking that the optocoupler has an input forward voltage of 1.2V, the R1 could be 220 to provide a safe 10mA to activate the optocoupler output.

At this point I am not sure how to calculate:

• R2 and R3 (If this is a good circuit.)
• R4 I am not sure if it is necessary since the G6S-2 has 1.028Ohm for the 12V version.
• Q1 - Is the BC860 a good candidate or should I use another?

I have also come up with this other version, where the optocoupler is activated using a low GPIO signal:

Which one of those 2 solutions would be the best to use? Are they correct?

• It would be simpler to change the relay for a P channel MOSFET and get rid of Q1 or the opto. There are some limitations imposed in doing so but, in most load switching circuits, those limitations would be of no consequence. Hence, why I'm totally reluctant to suggest which of your circuit ideas is best or how to calculate unnecessary resistor values. Feb 4, 2022 at 12:22
• Neiter is best. They both are filled with errors and bad decisions if they are simply copied from the interwebs, there is no point replicating other people's rookie mistakes and misunderstandings. First of all, as the optocoupler is not used for isolation, why include it at all in the circuit? You'll spend more current driving the opto from MCU than what the relay consumes. And you would not use a PNP transistor as a low-side switch. Why not simply use a NPN transistor or N-channel FET and drive them from the MCU without the opto? Feb 4, 2022 at 12:22

Given that you have the microprocessor ground tied to the relay circuit ground, you don't need an optocoupler at all.

• Throw out U1, R2, and R3.
• Connect the free end of R1 to the base of Q1.
• Change the value of R1 to 1k.
• Use an NPN transistor for Q1.
• Replace R4 with a wire.

Resulting circuit:

simulate this circuit – Schematic created using CircuitLab

• Thank you, I see that in this case, the optocoupler does not make sense at all... In order to choose the correct NPN transistor, what parameters should I look for? (I am a bit noob here) That the max Collector-Emisor voltage should be bigger than 12v and that the max collector current should be bigger than the 11,7mA expected by the G6S-2 12v relay? Do you recommend any NPN SMD in particular? Feb 4, 2022 at 12:52
• @paclema Design with margins, pick something that can deliver more current than the coil needs. BC817-16 perhaps (common part, dirt cheap), though you'll need to adapt to 3V3 levels. A N-MOSFET would work just fine too. Feb 4, 2022 at 13:00