simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Avoiding using Voltage Divider

Sun Jun 23, 2013 7:12 pm

I always use a 2 resistor voltage divider to interface things like 5V ultrasonics or 5V IR Line Sensors to feed signals into the GPIO pins but I was wondering if anyone has been avoiding the extra resistor and just using a single one in series instead.

IF so, what value do you use?

regards

Simon
PS I set my inputs to be have internal pull-ups resistors - e.g. in Python GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 7:34 pm

Are there zeners to clamp the gpios to 3.3V? I think someone suggested there were. It then would be a question of what the zeners can survive. I'd have thought a milliamp would be OK. But I don't know and I'm not going to experiment.

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 7:51 pm

simplesi wrote:I set my inputs to be have internal pull-ups resistors - e.g. in Python GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)
What happens before your Python code executes? Switchable pull-ups may not be there all the time.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 8:11 pm

But I don't know and I'm not going to experiment.
That makes two of us :)

But if someone has already risked it... :)
What happens before your Python code executes? Switchable pull-ups may not be there all the time.
Who knows (well I don't anyway) :)

Its a question really for someone who'es bodged it and found it works fine

I'm only doing this to make it easier/neater to add sensors via a mini breadboard

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

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

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 8:16 pm

a series resistor limits the current that will flow through the "upper" protection diode into the 3V3 supply, thereby protecting the transistors in the GPIO for voltages higher than what they are rated for, or negative voltages.

The "upped diode" being the one who's cathode is connected to 3V3, (VCC) and who's anode is connected to the GPIO, while the "lower" protection diode is the one who's anode is connected to GND, and its cathode to the GPIO pin.
Image

As long as the current is limited to a safe value, and the extra current is (much) smaller than what is consumed by the 3V3 logic nothing disastrous should happen. But do note that if more than one GPIO "receives" current this way, it is the sum of the currents through all the protection diodes that should not exceed the total current consumption of all the 3V3 logic!
in fact if current flows into the 3V3 supply this way, the regular 3V3 regulator will simply source less current to compensate, but if this current is decreased to zero further current will just increase the 3V3 level with potential disastrous consequences.

Also, the protection diodes will dissipate energy when current flows through them, and too much of it will damage the diode!

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 8:44 pm

As long as the current is limited to a safe value
In that case, if I just have a series 2k2 resistor then the max current is going to be (5 - 3.3)/2k2 ~= 0.7mA (plus whatever the current the internal pull up add to the mix.

Sounds safe but is it?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

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

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 8:58 pm

the internal pull-up is connected to 3V3, so shouldn't add to the current, but subtract (as its parallel to the upped diode) but thats academic.

I think its safe, but don't hold me to it!

I think its safe because I don't know any other "damage mechanisms" that could be at work, but correct me if i'm wrong!

Brett
Posts: 15
Joined: Sun Jun 23, 2013 2:00 pm

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 9:20 pm

The ESD protection diodes are typically not very well spec'd inside of an IC. I wouldn't trust a single resistor to prevent blowing them up.

Since you already have pull ups, you only need to actively drive the pin to ground when you need a logic 0. How about instead of a single resistor, you use a single transistor. Your 5V signal would drive the gate and the source would be attached to ground. Now when the 5V signal goes high, the drain will drive ground, and when the 5V signal goes low the transistor will present high impedance, and the internal pull-up will take care of your logic high. Basically you've build an open drain inverter.

Gotchas:
Remember to invert the bit when you read from the GPIO.
High source-drain capacitance can make this circuit a bit slow. Don't try to pass signals that are many 10's MHz though this. Choosing a low transistor with low source-drain capacitance would probably be a good thing.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 9:39 pm

Brett wrote:The ESD protection diodes are typically not very well spec'd inside of an IC. I wouldn't trust a single resistor to prevent blowing them up.

Since you already have pull ups, you only need to actively drive the pin to ground when you need a logic 0. How about instead of a single resistor, you use a single transistor. Your 5V signal would drive the gate and the source would be attached to ground. Now when the 5V signal goes high, the drain will drive ground, and when the 5V signal goes low the transistor will present high impedance, and the internal pull-up will take care of your logic high. Basically you've build an open drain inverter.

Gotchas:
Remember to invert the bit when you read from the GPIO.
High source-drain capacitance can make this circuit a bit slow. Don't try to pass signals that are many 10's MHz though this. Choosing a low transistor with low source-drain capacitance would probably be a good thing.
Should the RPi attempt to output a 1 while the transistor is conducting bad things may well happen.
>)))'><'(((<

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

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 9:41 pm

What aTao said: remember to put a resistor in again, between the drain (collector) of the N-FET (NPN transistor) and the GPIO pin, as when the transistor is open accidentally switching the GPIO to output a "1" will otherwise still damage your GPIO.

On the topic of the protection diodes, they are normally dimensioned the same as the MOSFET's, as they are actually part of the MOSFET itself!
Image

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Avoiding using Voltage Divider

Sun Jun 23, 2013 9:49 pm

I'm also careful about tinkering around with the GPIO input pin too but I believe it should be safe to do this using a transistor which is connected to the 3.3v supply rail.
Image
Even if 5v or more was present at the base the transistor can only control the current flow between collector and emitter which is 3.3v (the resistor at the emitter is for pull-down).
The other alternative is to use a opto-isolator / opto-coupler but that's a bit of an over kill for a 5v signal in my opinion.

Richard S.

Brett
Posts: 15
Joined: Sun Jun 23, 2013 2:00 pm

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 12:12 am

aTao wrote: Should the RPi attempt to output a 1 while the transistor is conducting bad things may well happen.
Excellent point. I'm used to working with bare metal processors on boards of our own design, so for my of my experience we have total control of IO direction at all times. In others words, "Don't do that" is adequate protection for us. Not to mention we can fix our boards when we blow them up :)

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 6:37 am

Interesting stuff :)

But all I was seeking to achieve was to save wiring one resistor :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

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

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 8:35 am

redhawk wrote:I'm also careful about tinkering around with the GPIO input pin too but I believe it should be safe to do this using a transistor which is connected to the 3.3v supply rail.
Image
Even if 5v or more was present at the base the transistor can only control the current flow between collector and emitter which is 3.3v (the resistor at the emitter is for pull-down).
The other alternative is to use a opto-isolator / opto-coupler but that's a bit of an over kill for a 5v signal in my opinion.

Richard S.
here again is there no protection to short circuit currents when the transistor is turned on, and you accidentally output a "0".

all you really need to make the GPIO's 5V tolerant is a series resistor, and optionally a zener or divider resistor.

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 8:51 am

Can someone confirm if any of the GPIO lines are guaranteed to reset to a specific state (input?) and stay there throughout booting of any official Raspbian release?

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 8:56 am

If you have the pull-ups to +3V3 enabled, wouldn't a single diode do the job ?
It effectively makes the driving signal into an "open collector" output so that it can only sink current when the output is at 0V and not source current it when the output is at +5V.

PeterO


PS: I don't believe the emitter follower circuit suggested above will behave as expected with with +5V on the input, as the base collector junction will no longer be reverse biased and the base emitter junction will try to pull the emitter up to +5V.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 10:37 am

here again is there no protection to short circuit currents when the transistor is turned on, and you accidentally output a "0"/
What short circuit currents??

When the transistor is on the 3.3v rail at the collect will flow into the GPIO input from the emitter, when the transistor is off the pull-down resistor would ensure 0v is present at the GPIO input.
The pull-down resistor would have to be a appropriate value if the GPIO is used for output mode but I fail to see how this would be a serious problem.

As it happens I did a search today for logic level converts and found the following circuit which incidentally wasn't too much different from my own - https://tylerjones.me/2012/11/howto-ras ... uart-pins/

It's been a long time since I've experiment with this arrangement but I remember putting 9v between ground and base but the output was still 3.3v. :)
Of course if you damage the transistor due to abuse then anything is possible just like a dry solder joint, burnt out resistor or cracked diode.

Richard S.

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 10:43 am

redhawk wrote:What short circuit currents??
If the input turns on the transistor and the Pi turn on the gpio as output low then there is a short from 3.3, through your transistor, into the gpio pin and to ground via the Pio output gate.

Not a problem if it can be guaranteed that the gpio is never configured as output and set low, but is there such a guarantee?

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 10:49 am

Image

The left circuit will only drive 3.3 to the Arduino, since as the emitter rises to 3.3 the B-E junction is no longer forward biased and the C-E path turns off. It's safe and should work, since 3.3 will, I think, exceed the threshold on the Arduino input. Still, the circuit isn't really doing what the designer thinks it is. It is basically a B-E series diode (you could disconnect the collector) that will prevent 5V at the Arduino end reverse driving the Pi output.

The right circuit has a risk of shorting if the gpio is configured as output low.

If the Arduino Rx pin can be configured as output that also risks a damaging short.

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 2:50 pm

Image

The right hand circuit is almost certainly not operating as he thinks....

With +5V on the input the base-emitter and base-collector junctions in the 2N2222 are forward biased, the device is NOT acting as a transistor, but as a pair of diodes.

The current flowing into the base could be flowing out of the emitter and collector connections. The collector is held at 3V3 by the supply so the base can't rise above 3.3+0.6 = 3.9V, but note the b-c junction is not designed to operate in forward biased mode. The emitter is held at 3.3+0.6= 3.9 by the GPIO protection diodes (assuming they are Si diodes) so that would try to limit the base to 3.9+0.6 = 4.5V.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 3:31 pm

PeterO wrote: The right hand circuit is almost certainly not operating as he thinks....

With +5V on the input the base-emitter and base-collector junctions in the 2N2222 are forward biased, the device is NOT acting as a transistor, but as a pair of diodes.
Yep, a pair of diodes.Current will flow to the 3.3V rail through the B-C junction. Probably not disastrous, with the 1K resistor in-line. Maybe 1mA, but not good design!

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 4:06 pm

Getting back to the OP :)
all you really need to make the GPIO's 5V tolerant is a series resistor
Really need solution is what I'm after :)

