I'm currently designing a PCB in order to drive relays (5V) with an optocoupler and a Raspberry pi (a CNY74-4 here is the datasheet). So I made this schematic:


My questions are the followings:

  • Is my scheme right?
  • The 5V come from an DC/DC supply which deliver 2.1A and 5V, it's not a problem for the optocoupler and the transistor?

  • Which CMS transistor can I use in this case?

  • Is the optocoupler good for this use?

THanks for your answer!

  • 1
    \$\begingroup\$ Why use opto-couplers? The relays will give you all the isolation you need. You can drive the transistors from the Pi directly (with the base resistor), or use a ULN2003. you have also missed out the back-emf diodes from across the relay coils. \$\endgroup\$ – Steve G Sep 2 '16 at 8:09

The 5V come from an DC/DC supply which deliver 2.1A and 5V

I suspect that there isn't a need for the opto couplers. By the sounds of it, the 5V is rated also for the RaPi (2.1 A) and so there is no benefit from adding the opto because the relay will provide isolation from the load however, you do need to use flyback diodes across the relay coils to prevent damage to the NPN transistors.

Looking at the spec of the opto, it has a minimum CTR (current transfer ratio) of 50% and therefore you are roughly obliged to feed (waste) more current driving the opto than you would driving the NPN directly.

  • \$\begingroup\$ I put an optocopler because all the Raspberry pi relay module have one on it. So why they add optocopler on this module? \$\endgroup\$ – Majonsi Sep 2 '16 at 8:28
  • \$\begingroup\$ I can't tell you why someone has decided to add what appears to be a redundant circuit. As a guess, maybe you can feed the opto output side with another isolated 5V supply? I can see some benefits of this as a general product but in your particular case I cannot. \$\endgroup\$ – Andy aka Sep 2 '16 at 8:32
  • \$\begingroup\$ Thanks for your advices ! I will use an ULN2803 it will be better! \$\endgroup\$ – Majonsi Sep 6 '16 at 13:32

You are missing catch diodes on the relay coils. Back-EMF from relays will blow the transistors when they are turned off.

Power supply from DC/DC converter is perfectly okay for relays.

You can use a BC547 transistor or a driver chip like ULN2003 or ULN2803 (they have built-in diodes and base resistors).

Optocoupler is fine.


In the "does it work" respect, it will work. You just need to find out what resistors should be placed in the transistor bases. (This is really important in order to have safe and fully functional circuit)

But some small problems that you may encounter and you must check:

  1. The optocouplers EASILY burn if the LED is lit with excessive current. Check that optocoupler LEDs are series with right resistors.
  2. Also if the resistor is very high in value, the optocoupler phototransistor might not be completely in ON-state.
  3. Check datasheet of the optocoupler. It might be able to sink enough current to drive the relay coil, so that you could remove those 4 transistors attached to relay coils.
  4. The relay coil has noticeable mutual/self inductance. As we know from power electronics, Switching inductors and motors using transistors without use of catch diodes (Sometimes called free-wheeling diodes) will put the poor transistors in risk of having excessive voltage applied to their Collector-Emitter pins and being blown. You have to insert a freewheeling diode with Cathode connected to collector and Anode connected to emitter. As @filo mentioned, you could use ULN series which already have the diode.
  5. I agree with Andy. If you want isolation of the Raspberry Pi from the outside work (You must be wanting that either way), the relay is already giving you that isolation. Optocoupler is just one more step of precaution. You can remove the optocoupler and power the coils using those 4 transistors by directly connecting the bases to Raspberry Pi (via resistors surely!). But make sure that you have a good 5V supply. Some 5V Relays just fail to turn on with low voltages as low as 4.2V. (Notice that when you turn on the transistor, a little amount of voltage known as VCE(SAT) ~ 0.2V drops on the junctions)

Also the 2.1A power supply is surely sufficient. Relay coils have small resistance but they usually draw about 30mA~150mA (depending on type and quality and also design of the circuit). Check the relay datasheet.


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.