Page 1 of 1
LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 1:37 am
by bryanhun
Is it possible to use a GPIO input to tell if a LED is on or off? It is separately powered from a 9v battery in this example.
GPIO.setup(pin, GPIO.IN) is giving me false positives. Any help is appreciated!
Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 4:30 am
by sprinkmeier
What does the circuit look like?
Note that 9V and GPIO does _NOT_ mix.
For that matter, no LED I know of runs off 9V. I hope you have a current-limiting resistor in there somewhere.
Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 2:31 pm
by bryanhun
I simply have one resistor between the + end of the LED and positive end of the battery. It drops it down to around 2.2v.
The reason I am using 9v is because in this particular application, there is a device that either has 0v or 9v in the line to signal a 1 or a 0 that I need to be able to read. What is the best way to detect this?
Thanks.
Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 2:37 pm
by pluggy
Could the LED circuit be connected to the Pi or does it have to be an optical solution ?.
Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 2:44 pm
by bryanhun
Yes, the LED circuit can be connected to the pi. If there is a better solution to detect the 9v, I am also open to that as well.
Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 3:46 pm
by pluggy
Voltage divider circuit. 2 resistors say 10k and 22k in series between 9v and 0v on the battery. Connect 0V to gnd on the Pi and a connection from between the 2 resistors to the GPIO pin on the Pi. The 10k should be at the gnd/0v end, the 22k at the 9v end. It basically divides the voltage so there is ~3 volts between them which can be fed to a gpio pin. Feeding "voltage divider" into google will give you the idea.

Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 4:46 pm
by bryanhun
Thank you Pluggy. Would it end up something like the image below? But, I would swap the "Echo" with the positive end of the battery, and the negative would be the "Ground" on the module in the image? (R1 would be 22k and R2 would be 10k)
Thanks again for your help.

- Ultrasonic-Module-Circuit-e1356208885394.png (34.91 KiB) Viewed 2843 times
Re: LED Status On/Off GPIO Input Reading
Posted: Wed May 29, 2013 4:57 pm
by pluggy
Thats about it, yes. That's a voltage divider, but in that case its for reducing 5V to ~3.3V levels.
Re: LED Status On/Off GPIO Input Reading
Posted: Thu May 30, 2013 5:48 pm
by bryanhun
The voltage divider works great.. thank you! It reduced the 9v to 2.8v and triggers the gpio input correctly.
Do I also need to run the ground wire from the device (as I do with the 9v battery)?
Also, what if there is a spike in voltage from the device over 9v? Do I need something like a optocoupler or will the voltage divider be sufficient?
Re: LED Status On/Off GPIO Input Reading
Posted: Thu May 30, 2013 9:45 pm
by sprinkmeier
Depends on the size of the spike you're expecting.
If you're expecting something worthy of an optocoupler then you'll probably have to worry about board layout, cable runs etc. to make sure spikes don't get in through coupling or arcing.
These circuits should give reasonable protection against moderate spikes and over-voltage.
They both use a RC low-pass filter to absorb the energy from spikes, and a diode to clamp the maximum input voltage.
Component values depend on how responsive you want the thing to be; higher R/C means slower response, but better protection.
Have a go at calculating them, let me know if you get stuck.
In the end it probably depends mostly on what you have lying around...
https://en.wikipedia.org/wiki/Light-emi ... _materials
https://en.wikipedia.org/wiki/Zener_diode
https://en.wikipedia.org/wiki/RC_circuit
Re: LED Status On/Off GPIO Input Reading
Posted: Thu May 30, 2013 11:44 pm
by bryanhun
Thank you for the info sprinkmeier.
The device itself runs on 24v, but I am using a single wire that either has 0v or 9v to signal if something is over the sensor. With the voltage divider (10k and 22k) resistors, what could it handle at most? 9v?
Re: LED Status On/Off GPIO Input Reading
Posted: Fri May 31, 2013 12:06 am
by sprinkmeier
let's go with the left-hand circuit.
ignore the zenner and the cap and assume the GPIO pin has high input impedance.
with the input at 9V you'll get (9V / (10k + 22k) = 0.28 mA) through the resistive divider and (9V * 10k / (10k+22k) = 2.8V) at the GPIO pin.
Add a 3V zenner, it should add negligible load (i.e. your 2.8V won't drop much, but check to make sure you still get reliable detection).
If you get more than 9V at the input the zenner should start to conduct and clamp the GPIO pin well under 3.3V.
That was the DC analysis (i.e. the steady-state analysis where you get to ignore the capacitor)
Now we add the capacitor and do the AC analysis.
The effective resistance is (1/((1/10) + 1/22)) = 6.8K)
Add a 47uF cap and you get a time-constant of 6.8k * 47u = 0.3 seconds. Your circuit could take as much as a second to respond to the input, but a whomping great cap like that, in addition to the zenner, should make your input very safe.
Drop to a 4.7uF cap and your response time should be better than 100mS and still quite safe.
The zenner will protect you from prolonged inputs above 9V
The capacitor will protect you from sudden spikes (as will the zenner, but the cap is cheap insurance).
Re: LED Status On/Off GPIO Input Reading
Posted: Mon Jun 03, 2013 4:53 pm
by bryanhun
Thanks again sprinkmeier.
I think I understand. Using the voltage divider circuit (similar to the ultrasonic module image above), I simply can add a 3V zenner to provide some protection.. And to do that, I just need to jump it across the RasPi GPIO and RasPi GND?
Otherwise, I can use an optocoupler.. but I am not 100% clear on the wiring. Referring to the image attached, on the left would be the 9v and ground coming from the device, and on the right would be the RasPi. According to the image, I need both the +3.3V, GPIO and Ground to the Pi, is that correct? Would I replace the 2.4k with the 22k one I am using from the voltage divider circuit? Everything else would be the same I assume.

- optoin.jpg (8.58 KiB) Viewed 2709 times
Re: LED Status On/Off GPIO Input Reading
Posted: Mon Jun 03, 2013 9:55 pm
by sprinkmeier
The circuit looks good.
Check the specs to see how much voltage the LED in the optocoupler drops (say, 2V) and how much current it needs to activate the transistor (say 5mA).
R = V/I = (9V -2V) / (5mA) = 1400
I suspect you should be able to get away with a much higher resistor. Check to see what works reliably for you.
Having gone through all the trouble to get an optocoupler make sure you keep the wiring physically separate (as you've shown on your circuit) to stop EMF pickup.
You could save yourself the 10k resistor by using one of the I2C pins which has an on-board pull-up or activating the internal pull-up.