I want to trigger a gpio pin with 24v. My idea is to use a relay that will close when 24v is applied to the coil and connect the 3v pin on the pi to one of the open gpio. I figure that should be pi safe.
Will this work?
Is there a better way?
Could I use a transistor? (If so how)
Do I need some type of circuit protection?
Thanks for your help.
- tedhale
- Posts: 114
- Joined: Thu Sep 20, 2012 4:52 pm
- Location: Williamsburg, VA, USA
- Contact: Website
Re: how to trigger gpio pin with 24v
I suggest a simple voltage divider
http://en.wikipedia.org/wiki/Voltage_divider
use resistors of 1.5K and 10K and you can drop the 24v to 3.1v
the current will be greatly limited as well.
Since I don;t know what your input current is, I suggest you measure it before and after the divider to see if it is safe. Add an additional resistor after the divider if needed.
http://en.wikipedia.org/wiki/Voltage_divider
use resistors of 1.5K and 10K and you can drop the 24v to 3.1v
the current will be greatly limited as well.
Since I don;t know what your input current is, I suggest you measure it before and after the divider to see if it is safe. Add an additional resistor after the divider if needed.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com
http://raspberrypihobbyist.blogspot.com
- yv1hx
- Posts: 383
- Joined: Sat Jul 21, 2012 10:09 pm
- Location: Now an expatriate, originally from Zulia, Venezuela
- Contact: Website Skype
Re: how to trigger gpio pin with 24v
The best and more safe way is using a opto coupler between the GPIO and the external circuit, specially if the external input monitored is related to a coil or a AC circuit.jay123035 wrote:I want to trigger a gpio pin with 24v. My idea is to use a relay that will close when 24v is applied to the coil and connect the 3v pin on the pi to one of the open gpio. I figure that should be pi safe.
Will this work?
Is there a better way?
Could I use a transistor? (If so how)
Do I need some type of circuit protection?
Thanks for your help.
Most cheaper opto couplers can give insulation in the order of 2KV to 3KV.
Here is some examples:
https://dl.dropboxusercontent.com/u/183 ... 11AG1M.pdf
https://dl.dropboxusercontent.com/u/18334341/4n25.pdf
http://electronica.yoreparo.com/foros/f ... 10v_ac.jpg
When working with AC mains, be very careful, the mistakes can be costly and dangerous!
Good luck!
Marco-Luis
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Re: how to trigger gpio pin with 24v
just to clarify. if the 24v side of a optocoupler is closed/powered the 3v side will be closed too.
my voltage is DC.
my voltage is DC.
- yv1hx
- Posts: 383
- Joined: Sat Jul 21, 2012 10:09 pm
- Location: Now an expatriate, originally from Zulia, Venezuela
- Contact: Website Skype
Re: how to trigger gpio pin with 24v
Can be both ways, only depends of how you build the circuit, hence depends of yours electronics skills.jay123035 wrote:just to clarify. if the 24v side of a optocoupler is closed/powered the 3v side will be closed too.
my voltage is DC.
The optocoupler transistor needs to be polarized according what you are looking for, in most circuits you should note a resistor in series with the emitter or the collector, to limit the current flow trough the transistor to a safe level ensuring the proper operation of the device.
Please have a look at this other example: http://www.8051projects.net/e107_files/ ... 4178_1.jpg
With the Configuration 1 example, you can connect the RPi GPIO direct to the Q1 Emitter and the R2 junction, however there is some situations when the output can not be exactly detected by the GPIO input, that is when the GPIO is not sensing the proper voltage to lock in the "high" or the "low" state.
If the voltage falls (and remains) between the "high" and "low" voltage level thresholds, the GPIO can lead to unexpected behavior.
The way to overcome this situation is inserting a Schmitt trigger gate (can be an Inverter, or any other logic gate, according to your needs) between the phototransistor output and the GPIO.
References:
http://en.wikipedia.org/wiki/Schmitt_trigger
http://en.wikipedia.org/wiki/Optocouplers
http://www.nxp.com/documents/data_sheet/74HC_HCT14.pdf
http://www.datasheetcatalog.org/datashe ... 357_DS.pdf
Marco-Luis
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
- tedhale
- Posts: 114
- Joined: Thu Sep 20, 2012 4:52 pm
- Location: Williamsburg, VA, USA
- Contact: Website
Re: how to trigger gpio pin with 24v
check out
http://raspberrypihobbyist.blogspot.com ... it_19.html
you would likely need a different optocoupler than I used.
http://raspberrypihobbyist.blogspot.com ... it_19.html
you would likely need a different optocoupler than I used.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com
http://raspberrypihobbyist.blogspot.com
Re: how to trigger gpio pin with 24v
thank you all for your help. i am going to use a octocoupler and an and gate so that the pi sees a clean/stedy singnal.
i will post a circut digram latter this week.
thanks
i will post a circut digram latter this week.
thanks
- yv1hx
- Posts: 383
- Joined: Sat Jul 21, 2012 10:09 pm
- Location: Now an expatriate, originally from Zulia, Venezuela
- Contact: Website Skype
Re: how to trigger gpio pin with 24v
Good luck!jay123035 wrote:thank you all for your help. i am going to use a octocoupler and an and gate so that the pi sees a clean/stedy singnal.
i will post a circut digram latter this week.
thanks