Any recommended max/min resistor values?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 4:27 pm

The best solution and safest would be to use opto-isolators - http://www.ebay.co.uk/itm/5x-4N25-Optoi ... 811911?pt=
The transistor side would be driven via the 3.3v supply rail, the LED side would be connected to your sensor circuit.
Since both parts are isolated there is no need to worry about 5v entering the Pi just make sure you've wired up the components with the appropriate current limiting resistors.
Opto-couplers / opto-isolators have been mentioned many times on this forum I'm sure there are handful of useful circuit diagrams floating around.

Richard S.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 5:04 pm

all you really need to make the GPIO's 5V tolerant is a series resistor
That is somewhat true. The problem with 5V coming in is that the ESD protection diodes start conducting. They will feed current into the 3V3 rail.
Those protection diodes are designed for high peak currents but for an extremely short period of time.
To make them survive for an infinite period of time you have to reduce the current.
Lets' says you add a 47K series resistor. You then have a diode+resistor with 5V on one side and 3V3 at the other side.
Let's take a standard voltage drop of 0.6V over the diode. You resistor then has 5-3.3-0.6 = 1.1Volt over it.
With a 47K resistor you have ~23uA (micro!) amps going through the resistor and through the diode.
The energy the diode has to cope with is ~25uW. It will survive that.
But the input of the pad will continuous see about 3.3+0.6 = 3.9V. Not ideal but again it is likely to survive that.
I am an old-fashioned engineer, for the sake of a $0.01 resistor I would not take the risk so I always use two resistors.
There are cases where you can not use two resistors but I leave that as 'exercise for the reader'.

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: Avoiding using Voltage Divider

Mon Jun 24, 2013 6:21 pm

Things to note as important are lower input current gives a slower input logic switching time. It would perhaps be better to use 2 resistors to avoid many problems. A Zener diode rated at 2V would perhaps work well for some things (1A Zener series, connected reverse bias). This is probly the most effective single component solution to voltage dropping.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

Return to “Automation, sensing and robotics”