Blue_Dragon360
Posts: 9
Joined: Sun Oct 19, 2014 9:34 pm

How can I read a button press from GPIO?

Sun Oct 19, 2014 9:41 pm

Hello!
I'm very new to the GPIO and circuitry in general, so bear with me a bit. :)

I am currently trying to wire a normal button to activate the "escape" key when pressed. It is a button with an input and an output, pretty simple. Not one with 4 wires off of it. I do not have a breadboard. Which pins should I wire it to? How should I read the button press in software? I'm using model B+. I've read some about "pushup" or "pushdown" resistors, can someone explain these to me? Do I need them for this? I am quite confused as to their function.

Any help is very much appreciated!

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: How can I read a button press from GPIO?

Sun Oct 19, 2014 10:07 pm

You could start here: https://www.youtube.com/watch?v=HhfzRn2WcDQ
But there are many other info sources available...

Gr
Dirk.

Blue_Dragon360
Posts: 9
Joined: Sun Oct 19, 2014 9:34 pm

Re: How can I read a button press from GPIO?

Sun Oct 19, 2014 10:22 pm

DirkS wrote:You could start here: https://www.youtube.com/watch?v=HhfzRn2WcDQ
But there are many other info sources available...

Gr
Dirk.
Thanks for the link! That helps a lot. I noticed he did use a resistor in that -- Should I use the same type for my button, even though it's a different type of button? I'm still confused about the different types of resistors I mentioned in my post.

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How can I read a button press from GPIO?

Mon Oct 20, 2014 6:00 am

The GPIOs sense the level of voltage on the pin. If the voltage is around 2.0 to 3.3 volts it reads as "1" or "on". If the voltage is around 0 to 0.8 volts it reads as "0" or "off". If it is between 0.8 and 2.0 volts it can't be sure and might read "0" or "1" or keep changing between the two.

When you have a switch between the GPIO and GND, pressing the switch will connect the GPIO directly to GND which is 0 volts and the GPIO will read "0". But when the switch is not pressed the GPIO will not be connected to anything and will "float". This means it will pick up any stray electrical noise in the air and circuit (it is very sensitive) and will give strange readings.

In order to stop that happening you need to make sure that the voltage reads over 2.0 volts when the button isn't pressed. A resistor allows a little electricity to flow, so connecting something like a 10K (10,000 Ohms) resistor between the GPIO and 3.3V will hold the GPIO "high" until the button is pressed and pulls it "low". This is called a "Pull-Up" resistor because it pulls the GPIO up to 3.3V.

A small current will flow from 3.3V through the resistor and button (when it is pressed) to 0V, but that will be tiny with a 10K resistor and nothing to worry about.

A Pull-Down resistor is used with the circuit wired "upside-down", with the resistor between the GPIO and GND and the button between the GPIO and 3.3V.

The GPU has got built-in Pull-up and Pull-down resistors (at about 56K), but they are normally disabled. You can enable them with code instead of adding external resistors.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How can I read a button press from GPIO?

Mon Oct 20, 2014 7:19 am

The Screaming Jelly Baby tutorial shows how to connect a basic switch. [Using a Jelly Baby and an mp3 are not compulsory.]

Blue_Dragon360
Posts: 9
Joined: Sun Oct 19, 2014 9:34 pm

Re: How can I read a button press from GPIO?

Mon Oct 20, 2014 11:35 pm

Thanks for all the great resources and information! I'll give it a shot :)

kylerichards89
Posts: 98
Joined: Sun Aug 31, 2014 12:42 am

Re: How can I read a button press from GPIO?

Thu Oct 23, 2014 1:45 pm

Blue_Dragon360 wrote:Thanks for all the great resources and information! I'll give it a shot :)
Something else to keep in mind. I'd personally stick with external resistors for the pull-up or pull-down circuit. It offers more protect for the GPIO pins and from my experience, using an external 10k resistor, it doesn't get any false readings like the internal ones. The interal resistors are very sensitive and can sometimes cause a false button push. You can prove this pretty easy by writing a script that prints anytime the button is pressed and every so often, you'll get a false reading using the internal resistors.

Return to “Beginners”