12V Input to GPIO
I have a project in which I want to be able to pick up a whole load of 12V DC inputs on my Rasberry pi. Obviously, it's maximum input is 3.3V, so I will have to do some logic shifting. What is the best way to do this (for between 20 and 40 inputs)? I thought some of the options would be relays (where the 12V powers the coil to connect the RPi input to a 3.3V supply), or a string of voltage regulators? Relays are relatively big and expensive to have 40, and usually, work to connect high power rather than low power, so would that work, or is there a better approach?
- mikronauts
- Posts: 2823
- Joined: Sat Jan 05, 2013 7:28 pm
Re: 12V Input to GPIO
use opto isolators
oliverar wrote:I have a project in which I want to be able to pick up a whole load of 12V DC inputs on my Rasberry pi. Obviously, it's maximum input is 3.3V, so I will have to do some logic shifting. What is the best way to do this (for between 20 and 40 inputs)? I thought some of the options would be relays (where the 12V powers the coil to connect the RPi input to a 3.3V supply), or a string of voltage regulators? Relays are relatively big and expensive to have 40, and usually, work to connect high power rather than low power, so would that work, or is there a better approach?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi
- mahjongg
- Forum Moderator
- Posts: 14919
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: 12V Input to GPIO
Simple resistor dividers work well.
Do all 12V DC outputs share a common GND, as that is a prerequisite for such a simple solution.
Do all 12V DC outputs share a common GND, as that is a prerequisite for such a simple solution.
Re: 12V Input to GPIO
If you know that your 12V signal is definitely either 12V or zero and it doesn't have any high voltage spikes on it, then you can use a simple voltage divider using a couple of resistors. However if you need to accept any voltage in a wide range then you need a more complex solution maybe including a comparator of some sort. If the signal is coming in from an industrial setting then you need to put some filtering on it and preferably an opto-isolator to protect the Pi.
Re: 12V Input to GPIO
If I am using an optoisolator, then presumably I can't just run 12VDC straight through the diode? So would I use a partial divider with resistors to achieve a lower voltage anyway?
- mahjongg
- Forum Moderator
- Posts: 14919
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: 12V Input to GPIO
Not really, you just need a serial resistor for each LED that is dimensioned so that the LED in the opto-isolator lights up with 12V, but full goes off with 0Volt. It depends on the specs of the LED in the isolator, but if we take a typical value of 1mA needed, and a forward voltage for the LED of 2 Volt, then the resistor should be R=V/I (or R=U/I if you are more used to using an U for voltage)oliverar wrote:If I am using an optoisolator, then presumably I can't just run 12VDC straight through the diode? So would I use a partial divider with resistors to achieve a lower voltage anyway?
U = 12-2 = 10V U/I = 10/0.001 = 10K.
On the receiving end I would also add a pullup resistor, also of about 10K, and a 1K resistor between pullup/opto_isolator_collector and a GPIO pin. Latter resistor for safety in case you incidentally program that GPIO as output, and the opto-isolator outputs a low.
Re: 12V Input to GPIO
OK, thank you. So for a 24V signal, I would do something like this:
24V - 2V = 22V
22 = 0.001 * R
R = 22/0.001 = 22K
which would look like:

Is that correct?
24V - 2V = 22V
22 = 0.001 * R
R = 22/0.001 = 22K
which would look like:

Is that correct?
Re: 12V Input to GPIO
I think you should:
1) Connect the emitters of the opto's output transistors (With the arrows on) to 0v.
2) Connect each output transistor's collector to a 1K resistor.
3) Connect the other ends of those resistors to +3.3v.
3) Connect the junctions of the collectors and 1K resistors to your GPIO inputs.
You need the resistor there to pull the output voltage high when the transistor is off. The transistor will pull the output low when it is on.
1) Connect the emitters of the opto's output transistors (With the arrows on) to 0v.
2) Connect each output transistor's collector to a 1K resistor.
3) Connect the other ends of those resistors to +3.3v.
3) Connect the junctions of the collectors and 1K resistors to your GPIO inputs.
You need the resistor there to pull the output voltage high when the transistor is off. The transistor will pull the output low when it is on.
Slava Ukrayini.
Re: 12V Input to GPIO
More like this?


Re: 12V Input to GPIO
Looks good. That 1k could be 10k to save some power if you like. Value not critical.
Thinking about it now, don't some/all of the Pi inputs have internal pull up resistors you can enable from software. Thus the external pull up would not be needed.
Thinking about it now, don't some/all of the Pi inputs have internal pull up resistors you can enable from software. Thus the external pull up would not be needed.
Slava Ukrayini.
- mahjongg
- Forum Moderator
- Posts: 14919
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: 12V Input to GPIO
In this case you don't need to tie the two GND's of the above schematic diagram together, they can be electrically different signals.
Internall pullups are very weak, say 50K, so it would be a good idea to still add external pullups (1K to 10K) to 3.3V.
Internall pullups are very weak, say 50K, so it would be a good idea to still add external pullups (1K to 10K) to 3.3V.
- davidcoton
- Posts: 7001
- Joined: Mon Sep 01, 2014 2:37 pm
- Location: Cambridge, UK
Re: 12V Input to GPIO
At the risk of further confusion, I would use 10K for the pull-up (to save power from the 3V3 supply), and I would also put 1K in series with the GPIO pin to prevent damage if it is accidentally programmed as an output and high while the 24V signal is on.
Location: 345th cell on the right of the 210th row of L2 cache
- mahjongg
- Forum Moderator
- Posts: 14919
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: 12V Input to GPIO
So exactly like I said earlier!davidcoton wrote:At the risk of further confusion, I would use 10K for the pull-up (to save power from the 3V3 supply), and I would also put 1K in series with the GPIO pin to prevent damage if it is accidentally programmed as an output and high while the 24V signal is on.
mahjongg wrote: On the receiving end I would also add a pullup resistor, also of about 10K, and a 1K resistor between pullup/opto_isolator_collector and a GPIO pin. Latter resistor for safety in case you incidentally program that GPIO as output, and the opto-isolator outputs a low.

