Pijus_magnificus
Posts: 2
Joined: Tue Nov 13, 2012 10:32 pm

How to sense a 5v input with GPIO

Tue Nov 13, 2012 10:43 pm

Hi there

Only a fast question. I would like to sense a 5v input with my pi using the GPIO. I know that I need to limit the current and drop down the volts to 3.3v. And my question is:

Is enought with a simple resistive divisor like this one (see test1.png)

Or I need to put some additional resistor-zener to protect GPIO (see test2.png)

Any tip would be will be welcome,

many thanks
Attachments
test1.png
circuit
test1.png (2.84 KiB) Viewed 61227 times
test2.png
test2.png (2.91 KiB) Viewed 61227 times

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

Re: How to sense a 5v input with GPIO

Tue Nov 13, 2012 11:11 pm

Yes, this would work fine, and R3 isn't needed!

I myself, would use 2K2 and 3K3 resistors for an exact 2:3 ratio, for a three-fifth attenuation, that is a conversion from 5.0 to 3.0 volt, but that is simply a question of taste.

This attenuation would be even suitable for high speed signals (like a high UART baudrate) as the input capacitance of a GPIO is very low.

In fact all, that is needed to safely connect a 5V signal into a (3V3) GPIO is some kind of current limiter to avoid putting massive amounts of current through the GPIO's inherent diode to 3V3 to the 3,3V power supply. Just putting an 1K resistor in series would also do that!

Metatronin
Posts: 55
Joined: Sat Aug 04, 2012 5:19 am

Re: How to sense a 5v input with GPIO

Wed Nov 14, 2012 12:51 am

So any 5v signal to the GPIO's need just a 1k resistor...is level shifting overkill(like in attached pic)?
Attachments
pilvshifter.jpeg
overkill?
pilvshifter.jpeg (8.22 KiB) Viewed 61214 times

Pijus_magnificus
Posts: 2
Joined: Tue Nov 13, 2012 10:32 pm

Re: How to sense a 5v input with GPIO

Wed Nov 14, 2012 2:26 pm

Many thanks for the answers,

Its only a unidirectional input signal. A simple movement sensor.

5v --> movement detected
0V --> No movement detected

I think I will keep the first circuit, that I hope is safe enough

Image

Thanks for the answers again

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

Re: How to sense a 5v input with GPIO

Wed Nov 14, 2012 4:51 pm

Pijus_magnificus wrote: I think I will keep the first circuit, that I hope is safe enough

Image

Thanks for the answers again
As a general rule, a voltage divider such as you have drawn appears as a source of voltage V0 in series with a resistance R0 such that:
V0=V*R1/(R1+R2)
and
R0=(R1*R2)/(R1+R2)

So in your case V0=~3v and R0=~6kΩ
That means you're safe

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

Re: How to sense a 5v input with GPIO

Wed Nov 14, 2012 5:11 pm

Metatronin wrote:So any 5v signal to the GPIO's need just a 1k resistor...is level shifting overkill(like in attached pic)?
Yes level shifting is generally overkill!
Also the left picture doesn't accomplish any level shifting, as its output to the Arduino is still 3V3 (actually its even a little bit less, as the base diode drop of the transistor is subtracted, so output will be 3.3- ca 0.5 = 2.8 Volt).
Generally no level shifting from 3V3 to most 5V devices is necessary, as the minimum voltage that is acceptable as high is often just 2.0 Volt.

adding an 1K series resistor before the GPIO isn't technically level shifting, it just prevents large currents entering the 3V3 supply through the GPIO's protection diodes. In that sense it prevents damaging the PI, but a much better solution would be a simple resistor divider, with a 2K2, and a 3K3 resistor.

Metatronin
Posts: 55
Joined: Sat Aug 04, 2012 5:19 am

Re: How to sense a 5v input with GPIO

Wed Nov 14, 2012 7:42 pm

Thanks, very helpful info. Is there anytime level shifting would be preferred over the resistor divider?

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

Re: How to sense a 5v input with GPIO

Thu Nov 15, 2012 12:19 am

There might be some situations where you want the GPIO to act as a bidirectional port, but actually the voltage divider as described would work in that situation too.
In "receiver mode" the voltage would be divided as described, and in transmitter mode the PI would simply push 3V3 on top of the bottom resistor of the resistor divider, and if the load after the "top resistor" isn't too great the paired device would still see 3V3.

