rvdz
Posts: 10
Joined: Mon Jul 23, 2012 7:13 pm

GPIO pin reading

Fri Jul 27, 2012 12:57 pm

Hi all,

I'm pretty new to the Pi and to this forum so sorry if this is a silly question but here we go.

I've been trying out the GPIO pins on the Pi and with some help from other posts on this forum I managed to get some LED's going using python - all very cool stuff.

Then I tried to read input on a pin so I wired it up to a push button but I'm getting really erratic readings, regardless of which pin I use.

When the pin is not connected to anything, the reading comes back as False consistently, which is fine. But as soon as I plug in a wire (not connected to anything at the other end!) I get random readings of True and False.

Am I doing something wrong?

The python code I'm using is as follows:

Code: Select all

while True:
    print(str(GPIO.input(22)))
    time.sleep(0.2)
Thanks in advance.

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

Re: GPIO pin reading

Fri Jul 27, 2012 1:03 pm

You get inconsistant readings, because the voltage on the GPIO pin is inconsistant.
You probably forgot to add a pullup (10K to 3V3) to consistently pull the pin to a logic high. Then you can use a switch or button to draw the pin low, that how it works.

Yes, the pins are so high impedance and so sensitive that they pick up noise (mains hum) from the surroundings.

rvdz
Posts: 10
Joined: Mon Jul 23, 2012 7:13 pm

Re: GPIO pin reading

Fri Jul 27, 2012 1:09 pm

Thanks for the quick reply, much appreciated!

So where do I wire in the pullup resistor?

I understand the pins are very sensitive so the reading I get is not accurate but does that explain that even if I connect a loose wire to the pin it already starts throwing in false readings?

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

Re: GPIO pin reading

Fri Jul 27, 2012 1:30 pm

you wire the pullup between the GPIO pin you are trying to read, and the 3,3 Volt power supply. That is pin 1 (or 17) of the GPIO header.

If you just connect a piece of wire to the pin, the wire acts as an "antenna", and pics up random AC signals from the air.

To get a reliable signal the GPIO pin must either be connected (preferably through a resistor) to either the 3,3 Volt, or the ground signal.

if the signal is above about 2 Volt the pin registers a "high" (1), if its below about 2 Volt it registers as a "low" (0).

The free magazine magpie has an article about this, see magpie #3 page 8.

rvdz
Posts: 10
Joined: Mon Jul 23, 2012 7:13 pm

Re: GPIO pin reading

Fri Jul 27, 2012 1:36 pm

Brilliant, that couldn't be clearer! Thanks for your help, I will try that out tonight.

User avatar
Grumpy Mike
Posts: 941
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: GPIO pin reading

Fri Jul 27, 2012 1:58 pm

For a full discussion of this see:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO pin reading

Fri Jul 27, 2012 2:16 pm

Have a look at the Interfacing article in the MagPi magazine issue 2 as well :
http://lgcproductions.co.uk/pi/magpi/Th ... 0Final.pdf

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

rvdz
Posts: 10
Joined: Mon Jul 23, 2012 7:13 pm

Re: GPIO pin reading

Sat Jul 28, 2012 8:52 pm

Thanks for the info guys, I managed to get it working this afternoon :D

raspiCAM
Posts: 57
Joined: Mon Aug 25, 2014 9:13 am

Re: GPIO pin reading

Fri Aug 29, 2014 3:36 am

mahjongg wrote:You get inconsistant readings, because the voltage on the GPIO pin is inconsistant.
You probably forgot to add a pullup (10K to 3V3) to consistently pull the pin to a logic high. Then you can use a switch or button to draw the pin low, that how it works.

Yes, the pins are so high impedance and so sensitive that they pick up noise (mains hum) from the surroundings.

Hi, how to solve this sensitive problem?

I can't use input pin at all, I just touch the wire and input always triggered.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO pin reading

Fri Aug 29, 2014 6:40 am

Have you used a pull-up resistor, as advised in the post?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

raspiCAM
Posts: 57
Joined: Mon Aug 25, 2014 9:13 am

Re: GPIO pin reading

Fri Aug 29, 2014 7:00 am

texy wrote:Have you used a pull-up resistor, as advised in the post?
Texy
Edit: Finally I got it to work.
Previously I saw someone post a schematic that pull up with 3.3V, so my board also pull up with 3.3V.
Now I give it a try to pull up with 5V, then it work with no problem.

Hi Texy,

Yes, I have used pull up resistor 10K.

But whenever I touch the wire, interrupt or read input always give me high input.

If I hold the push button down and touch the wire, then ok.

I don't think this is normal for a GPIO.

Thanks, Please advise.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO pin reading

Fri Aug 29, 2014 8:57 am

