I have an embedded device which uses an STM32 microcontroller to drive a relay. If the controller leaves the relay turned ON for a long time (~ 1 hour), the relay then refuses to turn back off when it receives the signal to do so.

Schematic: Relay driver circuit

  • I checked the voltage at the GPIO output (pin 1 of R15) and the controller successfully changes the pin state when it should.
  • I get 0.7V across pin 1 of T2 and GND when the relay is ON, and close to 10V when it's supposed to be OFF but remains ON.
  • I get 1.4V across pin 1 of R16 and GND when the relay is ON. When the relay remains ON after the controller turns it OFF, and I connect a multimeter across pin 1 to check the voltage, the relay immediately turns OFF.

Some more info:

  • Reports from the field suggest that this issue has popped up only in a few controllers (a batch possibly) but that isn't guaranteed to be the case. The relays were all from the some batch.
  • This device is on a PCB and there's a transformer sitting right next to the relay which heats up to a small degree after long hours of operation.
  • Turning the system OFF, and providing external 12V to the relay coil terminals turn the relay on and off promptly
  • Unlike shown in the diagram, the grounds on both sides of the optocoupler aren't shorted together.

Are there any design issues with the circuit itself? Being a newbie I can't spot anything that is wrong with the design of the circuit itself. Any help is appreciated!

Edit: Component values

R15 - 470E
T4, T3, T2 - BC547
OK3 - PC817
R16 - 2K7
D5 - 1N4007
K1 - HF118F

  • \$\begingroup\$ What is value of R17? Also, why the design is like it is - what is the purpose of opto-isolator as it is not used for isolation? Which exact relay model that is? If the coil current is really just 20mA, why on earth waste three transistors and an optoisolator just for driving a 20mA relay from 3.3V GPIO pin? \$\endgroup\$
    – Justme
    Commented Aug 11, 2023 at 9:17
  • \$\begingroup\$ @Justme It is being used for isolation isn't it? The last bullet in the question implies that the 12V and 3.3V supplies are isolated. The relay probably could be directly driven by the optoisolator, but I suppose this gives a bit more flexibility in terms of relay (i.e. they could easily change it to a higher power relay without changing the rest of the design). T5 might be necessary due to the MCUs limited drive strength. Using a darlington pair is definitely overkill though. \$\endgroup\$
    – BeB00
    Commented Aug 11, 2023 at 22:24

3 Answers 3