The trouble begins when you have a bidirectional bust with many slave devices, some of which attempting to put 5V levels on the bus, and others 3V3 levels, the 3V3 levels would also be divided to a level below the "minimum high" that the PI will accept.

But as you can see this is quite an "exotic situation", but can happen sometimes, some people who use multiple I2C devices connected to the PI want "high" levels appropriate to each slave device, and there a simple level converter, consisting of two FETs, and four pullups (to different VCC levels) are used.

see http://ics.nxp.com/support/documents/in ... n97055.pdf

eehmke
Posts: 5
Joined: Wed Jan 30, 2013 4:11 pm

Re: How to sense a 5v input with GPIO

Thu Jan 31, 2013 9:23 am

The circuit posted by Metatronin is no level shifter, as the transistor works as emitter follower (common collector). If input from PI is 3.3V, output will never exceed 2.8V. The transistor needs to work in common emitter mode to get a 5V output. In this case the signal is inverted, so you may need two transistors.

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: How to sense a 5v input with GPIO

Mon Mar 04, 2013 2:15 pm

Hi all,

I have a question in line with this one. I want to use a 5V PIR sensor on a GPIO pin... so the output of the PIR is a 5V pulse.

I tried connecting a 10k resistor in line with the output and connect this to the GPIO. This works, but I feel it is not the safest way to deliver this input to the Raspberry.
From this topic I tried applying a 2k2 and 3k3 voltage divider, but now the Raspberry does not see the input.
Thinking the output might be too low I also tried a 10k and 20k voltage divider, but no luck either.

Any thought how I can safely use the 5v input?
Thanks!

edit: this is the sensor circuit I'm talking about, maybe it helps to understand how to connect it: http://www.produktinfo.conrad.com/daten ... 12V_FG.pdf

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: How to sense a 5v input with GPIO

Tue Mar 05, 2013 12:14 am

so if i have a 16x2 lcd being driven from the 5v output gpio pin, i can't have a switch feed that same 5v as an input back into a gpio pin (with 10k pull down resistor, 1k series protection)?

ok, i've modified my circuit to this now, so instead of the 10k pull down i've got a 330/470 ohm divider, does this look right - it does seem to detect high when the button is pressed and its not blown up yet!? the +volts comes via the push-switch
hd44780-divider_bb.png
hd44780-divider_bb.png (39.72 KiB) Viewed 59965 times

Code: Select all

#!/usr/bin/env python

from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)

while 1:
    print GPIO.input(7)

    if GPIO.input(7):
        print "BUTTON PRESSED"

    sleep(0.2)

bradix14
Posts: 13
Joined: Mon Feb 09, 2015 7:34 am

Re: How to sense a 5v input with GPIO

Mon Feb 09, 2015 11:16 pm

thof wrote:Hi all,

I have a question in line with this one. I want to use a 5V PIR sensor on a GPIO pin... so the output of the PIR is a 5V pulse.

I tried connecting a 10k resistor in line with the output and connect this to the GPIO. This works, but I feel it is not the safest way to deliver this input to the Raspberry.
From this topic I tried applying a 2k2 and 3k3 voltage divider, but now the Raspberry does not see the input.
Thinking the output might be too low I also tried a 10k and 20k voltage divider, but no luck either.

Any thought how I can safely use the 5v input?
Thanks!

edit: this is the sensor circuit I'm talking about, maybe it helps to understand how to connect it: http://www.produktinfo.conrad.com/daten ... 12V_FG.pdf
Thof, did you ever resolve this? I've got the same issue. When I run my DS18B20 temp sensor through the voltage divider, I get no input to the Pi...

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

Re: How to sense a 5v input with GPIO

Tue Feb 10, 2015 12:07 am

measure with a volt meter what the swing is on the GPIO pin, there is nothing particular difficult going on here, no need also to re-open an ancient thread.

as long as
a zero signal is lower than 0.8V, and
a one signal is between 2.4 and 3.3V

the GPIO as input should work fine.

The DS18B20 is a 1-wire device, so it needs a complex (I2C like) interface, especially as the 2-wire device also needs to be powered from the single wire interface it can get pretty complex, not something you can solve with a simple resistor divider, and completely different from the case in the previous post.

