oliverar
Posts: 5
Joined: Thu Feb 02, 2017 4:01 pm

12V Input to GPIO

Thu Feb 02, 2017 4:20 pm

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?

User avatar
mikronauts
Posts: 2694
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: 12V Input to GPIO

Thu Feb 02, 2017 4:29 pm

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

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11671
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: 12V Input to GPIO

Thu Feb 02, 2017 4:30 pm

Simple resistor dividers work well.
Do all 12V DC outputs share a common GND, as that is a prerequisite for such a simple solution.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4249
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: 12V Input to GPIO

Thu Feb 02, 2017 4:31 pm

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.

oliverar
Posts: 5
Joined: Thu Feb 02, 2017 4:01 pm

Re: 12V Input to GPIO

Thu Feb 02, 2017 6:06 pm

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?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11671
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: 12V Input to GPIO

Thu Feb 02, 2017 6:25 pm

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?
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)
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.

oliverar
Posts: 5
Joined: Thu Feb 02, 2017 4:01 pm

Re: 12V Input to GPIO

Fri Feb 03, 2017 11:26 am

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:

Image

Is that correct?

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Fri Feb 03, 2017 11:41 am

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.

oliverar
Posts: 5
Joined: Thu Feb 02, 2017 4:01 pm

Re: 12V Input to GPIO

Fri Feb 03, 2017 11:51 am

More like this?

Image

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Fri Feb 03, 2017 12:24 pm

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.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11671
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: 12V Input to GPIO

Fri Feb 03, 2017 12:33 pm

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.

User avatar
davidcoton
Posts: 3656
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: 12V Input to GPIO

Fri Feb 03, 2017 4:33 pm

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.
Signature retired

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Fri Feb 03, 2017 6:12 pm

Good point.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11671
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: 12V Input to GPIO

Fri Feb 03, 2017 6:31 pm

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.
So exactly like I said earlier!
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.
:lol:

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4249
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: 12V Input to GPIO

Fri Feb 03, 2017 9:01 pm

So like this:
tmp.png
tmp.png (11.14 KiB) Viewed 8083 times

User avatar
davidcoton
Posts: 3656
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: 12V Input to GPIO

Fri Feb 03, 2017 9:03 pm

mahjongg wrote: So exactly like I said earlier! ... :lol:
The circle of Life! I forgot you had said that in this thread :roll:
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.)
Signature retired

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Fri Feb 03, 2017 10:15 pm

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.

User avatar
davidcoton
Posts: 3656
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: 12V Input to GPIO

Fri Feb 03, 2017 11:29 pm

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.
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.
Signature retired

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 4859
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: 12V Input to GPIO

Sat Feb 04, 2017 11:23 am

davidcoton wrote:
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.
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.
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).
Trev.
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my 2xP3A+, P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Sat Feb 04, 2017 12:39 pm

davidcotton,
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.
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.

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.

User avatar
bensimmo
Posts: 3892
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: 12V Input to GPIO

Sat Feb 04, 2017 1:33 pm

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.

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Sat Feb 04, 2017 3:49 pm

Good point.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11671
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: 12V Input to GPIO

Sun Feb 05, 2017 2:41 pm

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.
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.
Putting the LED in series with the LED in the optocouplers-isolator is a much better idea.

Heater
Posts: 11835
Joined: Tue Jul 17, 2012 3:02 pm

Re: 12V Input to GPIO

Sun Feb 05, 2017 6:52 pm

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.

User avatar
Burngate
Posts: 5838
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: 12V Input to GPIO

Mon Feb 06, 2017 11:21 am

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.

Return to “General discussion”