Maybe you have a faulty GPIO line or resistor. Are you able to use a different Pi or GPIO line?
Also have you considered using the internal pull resistors?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

raspiCAM
Posts: 57
Joined: Mon Aug 25, 2014 9:13 am

Re: GPIO pin reading

Fri Aug 29, 2014 9:29 am

texy wrote:Maybe you have a faulty GPIO line or resistor. Are you able to use a different Pi or GPIO line?
Also have you considered using the internal pull resistors?
Texy
Hi Texy,

Thanks for your support. Really appreciated it.

I have edited my reply above, now it worked.

I need to pull up with 5V instead of 3.3V.

Edit: Finally I got it to work.
Previously I saw someone post a schematic that pull up with 3.3V, so my board also pull up with 3.3V.
Now I give it a try to pull up with 5V, then it work with no problem.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO pin reading

Fri Aug 29, 2014 9:32 am

You will damage the pi by pulling up to 5volts - the Pi's I/O is only 3v3 volt tolerant.
I would look for another solution.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

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

Re: GPIO pin reading

Fri Aug 29, 2014 9:39 am

Do not pull up to 5V! The PI's GPIO ports are not 5V compliant, If you put 5V on a GPIO for longer time the GPIO will become damaged, causing an internal short inside the chip!

If when pulling up to 3V3 the GPIO is still sensitive to noise, then your pull-up is to weak (or not really connected at all) so simply lower its resistance, to for example 4K7 or 2K2, a GPIO pin can "drain" about 15mA max, so do not lower the pull-up so that it drains more than 15mA (that is do not lower the pull-up below R=U/I = 3.3V/0.015A= 220 ohm). in some cases it might help putting a small capacitor (10nF to 100nF) between the GPIO and GND.

raspiCAM
Posts: 57
Joined: Mon Aug 25, 2014 9:13 am

Re: GPIO pin reading

Fri Aug 29, 2014 9:42 am

texy wrote:You will damage the pi by pulling up to 5volts - the Pi's I/O is only 3v3 volt tolerant.
I would look for another solution.
Texy
Ok. Roger that. I have removed from 5V.

raspiCAM
Posts: 57
Joined: Mon Aug 25, 2014 9:13 am

Re: GPIO pin reading

Fri Aug 29, 2014 9:53 am

mahjongg wrote:Do not pull up to 5V! The PI's GPIO ports are not 5V compliant, If you put 5V on a GPIO for longer time the GPIO will become damaged, causing an internal short inside the chip!

If when pulling up to 3V3 the GPIO is still sensitive to noise, then your pull-up is to weak (or not really connected at all) so simply lower its resistance, to for example 4K7 or 2K2, a GPIO pin can "drain" about 15mA max, so do not lower the pull-up so that it drains more than 15mA (that is do not lower the pull-up below R=U/I = 3.3V/0.015A= 220 ohm). in some cases it might help putting a small capacitor (10nF to 100nF) between the GPIO and GND.
Hi Mahjongg,

Thanks for your advise and fast response before the board damaged.

I got it to work at resistor 2.2k.

Appreciated all help from Mahjongg and texy, have a nice weekend~

Andrew_ww
Posts: 8
Joined: Wed May 23, 2012 11:30 am

Re: GPIO pin reading

Sun Aug 31, 2014 3:47 pm

Hi!

I am new to raspberry, and would ask some gpio reading related question.

I downloaded bcm235-arm-peripherals.pdf doc from net, and not found any mention about gpio pin direction register in chapter 6. Want to be sure about their default behavior, so asking before permanently damaging a b+ by driving signals already driven by raspberry too. I prepare to drive some gpios with a board-connected pic: gpio25 (pin22) and gpio9 (pin21, a pic spi interface so signal will connect to here). Is it safe to design it without current limiter resistor? Sry for noob question.

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

Re: GPIO pin reading

Sun Aug 31, 2014 5:39 pm

Andrew_ww wrote:Hi!

I am new to raspberry, and would ask some gpio reading related question.

I downloaded bcm235-arm-peripherals.pdf doc from net, and not found any mention about gpio pin direction register in chapter 6. Want to be sure about their default behavior, so asking before permanently damaging a b+ by driving signals already driven by raspberry too. I prepare to drive some gpios with a board-connected pic: gpio25 (pin22) and gpio9 (pin21, a pic spi interface so signal will connect to here). Is it safe to design it without current limiter resistor? Sry for noob question.
You may have got a better response with a new thread.

A current limiting resistor would be safer. However gpios 25 and 9 should default to high impedance inputs when the Pi starts (even if SPI is enabled as gpio 9 will be MISO).

Return to “Interfacing (DSI, CSI, I2C, etc.)”