Marco-Luis
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Re: how to trigger gpio pin with 24v
update:
i have decided/hope to use a optocoupler but am having a hard time finding one that will work for me.The big issue is finding one that can take 24v across the anode/cathode and 3 across the emitter/collector.
Is the "forward diode voltage" the one i need to be 24V?
What about the "reverse diode voltage"? i shouldn't have any reverse needs
Isolation Voltage? Vrms?
thanks
i have decided/hope to use a optocoupler but am having a hard time finding one that will work for me.The big issue is finding one that can take 24v across the anode/cathode and 3 across the emitter/collector.
Is the "forward diode voltage" the one i need to be 24V?
What about the "reverse diode voltage"? i shouldn't have any reverse needs
Isolation Voltage? Vrms?
thanks
- theoriginalxbrav
- Posts: 41
- Joined: Sun May 12, 2013 7:16 pm
Re: how to trigger gpio pin with 24v
Doing the reverse (triggering 24V with 3.3V), I've just used N Channel MOSFETs. What field device are you using?
Projects:
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf
Re: how to trigger gpio pin with 24v
theoriginalxbrav wrote:Doing the reverse (triggering 24V with 3.3V), I've just used N Channel MOSFETs. What field device are you using?
the MOSFET looks interesting. and most importantly available in a relevant voltage for me.
my source is 12v or 24v circuit on a 40,000 ton forklift.
I would still like an opto isolator as it seams less likely to short/fry the Pi if it breaks. MOSFET being a physical connection could leak 24 into the 3.3v side. (maybe a fuse or something could protect from that, idk)
- theoriginalxbrav
- Posts: 41
- Joined: Sun May 12, 2013 7:16 pm
Re: how to trigger gpio pin with 24v
The problem with 24V is that there aren't really any opto-couplers I've seen that would give you a low resistance for 3.3V.jay123035 wrote:theoriginalxbrav wrote:Doing the reverse (triggering 24V with 3.3V), I've just used N Channel MOSFETs. What field device are you using?
the MOSFET looks interesting. and most importantly available in a relevant voltage for me.
my source is 12v or 24v circuit on a 40,000 ton forklift.
I would still like an opto isolator as it seams less likely to short/fry the Pi if it breaks. MOSFET being a physical connection could leak 24 into the 3.3v side. (maybe a fuse or something could protect from that, idk)
What I would do is locate a level shifter that can pull 24V down to 5V / 3.3V, followed by opto-isolator. First chip that comes to mind (but only works with less than 20 volts): http://www.ti.com/lit/ds/symlink/cd4504b.pdf
Projects:
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf
Re: how to trigger gpio pin with 24v
any ideas on the resister placement? (IDK were to even start on this, needs to work with 12v & 24v)
Re: how to trigger gpio pin with 24v
May I suggest this solution:
use optocoupler 4N28 or similar. it will cost you about $0.25 if you only buy a few.
connect 10k resistor from GPIO to GND
connect collector/emitter of optocoupler to 3.3V/GPIO
connect 6.8k resistor in series with diode of optocoupler
this should give you 3.3mA in the diode and 0.25mA in the transistor (approximately),
which means that the optocoupler can pull the GPIO up to 2.9V if you apply 24V to the diode+resistor
that is a good margin.
the simulation shows Input signal from 24V and output signal to GPIO
use optocoupler 4N28 or similar. it will cost you about $0.25 if you only buy a few.
connect 10k resistor from GPIO to GND
connect collector/emitter of optocoupler to 3.3V/GPIO
connect 6.8k resistor in series with diode of optocoupler
this should give you 3.3mA in the diode and 0.25mA in the transistor (approximately),
which means that the optocoupler can pull the GPIO up to 2.9V if you apply 24V to the diode+resistor
that is a good margin.
the simulation shows Input signal from 24V and output signal to GPIO
- Attachments
-
- LTSpice schematic
- opto.jpg (33.76 KiB) Viewed 47580 times
Re: how to trigger gpio pin with 24v
you rock and your idea rocks. I spent hours looking for an octcocoupler that could handle 24v.May I suggest this solution:
use optocoupler 4N28 or similar. it will cost you about $0.25 if you only buy a few.
connect 10k resistor from GPIO to GND
connect collector/emitter of optocoupler to 3.3V/GPIO
connect 6.8k resistor in series with diode of optocoupler
this should give you 3.3mA in the diode and 0.25mA in the transistor (approximately),
which means that the optocoupler can pull the GPIO up to 2.9V if you apply 24V to the diode+resistor
that is a good margin.
some Qs
Q1 does a 4n28 handle 24v across the diode or is the resistor what makes this work?
Q2:can this circuit stay "hot" for 1hr real world / 24/7 safety? or would something overheat (proper ventilation provided)
Q3: my GPIOin is the "out" just above R1?
i need a circuit that can easily be switched from a 24v and 12v (automotive voltages) source.
Q4a: is 3.4k a good value for a 12v side?
Q4b: any ideas/directions to point in for a circuit that could switch this automatically?
thanks for the feedback
Re: how to trigger gpio pin with 24v
the attached schematic shows how you can get the optocoupler input current to stay constant as the input voltage is changing. It should work in the range of 8V-40V or more.
the zener diode sets 6.2V on the base of the npn, so the voltage across the 1.5k resistor is constant at 5.5V even as input voltage changes. this is a constant current circuit. the npn is taking the heat as input voltage changes, but at 3.6mA there is not much of power dissipation. nothing that will heat up so you notice.
the zener diode sets 6.2V on the base of the npn, so the voltage across the 1.5k resistor is constant at 5.5V even as input voltage changes. this is a constant current circuit. the npn is taking the heat as input voltage changes, but at 3.6mA there is not much of power dissipation. nothing that will heat up so you notice.
- Attachments
-
- opto with constant current feed
- opto2.jpg (30.37 KiB) Viewed 47538 times
Re: how to trigger gpio pin with 24v
Tage wrote:the attached schematic shows how you can get the optocoupler input current to stay constant as the input voltage is changing. It should work in the range of 8V-40V or more.
the zener diode sets 6.2V on the base of the npn, so the voltage across the 1.5k resistor is constant at 5.5V even as input voltage changes. this is a constant current circuit. the npn is taking the heat as input voltage changes, but at 3.6mA there is not much of power dissipation. nothing that will heat up so you notice.
this rocks, thank you.
the voltage wont change after installation. but the circuit could be installed in ether a 12v or 24v environment. (i dont think that distinction will effect this circuit?)
thanks for your help i have a feeling this thread will be useful to others.
thanks to everyone who helped. i have learned a lot more than i would like to admit.
- yv1hx
- Posts: 383
- Joined: Sat Jul 21, 2012 10:09 pm
- Location: Now an expatriate, originally from Zulia, Venezuela
- Contact: Website Skype
Re: how to trigger gpio pin with 24v
jay123035:jay123035 wrote:Tage wrote:the attached schematic shows how you can get the optocoupler input current to stay constant as the input voltage is changing. It should work in the range of 8V-40V or more.
the zener diode sets 6.2V on the base of the npn, so the voltage across the 1.5k resistor is constant at 5.5V even as input voltage changes. this is a constant current circuit. the npn is taking the heat as input voltage changes, but at 3.6mA there is not much of power dissipation. nothing that will heat up so you notice.
this rocks, thank you.
the voltage wont change after installation. but the circuit could be installed in ether a 12v or 24v environment. (i dont think that distinction will effect this circuit?)
thanks for your help i have a feeling this thread will be useful to others.
thanks to everyone who helped. i have learned a lot more than i would like to admit.
I agree totally with the solution proposed by Tage, (I've used the 4N26 ~ 4N28 for ages), this circuit can handle superiors voltage levels if you calculate accordingly the series resistor with the diode, as long you don't exceed the maximum current capable of handling the led. (Most opto couplers can handle up to 10 ~ 15 ma forward current, you should check this threshold level in the spec sheet of the device that you are planning to use.
Marco-Luis
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Telecom Specialist (Now Available for Hire!)
http://www.meteoven.org
http://twitter.com/yv1hx
Re: how to trigger gpio pin with 24v
I had a look at the datasheet for 4N28 opto coupler, and it states that the maximum diode current at room temp is 60mA.
if reverse voltage is applied to the diode (input) it can not stand more than 5V. you should not see any reverse voltage with this schematic. at room temp you should expect that the voltage across the input is just over 1V at 3.6mA. the rest of the input voltage is taken by the npn transistor and the resistor.
The isolation voltage is 5000Vac.this is how much voltage it can take between input and output.
The current transfer ratio at 1mA input current is about 7% typical. this means that if you drive with 1mA you can not expect that the output can drive more than 75uA while keeping the collector-emitter voltage below 0.4V.
opto couplers age, so this will get worse over time. usually a designer would double the diode current to account for aging.
good luck!
if reverse voltage is applied to the diode (input) it can not stand more than 5V. you should not see any reverse voltage with this schematic. at room temp you should expect that the voltage across the input is just over 1V at 3.6mA. the rest of the input voltage is taken by the npn transistor and the resistor.
The isolation voltage is 5000Vac.this is how much voltage it can take between input and output.
The current transfer ratio at 1mA input current is about 7% typical. this means that if you drive with 1mA you can not expect that the output can drive more than 75uA while keeping the collector-emitter voltage below 0.4V.
opto couplers age, so this will get worse over time. usually a designer would double the diode current to account for aging.
good luck!
Re: how to trigger gpio pin with 24v
I need to run 4 of these circuits. Can I run all for off one D1? and one R1?Tage wrote:the attached schematic shows how you can get the optocoupler input current to stay constant as the input voltage is changing. It should work in the range of 8V-40V or more.
the zener diode sets 6.2V on the base of the npn, so the voltage across the 1.5k resistor is constant at 5.5V even as input voltage changes. this is a constant current circuit. the npn is taking the heat as input voltage changes, but at 3.6mA there is not much of power dissipation. nothing that will heat up so you notice.
- mahjongg
- Forum Moderator
- Posts: 13555
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: how to trigger gpio pin with 24v
in principle yes, but you sometimes may have to lower the value of the resistor a bit, so that it can provide the current for three more base inputs, but as these currents are quite small, compared to the current running through the zener it may turn out to not be necessary, depending on the input range you need.
with an emitter current of 3.6mA, and a typical (minimum) gain of 100 times, this means the base current is just 0.036mA, so in total just 0.144 mA of base current is needed for four transistors.
The 10K resistor will get (at 24V input) 24 - 6.2 = 17,8 V, so there will flow 17.8 / 10K = 1,78mA through it.
Thats is so much that the 0.144 mA doesn't make a big difference, so the 10K is fine, and doesn't have to be lowered.
but in its lowest (8V) input range there will be just 8 - 6.2V = 1.8V over the 10K, so there is just capacity for 1.8V / 10K = 0.18 mA, and then subtracting 0.144 mA leaves just 0.036mA for the zener, which will be too little.
So if you need the 8V lower range you should lower the resistor value, but then you need to calculate if at the maximum input voltage the zener doesn't need to dissipate too much.
with an emitter current of 3.6mA, and a typical (minimum) gain of 100 times, this means the base current is just 0.036mA, so in total just 0.144 mA of base current is needed for four transistors.
The 10K resistor will get (at 24V input) 24 - 6.2 = 17,8 V, so there will flow 17.8 / 10K = 1,78mA through it.
Thats is so much that the 0.144 mA doesn't make a big difference, so the 10K is fine, and doesn't have to be lowered.
but in its lowest (8V) input range there will be just 8 - 6.2V = 1.8V over the 10K, so there is just capacity for 1.8V / 10K = 0.18 mA, and then subtracting 0.144 mA leaves just 0.036mA for the zener, which will be too little.
So if you need the 8V lower range you should lower the resistor value, but then you need to calculate if at the maximum input voltage the zener doesn't need to dissipate too much.
-
- Posts: 10
- Joined: Mon Dec 23, 2013 8:13 pm
Re: how to trigger gpio pin with 24v
This nice. I use this design also for a while, for pic and msp430.
Now I want to use it with raspberryPi and have 6 of these running.
Now I am trying to figure out how to extend this design with a front part low pass filter.
I would love, actualy, I need to get rid of al sparks, spikes, noice smaller then 10ms. Any concrete suggestions? The length that I need I will test in software.
Kind regards
Now I want to use it with raspberryPi and have 6 of these running.
Now I am trying to figure out how to extend this design with a front part low pass filter.
I would love, actualy, I need to get rid of al sparks, spikes, noice smaller then 10ms. Any concrete suggestions? The length that I need I will test in software.
Kind regards
- Richard-TX
- Posts: 1549
- Joined: Tue May 28, 2013 3:24 pm
- Location: North Texas
Re: how to trigger gpio pin with 24v
yv1hx wrote: The best and more safe way is using a opto coupler between the GPIO and the external circuit

+1
I would not trust a voltage divider under any circumstances. One resistor changes value or an open occurs and the Rpi goes "POOF".
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip
Re: how to trigger gpio pin with 24v
Sorry to revive an old thread from the dead but I am in the process of doing something like this and had a few questions for the experts.
What is the advantage of doing this vs using a Relay to be powered from 24v external source and just having it act like a Normally open contact on the GPIO side ?
I also found this post: https://123d.circuits.io/circuits/27512 ... #schematic
where the user states he followed this link but in his components lists a 100kohm resistor
Can someone explain this ?
What is the advantage of doing this vs using a Relay to be powered from 24v external source and just having it act like a Normally open contact on the GPIO side ?
I also found this post: https://123d.circuits.io/circuits/27512 ... #schematic
where the user states he followed this link but in his components lists a 100kohm resistor

- mahjongg
- Forum Moderator
- Posts: 13555
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: how to trigger gpio pin with 24v
Nothing much, except price perhaps, and its smaller, and the 24V is less loaded, by a factor 100 at least.What is the advantage of doing this vs using a Relay to be powered from 24v external source and just having it act like a Normally open contact on the GPIO side ?
yes, the 100K is probably connected to the base of the phototransistor (pin 6), and GND, that makes the phototransistor less sensitive.I also found this post: https://123d.circuits.io/circuits/27512 ... #schematic
where the user states he followed this link but in his components lists a 100kohm resistorCan someone explain this ?