Re: 12V Input to GPIO
So like this:
- davidcoton
- Posts: 7001
- Joined: Mon Sep 01, 2014 2:37 pm
- Location: Cambridge, UK
Re: 12V Input to GPIO
The circle of Life! I forgot you had said that in this threadmahjongg wrote: So exactly like I said earlier! ...

Now all we need is to learn to draw conventional circuit diagrams with Gnd at the bottom and +V at the top! (Just joking, the diagrams are perfectly clear.)
Location: 345th cell on the right of the 210th row of L2 cache
Re: 12V Input to GPIO
As a final flourish can we put a LED in series with that 10K pull up. Probably reducing the 10K value.
Just so that we can see when the input is activated.
Just so that we can see when the input is activated.
Slava Ukrayini.
- davidcoton
- Posts: 7001
- Joined: Mon Sep 01, 2014 2:37 pm
- Location: Cambridge, UK
Re: 12V Input to GPIO
We can, but it might not do what we want. The voltage drop across the LED might be too great to pull the GPIO input high.Heater wrote:As a final flourish can we put a LED in series with that 10K pull up. Probably reducing the 10K value.
Just so that we can see when the input is activated.
Location: 345th cell on the right of the 210th row of L2 cache
- FTrevorGowen
- Forum Moderator
- Posts: 7153
- Joined: Mon Mar 04, 2013 6:12 pm
- Location: Bristol, U.K.
Re: 12V Input to GPIO
Depends upon the LED colour of course - IIRC, in a thread from a year or two ago, a blue LED was used (+ a series resistor) to "clamp" the Pi's serial input voltage (from a higher voltage "RS232" serial source).davidcoton wrote:We can, but it might not do what we want. The voltage drop across the LED might be too great to pull the GPIO input high.Heater wrote:As a final flourish can we put a LED in series with that 10K pull up. Probably reducing the 10K value.
Just so that we can see when the input is activated.
Trev.
Begining to test Bullseye on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B, B+, and a A+) and Pi's with cameras with Buster on the P3B+, some P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm
Re: 12V Input to GPIO
davidcotton,
Or put the LED accros the optos emitter and collector. Although that will now be off when the input is on.
Or put the LED in series with the 22K resistor on the input.
Happens I was just toying with some 4mm LEDs in my junk box and measured the following voltage drops:
Green (old low efficiency LED) - 2v
Red (new high efficiency) - 1.8v
White - 2.6v
Sadly I don't have any blue or yellow to hand.
The pull up can go to 5 volts instead of 3.3 volts. The voltage drop of the LED will prevent the GPIO seeing more than 3.3 volts. It will pull high enough to register as a HIGH on the GPIO.We can, but it might not do what we want. The voltage drop across the LED might be too great to pull the GPIO input high.
Or put the LED accros the optos emitter and collector. Although that will now be off when the input is on.
Or put the LED in series with the 22K resistor on the input.
Happens I was just toying with some 4mm LEDs in my junk box and measured the following voltage drops:
Green (old low efficiency LED) - 2v
Red (new high efficiency) - 1.8v
White - 2.6v
Sadly I don't have any blue or yellow to hand.
Slava Ukrayini.
Re: 12V Input to GPIO
Put them in the correct order of colour and you can guestimates where yellow will be.
The white you have is most likely a blue led with a phosphorus coating.
The white you have is most likely a blue led with a phosphorus coating.
- mahjongg
- Forum Moderator
- Posts: 14919
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: 12V Input to GPIO
Nope, instead you will blow up your PI when you do, the LED only has a forward voltage if any current runs through it. Without that current no voltage will fall over it, and the PI WILL get 5V on a GPIO, and yes, that might be enough to trigger a latchup which will destroy your PI! Do NOT take that risk.Heater wrote: The pull up can go to 5 volts instead of 3.3 volts. The voltage drop of the LED will prevent the GPIO seeing more than 3.3 volts. It will pull high enough to register as a HIGH on the GPIO.
Putting the LED in series with the LED in the optocouplers-isolator is a much better idea.
Re: 12V Input to GPIO
mahjongg,
Hmm...I expect the GPIO has the usual diode protection built in. I which case:
1) The LED is not lit. There is very little current through it possible. The diode protection can handle it.
2) The LED is lit. Current does flow. But then the LED drops the volts and the GPIO is safe.
But yes, you are right. I have not tried this. It's out of spec. Don't do it. Try at your own risk.
Hmm...I expect the GPIO has the usual diode protection built in. I which case:
1) The LED is not lit. There is very little current through it possible. The diode protection can handle it.
2) The LED is lit. Current does flow. But then the LED drops the volts and the GPIO is safe.
But yes, you are right. I have not tried this. It's out of spec. Don't do it. Try at your own risk.
Slava Ukrayini.
Re: 12V Input to GPIO
I thought we'd had this discussion before, though I can't now find it, but from what I remember, there ain't no diode protection, there's just some rather poor parasitic diodes, not really capable of protecting anything.
Also your description "not lit" is somewhat vague - depending on the ambient lighting, and whether my eyes are dark-adapted, even 1mA through a LED could be described as "not lit" when compared to the light from 10 or 20mA through it.
Also your description "not lit" is somewhat vague - depending on the ambient lighting, and whether my eyes are dark-adapted, even 1mA through a LED could be described as "not lit" when compared to the light from 10 or 20mA through it.