PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

GPIO and Buttons

Tue Mar 25, 2014 11:35 am

Hi,

I have seen a few tutorials online on hooking up a button to a GPIO pin. Some use a pull-up resistor and some don't. If I'm sending 3.3v to the GPIO pin, do I need a resistor or not?

Thanks

Phil

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

Re: GPIO and Buttons

Tue Mar 25, 2014 11:44 am

You don't need one but it will be safer if you use one. If the gpio is mistakenly configured as an output you may short 3V3 to ground without a resistor.

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: GPIO and Buttons

Tue Mar 25, 2014 11:49 am

joan wrote:If the gpio is mistakenly configured as an output you may short 3V3 to ground without a resistor.
Thanks. if that was to happen, what damage to the Pi would/could happen?

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

Re: GPIO and Buttons

Tue Mar 25, 2014 11:55 am

If the button (switch) is connected between the GPIO and ground, when it's pressed, there'll be 0v on the GPIO.
But when it's not pressed, there's nothing to tell the GPIO what voltage to be at - it could be 0, it could be 3v3, it could be somewhere in between.

The pull-up resistor tells the GPIO what to do if the switch isn't closed.
In that respect, the tutorials that aren't using a pull-up are wrong - though they could be relying on the Pi's built-in pull-ups

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

Re: GPIO and Buttons

Tue Mar 25, 2014 12:01 pm

PhillyNJ wrote:
joan wrote:If the gpio is mistakenly configured as an output you may short 3V3 to ground without a resistor.
Thanks. if that was to happen, what damage to the Pi would/could happen?
Best case, nothing. Worst case, the transistors inside the SoC driving the pin would be overloaded, and the silicon would melt. No more Pi.
The latter is more likely than the former.

1) Use a pull-up, either real or the internal software-controlled.
2) Put a resistor between the Pi and your button

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

Re: GPIO and Buttons

Tue Mar 25, 2014 12:06 pm

PhillyNJ wrote:
joan wrote:If the gpio is mistakenly configured as an output you may short 3V3 to ground without a resistor.
Thanks. if that was to happen, what damage to the Pi would/could happen?
I don't know. At a guess something between a Pi reset (caused by the 3V3 supply falling too low to power the CPU) to a burned out gpio to a burned out bank of gpios to a burnt out Pi.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: GPIO and Buttons

Tue Mar 25, 2014 12:28 pm

There will be a pull up/down resistor involved in the circuit. Some will use a physical resistor and some will use the internal software pull up/down resistor. Without either the pin is floating and the condition of the pin with the switch open circuit is ambiguous.

It all depends if you want it to work or not.

The pull up/down resistor isn't going to save anything if you program it as output, set the pin high and then have a hard switch pulling it low. (Or vice versa). The resistor doesn't enter into the equation, its a short circuit with or without it.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: GPIO and Buttons

Tue Mar 25, 2014 1:00 pm

To be clear, OP, a pullup or pulldown is not in series with your button/ peripheral. It is attached at the point where the button would connect to your gpio, either internally or externally. The other end of the pullup/down is then connected to 3.3v (pullup) or ground (pulldown). This defines logic state when the button is not pressed. The other end of the button is connected to the logical opposite of the pull-up / down resistor. If you're using a pull-up, for example, this would be ground.

The resistor used to protect the gpio is a current-limiting resistor. It is placed in series with the button (between the gpio and button or between the button and 3.3v or ground).

The value of either if these resistors can be quite high unless you are also trying to drive something like an led that requires some current. Typical pull-up and pulldown values are 10k, and an inline current limiter in this case just needs to be small enough to overcome the pulldown and get the input voltage suitably low or high. From very preliminary googling it appears these values are 0.8V and 1.3V. A 1k resistor would bring you 0.3V or 3.0V, so you'd be good there and limit you to 3.3mA on a button press to ground. Just make sure to keep it under 15mA!

Colin
Last edited by iinnovations on Tue Mar 25, 2014 2:10 pm, edited 2 times in total.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: GPIO and Buttons

Tue Mar 25, 2014 1:11 pm

Thanks all. I will go the safe route and add resistors. Better safe than sorry.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: GPIO and Buttons

Tue Mar 25, 2014 1:38 pm

If you're making it safe with a resistor then its not a pull up, its a current limiter and is wired differently. A pull up won't save you if you screw up the in/out bit.

Image
Last edited by pluggy on Tue Mar 25, 2014 1:46 pm, edited 1 time in total.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

trippes
Posts: 6
Joined: Mon Mar 24, 2014 12:42 pm

Re: GPIO and Buttons

Tue Mar 25, 2014 1:44 pm

Hi all
this is a similar post to mine - but with many more answers - which is great :-). I have no intention/desire to hijack this post but I would like to add in my thoughts

I think I now understand - the resistor offers protection to the pi in case voltage "in/out" is configured correctly. However, if the configuration of the pi is never going to change - (all it will do is run some code when the button is pressed) - then, providing it is configured correctly the first time, am I right in saying the risk is minimal ?

obviously if people were able to access the pi and change parameters - I think the resistor would be essential

Or have I fundamentally misunderstood .... again :-)
cheers

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: GPIO and Buttons

Tue Mar 25, 2014 1:54 pm

trippes wrote:Hi all
this is a similar post to mine - but with many more answers - which is great :-). I have no intention/desire to hijack this post but I would like to add in my thoughts

I think I now understand - the resistor offers protection to the pi in case voltage "in/out" is configured correctly. However, if the configuration of the pi is never going to change - (all it will do is run some code when the button is pressed) - then, providing it is configured correctly the first time, am I right in saying the risk is minimal ?

obviously if people were able to access the pi and change parameters - I think the resistor would be essential

Or have I fundamentally misunderstood .... again :-)
cheers
Nope, a resistor wired as a pull up has no protection value whatever. The pin is wired directly to ground if the switch is closed.

Image

Now if you had a larger value pullup resistor and a smaller value current limiter, that would be a different story. You have to be careful with the values because its a voltage divider when the switch is closed.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: GPIO and Buttons

Tue Mar 25, 2014 2:09 pm

trippes wrote:Hi all
this is a similar post to mine - but with many more answers - which is great :-). I have no intention/desire to hijack this post but I would like to add in my thoughts

I think I now understand - the resistor offers protection to the pi in case voltage "in/out" is configured correctly. However, if the configuration of the pi is never going to change - (all it will do is run some code when the button is pressed) - then, providing it is configured correctly the first time, am I right in saying the risk is minimal ?

obviously if people were able to access the pi and change parameters - I think the resistor would be essential

Or have I fundamentally misunderstood .... again :-)
cheers
Please read my post above. Both are defined.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

Return to “General discussion”