Creating an opto isolator board for Raspberry Pi

I have a relay board which I bought from dx.com and I am not sure about its reliability. If it fails, then it may harm my Pi right? So I googled around and read that opto isolator provide complete isolation. Even though the relay board is built with an opto isolator, still I won't trust it.

I chose PIC 817 for this. There is no strong reason behind it. I found its datascheme here. From what I understand, for operating input should be 1.2v 20mA.

The relay requires 5v to turn on/off and 5v to operate the relay board.

Based on this I made following circuit and I calculated R1 using Ohm's law.

(3.3v - 1.2v)/0.02A = 105 Ohms


I also want to power the relay from Pi itself. I think I should keep a diode in between them so that its always unidirectional and there is no current coming from relay board to Pi. And I think diode with minimum 5v should do for this purpose. I picked 1N4007 because it's easily available for me. It's ratings are 1000v and 1A. Or should I be using a transistor instead?

Is following circuit correct? Do I need to any extra component? and also can this whole thing can be replaced by any available IC?

simulate this circuit – Schematic created using CircuitLab

First of all, you probably don't need the PIC 817 at all. There is a transistor or "something" at the input of the relay board which keeps the Pi out of harm. Unless you have some freak accident.

That said, the PIC 817 is wired wrong. As it is know the collector will short he Pi 5V to ground when it activates. You need a pullup resistor instead of the straight wire to the diode. Note that your logic will be "active low" by using the optocoupler. A "1" on the GPIO will activate the optocoupler which will pull the relay boards CH1/2 low and deactivate the relay.

The GPIO on the Pi will probably also risk being fried by your PIC 817 diode. Have you looked up what the maxium allowable Pi GPIO current is? I think it is pretty low.

You also need to check what current the relay needs. The hk3ff-dc5v-shg relays seem to have a coil resistance of 70 Ohm. That would be approx. 0.14A on the 5V regulator if both relays are drawn. Is that OK?

• What if the relay board uses some poor quality components? What if the transistor on relay board fails? I googled and allowable GPIO current is 1.2v I think, then I am not so sure as not I am not getting proper google results. – avi Jun 1 '14 at 4:52
• 1.2V is a voltage. Not a current. Google for: Raspberry Pi GPIO output current. As biggcsdiccvs said in the other answer, the type of failure mode where the relayboard kills your GPIO is unlikely. – Dejvid_no1 Jun 1 '14 at 10:13
• sorry, its 3.3V at 16mA if GPIO as input and 3.3v, 50mA GPIO output. I do not want to take any risk with my rPi. What if there is a short circuit within the relay, still it wouldn't harm my Pi? The wiki page also mentions its importance. – avi Jun 1 '14 at 11:29

First of all, you don't need to worry about the relay board failing in such a way that it will destroy your Raspberry Pi. That's an extremely unlikely failure mode. And if you're powering it from the same source, then you're not completely protected, either, even with that diode. But again, it's an unlikely scenario, basically something like a lighting strike / power surge into the load circuit strong enough that it would create an arc into the relay coil. And the optocoupler still wouldn't give you complete protection against that.

Unfortunately, it's not clear what the control voltage levels are for the the relay board. If it's 5 V as you say and your board outputs 3.3, then there is a problem, but I don't see where you got that information. The relay board has driver transistors and my guess is that it will work just fine with 3.3 V control signals.

But if you really want to use an optocoupler, you can do that. At least it will work as a level shifter. Your circuit will not work, though -- you will fry either your optocoupler or the unnecessary D1 as soon as you send current through the optocoupler's LED. The optocoupler does not require a separate supply voltage. You can think of it as a polarized solid-state relay. You could insert a load resistor (let's say, 10k) between pin 3 of the optocoupler and ground and connect that junction to the respective input of the relay board. BTW, you need a separate optocoupler for each channel, of course. Also, 20 mA drive current would be unnecessary, 5 mA would be sufficient, so make R1 400 Ohms.