It sounds like your optocoupler is leaking a small amount of current when it heats up and is turned off (or under some other condition that happens after a while). This small current is being amplified by your high-gain darlington transistor array, and providing enough current to keep the relay on (shown by the 2V being dropped across your relay when it's supposed to be "off". When you try to measure the R16 voltage with your multimeter, enough current leaks through the multimeter to turn off the relay.

Relays will have two important voltages - the "must operate" voltage (the voltage at which the relay must turn on), and the "must release" voltage (the voltage at which the relay must turn off). Anything between these voltages is indeterminate - often the "must operate" will be about 80% of the rated voltage, and the "must release" will be about 10% (although you should check your relay datasheet). In this case, the "must release" voltage would be 1.2V. Since your relay is seeing 2V, it would be within spec for it to stay on when your circuit turns it off.

To fix this, you could put a resistor to ground from OK4 pin 3, or the base of the darlington pair.

  • \$\begingroup\$ Thanks. This was what I've been thinking as well. A batch of optocouplers could have a slight flaw that could lead to a much increased leakage current, right? \$\endgroup\$ Commented Aug 11, 2023 at 2:30
  • 2
    \$\begingroup\$ @nottherealfaraday you should be designing your system to work within datasheet specified tolerances. All of your optocouplers will leak different amounts, but the datasheet will tell you what the maximum is. I'm not sure I would consider it a "flaw" if some of them are leaking more than others, as long as it's within the datasheet specification \$\endgroup\$
    – BeB00
    Commented Aug 11, 2023 at 6:52
  • 3
    \$\begingroup\$ Assuming in your question you meant that OK4 is PC817, the leakage max is 100nA, and if your transistors are both at 600 gain, that maxes out at 36mA going through your transistors, which would be the full 12V across your coil (and then some). Obviously that's not happening, because your leakage isn't as bad as it could be, and your transistors (and circuit) isnt as high gain as it could be, but that would all be within tolerance if that did happen. \$\endgroup\$
    – BeB00
    Commented Aug 11, 2023 at 6:59
  • \$\begingroup\$ Thanks for explaining that! Putting the resistor solved the problem! I'm going to mark your answer as the solution. @BeB00 \$\endgroup\$ Commented Aug 14, 2023 at 13:10

and I connect a multimeter across pin 1 to check the voltage, the relay immediately turns OFF.

This is the clue. There is enough leakage current, likely from collector to base of T2 to keep the transistors turned on.

No component values are provided, but a resistor between 10k and 100k from the base of T2 to ground will solve the problem. For a Darlington pair it may be wise to put a 10k from the base of T3 to ground as well. These resistors will discharge the base emitter capacitances as well.

These resistors need to be small enough to turn off the transistors while still allowing current through the opto-coupler to turn them on.

  • 1
    \$\begingroup\$ I would size that resistor to be between 1 and 1/10 times the resistance of R16 -- which for sensible values of R16 probably overlaps with your 10k to 100k. \$\endgroup\$
    – TimWescott
    Commented Aug 11, 2023 at 0:32
  • \$\begingroup\$ @RussellH Added component values! \$\endgroup\$ Commented Aug 11, 2023 at 2:07
  • \$\begingroup\$ @TimWescott R16 is in fact 2.7K. Is that a design flaw? I understand that R16 limits the current flow to the base and this seem to be more than enough if we select max base current as 5mA. \$\endgroup\$ Commented Aug 11, 2023 at 2:08
  • \$\begingroup\$ 2.7k is reasonable, and yes, it doesn't overlap the 10k to 100k mentioned, so I was wrong on that point. Generally you want the resistor in parallel to the base to be less than or equal to the series resistance, and usually that's best. However the actual best resistance depends on a bunch of things, notably the voltage and current capability of the driver, and the current needed by the base. \$\endgroup\$
    – TimWescott
    Commented Aug 11, 2023 at 2:44
  • 1
    \$\begingroup\$ There is also 100 nA leakage from the PC817 optocoupler. But yeah, even 100 kohm will sink that easily. \$\endgroup\$
    – jpa
    Commented Aug 11, 2023 at 9:11

Are there any design issues with the circuit itself? Being a newbie I can't spot anything that is wrong with the design of the circuit itself. Any help is appreciated!

I will mention one. You have a standard diode across the coil of the relay to snub the inductive kick when the coil is turned off. That is good, but because it conducts at a low voltage, it can cause the turn off of the relay to be sluggish. This can cause premature failure of the relay contacts. A improvement to the single standard diode snubber is to add a Zener diode in series, (but in the opposite direction). This will raise the voltage of the inductive kick, but will speed up the relay snapping back to its un-energized position. Since your power supply is 12V, I would recommend a 10V Zener.

For more information about why adding a Zener to a relay coil snubber is useful, see this application note from TE

  • 2
    \$\begingroup\$ The point you raise is a valid concern. Depending on the type of the load (with inductive loads being most evil), slow release of the relay can cause arcing, and finally result in the relay getting stuck closed when it is supposed to open. In that case, you can often "fix" the issue by applying a mechanical shock. In this question, the relay gets unstuck by measuring the darlington base voltage, though, so the problem at hand is definitely the leakage current, not the slow turn-off time. Adding a Zener to the snubber might still be a very good idea to enhance relay life time. \$\endgroup\$ Commented Aug 11, 2023 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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