simulate this circuit – Schematic created using CircuitLab

I have a Raspberry Pi that I want to use to detect if my desktop computer is on, by detecting if the 5V rail is powered. I have in the past used optocouplers to short pins on the motherboard together with the RPi, but when I tried to use my pc817 optocouplers for this purpose, they kept releasing magic smoke.

Am I doing something wrong? I was directly connecting one end of the optocoupler to the 5V wires in a USB cable I took apart and the other end of the optocoupler to the GPIO.

Thanks in advance! Neil

  • 1
    \$\begingroup\$ Could you provide a schematic of whatyou tried to do? It looks like your setup lacks a serie resistor \$\endgroup\$ – Sclrx May 11 '17 at 7:14
  • \$\begingroup\$ Just edit your answer, ther is a schematic feature embedded in the site \$\endgroup\$ – Sclrx May 11 '17 at 7:16
  • 1
    \$\begingroup\$ I think @Sclrx meant question. But yes, there's a schematic editor embedded in the question/answer editors Electronics.StackExchange. Just build it in there and all will be fine. \$\endgroup\$ – Mast May 11 '17 at 7:22
  • \$\begingroup\$ I added a schematic. I hope it helps. It should really be very simple. \$\endgroup\$ – ifconfig May 11 '17 at 7:26
  • 1
    \$\begingroup\$ An optocoupler would be useful if you can't guarantee a common ground between the systems. If using a current limiting resistor for a direct connection, what would probably be desired would be a pull down resistor, though likely there's enough load on a PC power bus that the voltage will quickly decay when the supply is off, so the pulling resistor would really only be there for the disconnected case. \$\endgroup\$ – Chris Stratton May 11 '17 at 16:55

First, your magic smoke comes from the fact that one should not supply a diode (here, the photocoupler's input diode) directly with a voltage source (in this case, the USB's 5V). You must add a series resistor to limit the current through said diode.

Schematic taken from The Raspberry Pi Hobbyist Blog

Schematic taken from The Raspberry Pi Hobbyist Blog, Ignore the resistor value.

According to the datasheet of your photocoupler, the input diode can sustain 50mA of current, but works fine with 5mA. i will aim for that current.

Again from the datasheet, the forward voltage drop of the diode is typically 1.2V, which leaves us with 5V-1.2V = 3.8V across our resistor.

Following Ohm's law, 3.8V/0.005A = 760 Ohms. Since this value is non-standard, we will use a 820 Ohms resistor, which will in turn give us 3.8/820= 4.6mA through our diode, close enough.

Conclusion : add a 820 Ohms serie resistor between your USB's 5V and your photocoupler and everything should work fine.

  • \$\begingroup\$ Alright, that makes more sense than what I have been doing. Thanks! \$\endgroup\$ – ifconfig May 11 '17 at 14:34
  • \$\begingroup\$ If I don't have an 820 ohm resistor, what direction should I go? Should I increase or decrease the resistance? Increase, right? \$\endgroup\$ – ifconfig May 11 '17 at 19:19
  • \$\begingroup\$ in this case, yes, 1k should be fine, but if you want to be sure, you can always calculate the current by adapting the values in the answer's formulae. \$\endgroup\$ – Sclrx May 12 '17 at 9:07

There is an LED inside the optocoupler. It needs a series resistor. Without a resistor the LED will burn out very quickly.


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.