I am trying to use Raspberry Pi GPIOs to turn on and off optocouple switches controlled by transistors.

I really have no idea what I am doing (programming the thing okay. Wiring it? Not so much). I think that the base resistors are my problem, but I really don't know.

There are 20 switches.

Each one is connected to a separate GPIO pin at 3.3 Volt Output. (I have only drawn 3 for clarity)

The Optocouples require 1.2 V at 20 mA.

I really think my problem is the 10k ohm resistors but I don't have a clue how to calculate the needed resistance here.

Please be kind, I know it is a stupidly simple circuit and question. But I don't know how to go about this.

enter image description here

  • 1
    \$\begingroup\$ What are you doing with these lines? What frequency, duty cycle, etc. Any and all information you can provide will help a lot. Other than that, you are using a single R1 for all of them. Are you using them at the same time? Also, is there a reason you cannot sink \$5\:\text{mA}\$ with your GPIOs? \$\endgroup\$ – jonk Mar 1 '18 at 4:31
  • 1
    \$\begingroup\$ In the text you say "10 KOhm resistors" but I don't see them in the diagram... Also, a common resistance (R1) for all the LEDs of the optocouplers will mean trouble. Give each LED its own resistance. \$\endgroup\$ – Claudio Avi Chami Mar 1 '18 at 4:32
  • \$\begingroup\$ you're right... i'm sorry that is my mistake... they are 10K ohm, i accidentally marked them as 10 ohm \$\endgroup\$ – me_ Mar 1 '18 at 4:34
  • \$\begingroup\$ Edited to fix the resistors in the schematic \$\endgroup\$ – me_ Mar 1 '18 at 4:41
  • 1
    \$\begingroup\$ @me_ (1) The optoisolator you specified appears on first blush to only require about 5 milliamps of current to drive it. Most GPIO pins can handle that without the addition of external transistor circuits. So I believe all that extra stuff may not be required. Just one resistor per GPIO. (2) How often will you set these GPIO HI or LO. Once a year? Or once every microsecond. It matters. (3) What % of the time will they be HI and what % LO? \$\endgroup\$ – jonk Mar 1 '18 at 6:12

As pointed out in the comments one potential problem is the use of a single pull up resistor, (R1), for all the optocouplers. For correct operation each optocoupler should have a separate pull up resistor, (and they should not be all tied together, for example - remove the extra wire segment between each pin 1). In your original schematic if you were to trigger one optocoupler all the others would become disabled (to some degree but not guaranteed).

As for the base resistor, that determines the base current in the transistor according to (Vin - Vbe)/Rb in your case (3.3V - 0.7V) / 10k = 0.26mA. This base current is then multiplied by the gain of the transistor to give the collector current (max). A typical MPS2222A has a gain as low as 35 with low base current, so the collector current could potentially be as low as 9.1mA, that should be well enough to turn on the optocoupler, (the 20mA spec might be a max).

Another point from a comment is that the each I/O port could most likely sink enough current to turn on an optocoupler without even using the transistor. You would just connect each I/O pin directly to each pin 2 of the optocoupler, keep a 200 ohm pull up on each pin 1 but instead of using 5V use the main system's 3.3V at the top of each resistor. In this case the optocoupler LED current would be about 10.5mA, and the logic would be reversed, (a low on the I/O pin turns on the optocoupler).

Also note that you will need to have a power and load connected to the output pins of the optocoupler to verify the switching action. (Your schematic does not show what is connected at those points.) Unlike a mechanical relay you cannot normally measure the on/off switching with just a DVM placed on the open pins.

| improve this answer | |
  • \$\begingroup\$ thank you for all the info... I have verified the "missing" part of the circuit to be working when proper current/voltage is passed. My problem definitely lies on the gpio/optocoupler side of things. I did not understand the comment above about just wiring directly from each GPIO. It makes more sense than what I am doing though. I will try rewiring with your suggestions. If it works I will be glad to select yours as the correct answer. Thank you for your help. \$\endgroup\$ – me_ Mar 1 '18 at 7:31
  • \$\begingroup\$ I put the 3.3v with 120 ohm resistors (below max current, well above min current) on pin 1 for each switch and kept the logic with 1 as on and 0 as off... after tracking down some bad soldering (told you i was new to this) everything works... my newly created magic mirror is now the biggest cable box and tv remote combo you've ever seen... not likely to misplace it :P... thank you for the help (all three of you) and answer accepted nedd. \$\endgroup\$ – me_ Mar 2 '18 at 18:53

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.