Page 1 of 1

GPIO pin reading

Posted: Fri Jul 27, 2012 12:57 pm
by rvdz
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.

Re: GPIO pin reading

Posted: Fri Jul 27, 2012 1:03 pm
by mahjongg
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.

Re: GPIO pin reading

Posted: Fri Jul 27, 2012 1:09 pm
by rvdz
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?

Re: GPIO pin reading

Posted: Fri Jul 27, 2012 1:30 pm
by mahjongg
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.

Re: GPIO pin reading

Posted: Fri Jul 27, 2012 1:36 pm
by rvdz
Brilliant, that couldn't be clearer! Thanks for your help, I will try that out tonight.

Re: GPIO pin reading

Posted: Fri Jul 27, 2012 1:58 pm
by Grumpy Mike
For a full discussion of this see:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Re: GPIO pin reading

Posted: Fri Jul 27, 2012 2:16 pm
by texy
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

Re: GPIO pin reading

Posted: Sat Jul 28, 2012 8:52 pm
by rvdz
Thanks for the info guys, I managed to get it working this afternoon :D

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 3:36 am
by raspiCAM
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.

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 6:40 am
by texy
Have you used a pull-up resistor, as advised in the post?
Texy

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 7:00 am
by raspiCAM
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.

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 8:57 am
by texy
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

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 9:29 am
by raspiCAM
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.

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 9:32 am
by texy
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

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 9:39 am
by mahjongg
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.

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 9:42 am
by raspiCAM
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.

Re: GPIO pin reading

Posted: Fri Aug 29, 2014 9:53 am
by raspiCAM
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~

Re: GPIO pin reading

Posted: Sun Aug 31, 2014 3:47 pm
by Andrew_ww
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.

Re: GPIO pin reading

Posted: Sun Aug 31, 2014 5:39 pm
by joan
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).