Issue: I have a relay which is pushbutton controlled to turn it on or off. There is also an LED connected to the MCU output pin controlling the relay. The whole thing is powered by an AC - DC power supply.

When I plug in the PSU and power up, the LED turns on but the relay doesn't turn on. After pressing button a few times it works and after that it works properly. After that this problem does not occur even if I turn on off the switch but it occurs when I put it in a different switch.

What can be the reason for this? I am using an optocoupler and a BJT based circuit for relay control.

Schematic of the relay circuit I am using.

I am using 510 ohms instead of 470 R.


The relay works at 12V and the triggering voltage at PC817 is 5V via a GPIO pin.

An SMD red LED is also connected to the same pin as the relay output to depict the state of the relay.

  • 3
    \$\begingroup\$ To get an answer I think we need (a) The schematic of how the components are connected (b) The source code for the MCU (c) details of any measurements made in the debugging so far (d) links to the datasheets for the components. Please edit the question to include the information. \$\endgroup\$ Dec 27, 2023 at 11:37
  • 1
    \$\begingroup\$ W.r.t. but it occurs whenever i put it in different switch, does the design allow for switch bounce? \$\endgroup\$ Dec 27, 2023 at 12:09
  • \$\begingroup\$ Yes my code have switch de bounce and this error comes up very rarely. \$\endgroup\$ Dec 28, 2023 at 5:53
  • 2
    \$\begingroup\$ We need an accurate schematic including the LED. You can use the schematic editor to add it to your question. \$\endgroup\$ Dec 28, 2023 at 8:39
  • 1
    \$\begingroup\$ Exactly how is the extra LED connected to the MCU output pin? If you take that LED out of the circuit does it change or improve the operation of the relay? \$\endgroup\$
    – Nedd
    Dec 28, 2023 at 10:04

1 Answer 1


If you calculated that base resistor right for that relay, then this cannot work because the maximum current of your optocoupler is 30mA, which is too low (not typical.)


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.