0
\$\begingroup\$

For a current project, I have to read the state of an LED in another circuit with a Raspberry Pi.

My current schematic is this:

schematic

simulate this circuit – Schematic created using CircuitLab

Everything inside the dashed box is not physically accessible to me in the final version of the project so I can only attach the wires next to the button. (The LED is still visible)

Now when I start my code for reading the state of Pin 12 with the internal pull-down resistor I only get 0s as Output.

Code for reading the Pin (with pull-down) :

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

for i in range (5):
    print GPIO.input(12)
    time.sleep(.01)
GPIO.clear

Output (pull-down) :

0
0
0
0
0

My idea here is that the current might go through the resistor and not the pin but I am not sure on that one.

So if I eliminate the pull-down and try the same thing again I'll get an alternating pattern of 1s and 0s.

Code for reading the Pin (without pull-down):

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN)

for i in range (10):
    print GPIO.input(12)
    time.sleep(0.01)
GPIO.clear

Output (no pull-down) :

0
1
0
1
0
1
0
1

From my understanding this comes due to missing pull-down resistor since the current has nowhere to go so it is creating this interference.

Now I wanted to ask how it would be possible for me to get the current state of the LED with circumstances given and where this alternating pattern is coming from? (Maybe it's a misunderstanding of pull-down resistors).

Solution:

Thank you @Transistor

the Pi needs a return path for the sensing current.

The final schematic is this:

schematic

simulate this circuit

Now the Output is:

  • LED off : 1
  • LED on : 0
\$\endgroup\$
1
\$\begingroup\$

Yes the Pi needs a return path for the sensing current.

schematic

simulate this circuit – Schematic created using CircuitLab

Figure 1. The Pi needs a ground reference to the original circuit.

If the LED box is fully isolated then you can do this:

schematic

simulate this circuit

*Figure 2. For isolated units the contact voltage can be monitored as shown."

With the switch open the Pi GPIO will read 'high' and with the contact closed will read 'low'.

\$\endgroup\$
  • \$\begingroup\$ First of all, thank you for the answer. The 1st schematic worked like a charm. The second one although was not quite working as my Output was 0s only. Interesting though is that as soon as I start my scripts the LED starts lighting up just a bit. So maybe there is some current coming from the pin even though it's set as an Input. \$\endgroup\$ – Ironlors May 27 '18 at 15:39
  • \$\begingroup\$ That means that there is an error in your question: "Everything inside the dashed box is not accessible to me so I can only attach the wires next to the button." \$\endgroup\$ – Transistor May 27 '18 at 15:41
  • \$\begingroup\$ I'm currently working on a test circuit where I have access but I won't have the access in the final version. \$\endgroup\$ – Ironlors May 27 '18 at 15:43
  • 1
    \$\begingroup\$ "Interesting though is that as soon as I start my scripts the LED starts lighting up just a bit." Check if the Pi has internal pull-ups and, if so, turn them off in your code. \$\endgroup\$ – Transistor May 27 '18 at 15:46
  • \$\begingroup\$ I disabled all internal pull-ups / pull-downs and replaced them with a physical 10k pull-down. The second version is now working like a charm. I'm not sure why since GPIO.IN, pull_up_down=GPIO.PUD_DOWN should set the pull-down and deactivate the pull-up but the problem is solved. \$\endgroup\$ – Ironlors May 27 '18 at 16:08
1
\$\begingroup\$

A better solution is an opto sensor attached over the led. You can monitor it without electronically connecting to the circuit. Nothing more than taping a ldr or photodiode over the led.

\$\endgroup\$
  • \$\begingroup\$ "Everything inside the dashed box is not accessible to me in the final version of the project." \$\endgroup\$ – Transistor May 27 '18 at 15:57
  • \$\begingroup\$ @transistor so your telling me that the led is inside a sealed box without any light escaping to the outside? That's stupid. Why would you design it like that. \$\endgroup\$ – Passerby May 27 '18 at 16:14
  • \$\begingroup\$ I'm sry for my phrasing. You can see the LED but you do not have physical access. Thank you for this point of view regarding the solution. \$\endgroup\$ – Ironlors May 27 '18 at 16:19
  • \$\begingroup\$ Oops! I read "opto-isolator" where you wrote "opto-sensor". Your solution will work if the OP can tolerate the stuck-on component. As an aside, I did see a CRT colour monitor advertised a few decades ago and its blurb claimed that the EMI shielding blocked all EM radiation. I wondered what use a monitor that didn't emit any light was. \$\endgroup\$ – Transistor May 27 '18 at 16:20

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.