Neitsabes
Posts: 2
Joined: Wed May 20, 2015 9:17 am

Re: How to sense a 5v input with GPIO

Wed May 20, 2015 7:01 pm

I would like to connecte arduino MEGA to my raspberry using SPI interface. Could i use the kind of diviser discribe above ?
And if yes, on wich pin i have to connect it : MISO, CC and CLK is enough or i have to put it on MOSI too ?

The master will be the raspberry and slave arduino.

Thanks for your answer,

Thank you

User avatar
mikronauts
Posts: 2823
Joined: Sat Jan 05, 2013 7:28 pm

Re: How to sense a 5v input with GPIO

Wed May 20, 2015 7:16 pm

If Pi is SPI master, Mega is SPI slave:

only MISO needs the divider

I do this all the time in order to run SPI slaves at 5V connecting to the Pi - for one sample with schematic see:

http://www.mikronauts.com/raspberry-pi/ ... and-howto/

Theoretically just a current limiting resistor would be OK, but I feel safer with a voltage divider with a Pi.

With the Parallax Propeller microcontroller, I generally just use a 2k2 or 2k4 in-line current limiting resistor to interface to 5v I/O.
Neitsabes wrote:I would like to connecte arduino MEGA to my raspberry using SPI interface. Could i use the kind of diviser discribe above ?
And if yes, on wich pin i have to connect it : MISO, CC and CLK is enough or i have to put it on MOSI too ?

The master will be the raspberry and slave arduino.

Thanks for your answer,

Thank you
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

Marre
Posts: 6
Joined: Thu Dec 31, 2015 11:02 am

Re: How to sense a 5v input with GPIO

Sat Mar 12, 2016 9:59 pm

Just bought a PIR and saw this post. Seems like no need to limit the voltage as the "standard" raspberry PIR specs. read ".... Output: Digital pulse high (3V) when triggered (motion detected) digital low when idle (no motion detected)." Input is 5-12V. Should be fine with just current limiting resistor. I will however measure the output before wiring it to a GPIO pin.

gtoal
Posts: 123
Joined: Sun Nov 18, 2012 12:02 am

Re: How to sense a 5v input with GPIO

Tue Jun 26, 2018 1:40 am

I have a very similar problem but the issue I want to check with you is how much current is dissipated across the resistors - I have a system that is powered by the 5V bus of a Vectrex computer. Attached to the Vectrex is a smart cartridge (ARM powered but not pi-related) which is powered by the Vectrex's 5V bus. I have a Pi that is externally powered by a wall-wart but it has access to the 5V line that powers the smart cartridge from the Vectrex's edge connector (and its GND pin) - so when the vectrex powers up (and the 5V line is asserted) I want my Pi to start up a particular program that feeds data to the smart cartridge. However I do *not* want to sink any significant amount of power by doing so, because the smart cartridge really needs it.

So what value of resistor in any of the circuits suggested above would let me test for the 5V line being powered, without shorting so much current between 5V and GND that it risked underpowering the smart cartridge? Is the leakage down in the single digit mA range? I'm fairly sure that drawing 20 - 30 mA would be problematic.

pcmanbob
Posts: 13722
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to sense a 5v input with GPIO

Tue Jun 26, 2018 10:54 am

You can calculate the current flowing through the resistors by simply using ohm's law were current = voltage / resistance , I =V/R

https://en.wikipedia.org/wiki/Ohm%27s_law

so if you were to use a 1K & 2K resistor your total resistance would be 3K

so 5/3000 = 0.00167A or 1.67mA

if you want to draw less than that just change the resistance values to 10K and 20K which will be 30K in total

so 5/30000 = 0.000167A or 0.167mA which less than 1mA
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

gtoal
Posts: 123
Joined: Sun Nov 18, 2012 12:02 am

Re: How to sense a 5v input with GPIO

Thu Jun 28, 2018 10:01 pm

pcmanbob wrote:
Tue Jun 26, 2018 10:54 am
You can calculate the current flowing through the resistors by simply using ohm's law were current = voltage / resistance , I =V/R
Groan! The embarrassment of realising that E=IR was the only factor involved :-/

Return to “HATs and other add-ons”