enter image description here I am very new in electronics. I am controlling a relay with stm32 micro controller. For the isolation I have used a 4N33 opto-isolator. As my relay coil needs around 250 mA, I used a mps222 transistor. I figured that the logic gets inverted when I used the gpio as the source voltage for the opto-isolator led. I just used a pull up instead and connected the other end of the led part to the gpio. Gpio is open drain, I will drain around 3mA in the pin. I do not have the R2 resistor grounded at this moment. My problem is if in case my stm gets disconnected then my relay will be turned on - which I want to avoid. Could anyone please tell me if adding this R2 will fix the issue?

enter image description here

  • \$\begingroup\$ Is there any problem with inverting the relay drive? Swapping R4 from the collector of Q1 to the emitter of Q1 (disconnecting the link to ground of course) \$\endgroup\$ – Spoon Mar 1 '13 at 8:12
  • 1
    \$\begingroup\$ Why do you need to isolate the micro from the coil side of the relay? Relays already provide isolation between the coil side and the switches. In other words, why not connect GPIO to the left end of R4 and eliminate the opto altogether? You'd have to invert the level on GPIO from what you show above, but that's minor firmware change. \$\endgroup\$ – Olin Lathrop Mar 1 '13 at 14:13
  • \$\begingroup\$ @RobinClarke so is it push-pull or open drain output? You specify push pull in the schematic and open drain in text. \$\endgroup\$ – miceuz Mar 2 '13 at 1:49

You can do it like this. Remember, BJT transistor is a current controlled device, that's why you can stack them on top of each other to form a Darlington pair.

Your optocoupler will have a current transfer ratio of ~400% at 3mA LED current, this makes 12mA running thru Q1. For Q2 you need a transistor with hfe greater than 250mA/12mA=20 MPS2222 seems to have hfe of 75 at base 10mA current, so you should be ok.


simulate this circuit – Schematic created using CircuitLab


simulate this circuit

Edit on MCU pin mode

From your comments I get that you don't get exactly how push-pull and open drain output stages operate. While it's discussed in this question, I'll just give a short description.

Plase note, that in most stm32 MCUs outputs can be configured as open drain or push pull and whole combination of internal pull ups and pull downs. This is versatile and usefull.

Now, what open drain is - it's just a transistor with it's drain (collector) unconnected - you can hook up your load to this drain (D1 in my schematic). You use open drain when you want to switch current. It can only sink current, not source it.

When the open drain pin is off, no current flows into the pin, the voltage at it is undefined, it is said to be "floating". When the pin is on, it just ties to the ground whatever it is connected to it.


simulate this circuit

When something outside of the pin wants to read voltage (like high impedance input), you solve this by hooking up a pull up resistor to open drain. Now, while the pin is off, output will be high as the resistor is pulling it, when the pin is on, the internal transistor slams bottom side of pullup resistor to ground.

Push-pull output actively sources and sinks current, when it's on - current flows out of the pin, when it's off - current flows into it. You usually don't use pullups or pulldowns with push-pull output.


simulate this circuit

  • \$\begingroup\$ What is the purpose of R3? \$\endgroup\$ – Wouter van Ooijen Mar 1 '13 at 10:49
  • \$\begingroup\$ oh, yes, R3 is not needed as relay will limit the current going thru Q2. too early in the morning was it. \$\endgroup\$ – miceuz Mar 1 '13 at 11:23
  • \$\begingroup\$ @miceuz, Thanks for your reply. So I am still inverting the signal the.Since Q2 will not operate unless I have zero signal in my MCU. You are right MPS222 is capable of 600mA. I should be fine. I was hoping to operate the circuit such that it is on when I have High output.Thanks for your nice explanation,really great! \$\endgroup\$ – Robin Clarke Mar 2 '13 at 0:36
  • \$\begingroup\$ @RobinClarke you should be good without any inversion. While MCU starts up the output probably would be high impedance and the current will not be running thru D1, thus not turning on the transistors. When you output high on the pin, the mosfet on the output of the pin will conduct, D1 will light up turning everything on. Oh. Provided that you have open drain output. Please clear up on that. \$\endgroup\$ – miceuz Mar 2 '13 at 1:45
  • \$\begingroup\$ @miceuz, Sorry about the confusion. I am using the open drain for sure. I just mentioned the push pull mode only if I use the figure at the very top,where the output pin will source the voltage for D1. Thanks for clarifying! \$\endgroup\$ – Robin Clarke Mar 2 '13 at 6:44

No, adding an R2 as you show will not solve your problem.

A disconnected processor is (in this case) equivalent to a processor NOT pulling the pin low (outputting a logic 1). Hence you will need to wire things to have the relay powered when a 0 is applied, and not powered when a 1 is applied. Your processor's program will have to live with this inversion.

A simple way to achieve this is to put the Q1 in series with R3. R4 can be left out, but you might need a resistor from Q2's base to its emitter, to avoid switching the transistor on by the leakage current of Q2.

PS: your circuit shows a common ground (and common 5V?) for both sides of the optocoupler. This makes the optocoupler useless.

  • \$\begingroup\$ @ Wouter van thanks for your reply. I will change the grounding of the left side of the octo-isolator .you are roght it is high logic if the micro controller is disconnected some how and the circuit will be turned off.Thanks! @ Nick I am using a 50 amp relay to switch 240V , so I thought to use an isolation to protect my micro. \$\endgroup\$ – Robin Clarke Mar 2 '13 at 0:50
  • \$\begingroup\$ Hi All , I was wondering if the image that I added at the top works.Seems like I can get rid of the inversion.Please let me know what you think.As Wouter pointed out that common 5V ground will not work,I could ground the lest side of the opto isolator to the micro as micro pin is 3.3V.I really appreciate your help.I was all without help until now.Thanks! \$\endgroup\$ – Robin Clarke Mar 2 '13 at 1:20
  • \$\begingroup\$ @RobinClarke get rid of R3, that was my mistake. \$\endgroup\$ – miceuz Mar 2 '13 at 1:52
  • \$\begingroup\$ @miceuz, really sorry I could not reply earlier.It really makes sense.You made it very clear.I am going to make the circuit this weekend.Thanks a lot!I really really appreciate your help. \$\endgroup\$ – Robin Clarke Mar 5 '13 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.