First of all: I'm still learning about electronics, so please be patient with me and help me when I use wrong vocabulary. I am sure this question has already been asked but I cannot quite find what I need. If you find a perfect answer don't hesitate to point me to it. :)

I have an extremely simple circuit that I would like to connect to my Raspberry Pi. For this I bought something very similar to this: https://www.amazon.com/SunFounder-Channel-Shield-Arduino-Raspberry/dp/B079PTBBL8 (and some connecting wire)

Basically I want to detect when the switch Ta1 is closed, which causes LED1 to light up. In order to do this I figured I need to connect the same wires to the optocoupler on the relay board. Searches for this only end up giving tutorials on how to control the relay board from the Raspberry Pi, but I want to "read" the state of the circuit I'm connected to. enter image description here Normally I would just try to connect these somehow, but I'm afraid I could break the Raspberry Pi or the relay board.

Please advise me whether my next steps are correct:

  • Connect one of the Ground pins on the Raspberry to the GND pin on the board
  • Connect one of the 3.3V pins on the Raspberry to the VCC pin on the board
  • Connect one of the GPIO pins on the Raspberry (say #2) to the IN1 pin on the board
  • Connect + and - of the circuit to one of the optocoupler modules. As you can see from the image on Amazon, one optocouple module has 3 sockets. How would I connect this?
  • Write a Python script to configure and query the status of the GPIO pin #2.
  • ...am I even using the relays?

Do I need to add additional resistors? I need both Ground and 3.3V so that the state of the pin is always defined and not "floating", correct?

edit: In case you are wondering why I don't connect Ta1 directly to a GPIO pin, 3.3V and Ground: I am actually tapping into an external circuit which has its own power supply, and I would like to know if there is electricity on the wires I am trying to connect to. I don't want to just have a switch. The switch Ta1 in the schematic is only to test the wiring. There is no actual switch that I have control over later, only a connection I can "read out".

edit2: This is how it works now (using "Button/Switch" with the optocoupler instead of a button from here: https://elinux.org/RPi_GPIO_Interface_Circuits ). enter image description here

  • 2
    If the opto-coupler is connected as show it will die immediately the switch is closed. What EXACTLY are you trying to detect? See elinux.org/RPi_GPIO_Interface_Circuits
    – Milliways
    Oct 16, 2018 at 23:40
  • @Milliways The optocoupler in the schematic is just a placeholder to show how I would like to connect the relay board to my circuit. I am trying to detect if the switch is closed. As in, have a variable in a Python script which is true when the switch is closed and false when it's open. If that requires the connection to move between R1 and LED1 or sequentially rather than parallel to R1 and LED1 that would not be an issue. If I don't actually need an optocoupler even better, but I was told that, since it separates both circuits, it may be safer to use one.
    – AyCe
    Oct 16, 2018 at 23:43
  • The module is the wrong kind of device. It COULD be made to work but you would need more components than doing it right. If all you want to do is "detect if the switch is closed" just use the circuit in the link.
    – Milliways
    Oct 16, 2018 at 23:47
  • @Milliways Apologies, I think I explained it wrong. See my edit please.
    – AyCe
    Oct 16, 2018 at 23:52
  • This question is unanswerable without details of the external circuit. By all means use an opto-coupler, but the relays just complicate things.
    – Milliways
    Oct 17, 2018 at 0:13

3 Answers 3


The piece of hardware you have linked is not really what you need. What you need, is what the diagram you have provided shows, plus another resistor.

No need for relays. You need a stand alone opto-coupler (which needs a resistor or you will burn it out)

When power is supplied to the light, it will also be supplied to the opto-coupler, which will send a signal to the RPi.

Relays (technically) could work, but its way overkill and just complicates things.

Good Luck! Keep Learning! :)

  • Thanks, I suspected as much after the exchange in the comments. I will accept this once i get it to work.
    – AyCe
    Oct 19, 2018 at 11:06
  • I solved it with just one optocoupler and a resistor. So happy that it works now! :) See edit2 of my question.
    – AyCe
    Oct 21, 2018 at 19:26
  • This will work In this case, but just remember that if you're load was anything more than an LED, running them in series (as in edit 2) would probably burn out the optocoupler which is why I did not suggest this. Oct 21, 2018 at 21:31

After reading both your edits to the question I can say you are doing the right thing, that will work for your need.

I would just be very careful with selecting the right resistor and LED, since they are all in series with the optocoupler (so really one current is circulating in your entire primary circuit). A too powerfull LED will not light up enough if the resistor is too high or the optocoupler demands too low of a current for example. Too low of a resistor with too powerful of a LED may burn the optocoupler.

That said, your first circuit (with LED in one branch and opto in another) would work safer, just add a series resistor to the optocoupler (and keep the secondary of the second picture, with the pull-up resistor to the Pi pin).


Frankly – you have posted in wrong “forum” to start with. This in NOT a discussion / learning forum, this is where statistical data is collected and UNNECESSARY criticism is received / posted. I would suggest you start with emulating switch in software. Build a “timing loop”, say 1 second interval , and simply toggle the switch in software. I instead of “flashing LED” have a print routine “LED On /OFF”. Few lines of code will do to learn.

Hardware IS unforgiving – as you have already demonstrated and received comments on your circuit. (BTW you need to be VERY specific about "detecting electricity").

On the other hand software “errors” are not that hard to fix. Just cause headaches. Good luck.

  • Of course this is for learning. Do you know for what the Raspberry Pi is made in the first place? It is made for education.
    – Ingo
    Oct 17, 2018 at 20:51

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.