mendhak
Posts: 9
Joined: Mon Apr 01, 2013 2:51 pm

Push buttons - should I be using a resistor or not? (yes)

Sat Jun 08, 2013 4:34 pm

Complete amateur here, so this might be a really obvious one. I'm looking at connecting push buttons to various GPIO pins. In some tutorials online, I see people use resistors in the circuit and in some cases, no resistors.

This example involves a resistor.
This example involves a resistor.
This example does not involve a resistor.

I have tried all of the above, and they all work. I like the simplicity of the last one, though. It only involves GPIO pin and a 0V pin, no 3.3V rail.

This is my current setup:
Image


Pin3->Switch->Negative->Pin6

I shoved a 100 ohm resistor in between the green and the switch, it still works. I put a 10K ohm resistor and it still works.

Seeing as how it works in all of these cases, am I missing something obvious - should I be using a resistor or not?

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

Re: Push buttons - should I be using a resistor or not?

Sat Jun 08, 2013 5:28 pm

I think the resistor is a safety feature helping to guard against mistakes. It limits the current flow. You are less likely to damage a gpio or the Pi if only a few milliamps can flow. It makes no difference to being able to detect logic 0s or 1s.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Push buttons - should I be using a resistor or not?

Sat Jun 08, 2013 5:57 pm

Examples 1 and 2 are pull-up resistors, this is required to prevent the inputs from floating in either digital 0 or digital 1.
The switch in example 3 provides a ground connection to GPIO 0 which according to the data specification already has a 1.8k pull-up resistor incorporated into the circuit board so an external one is not needed.

Richard S.
Last edited by redhawk on Sat Jun 08, 2013 5:59 pm, edited 1 time in total.

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

Re: Push buttons - should I be using a resistor or not?

Sat Jun 08, 2013 5:58 pm

The last example is using P1 pin 3, which is gpio 2 (or gpio 0 on rev 1 boards). That pin (and pin 5) already has a 1.8K pull-up resistor on the Pi board, so an external one isn't needed.

For any other gpio inputs, I would use something like an external 10K pull-up resistor and also a 1K resistor between the switch and the gpio pin. The 1K resistor would be a safeguard if the pin was accidentally set as output and set to 3.3V and the button being pressed and shorting it to ground.

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

Re: Push buttons - should I be using a resistor or not?

Sat Jun 08, 2013 6:27 pm

Seeing as how it works in all of these cases, am I missing something obvious - should I be using a resistor or not?
The one obvious point that you are missing is that the two GPIO's normally used for I2C already have (1.8K) pull-up resistors to 3V3, all other GPIO's need pullups.
furthermore, for safety purposes there actually should also be a 1K current limiting resistor in-between the GPIO, and the node between the switch and pull-up if you are using your own pull-up, (but for the on-board pullups this isn't possible).
The reason is that you might make a mistake and program the GPIO used for the button as an output, and program it to output 3.3V, if you then push the button you short the 3.3V to GND and the short-circuit current can then damage the GPIO. the 1K resistor limits the current to a safe value, while doing nothing for its normal input function.

Lastly, if you connect a 100nF capacitor over the switch you wont be bothered (when reading the switch) by bouncing contacts of a closing switch. The CPU is so fast it can "see" multiple key presses otherwise, unless you write software to suppress key bounce.

finally, the PI's GPIO logic is quite fancy, and its possible to program an internal (to the PI's chip) pull-up (or pull-down). You may think this makes an external pull-up unnecessary, but this isn't completely true as the pull-up is relatively weak (probably around 50K), and long wires may pick up (as an antenna) electrical noise which may override the weak pull-up so use an external pull-up anyway, a good value being 4K7 or 10K.

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Push buttons - should I be using a resistor or not?

Sat Jun 08, 2013 7:25 pm

Interesting. I understand all the reasons here. How do you enable and disable internal pull-up and pull down resistors?

I am still learning Pi.

For a simple setup the OP is doing enabling internal pull-up would be sufficient even if it is a 50K. 1.8Kohm seems pretty strong pullup for I2C. Was there any particular reason for this value?
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

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

Re: Push buttons - should I be using a resistor or not?

Sat Jun 08, 2013 8:03 pm

I2C is a semi-high speed bus, so you need strong pullups to get fast enough rise times, when dealing with long wires (with many picofarads of parasitic capacitance), and 1K8 are the value that gives a pull down current that is the maximum the GPIO's will safely handle.

The Broadcom chip has many registers controlling the behavior of the GPIO's, but most often simplified libraries are used that ignore some of the possibilities, and won't turn on the pullups per default. there are some header files describing the registers in details, and there is the data sheet of the broadcom chip that describes all of them. also the Wiki for the raspberry gives a lot of info. http://elinux.org/RaspberryPiBoard.

I would not recommend relying on only the software controlled pullups, even though they are compatible with the current limiting 1K safety resistor. That is an 1K resistor to GND will override the 50K pull-up enough to read as "0".
50K is too sensitive for disturbances.

using a 10K external pull-up, and a 1K protection resistor, is simply best practice especially when experimenting.

mendhak
Posts: 9
Joined: Mon Apr 01, 2013 2:51 pm

Re: Push buttons - should I be using a resistor or not?

Sun Jun 09, 2013 6:54 am

Thanks for the replies everyone. While going with the last one (simple, no resistors), I noticed that having three buttons just wouldn't work. The third button would keep receiving 'phantom' highs/lows which the code was picking up, so it was as though the button was being pressed all by itself. With two, it would work just fine.

In the end, I went back with the adafruit circuit (first example), using the 3.3V rail, buttons, and 10K resistors.

Image

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

Re: Push buttons - should I be using a resistor or not?

Sun Jun 09, 2013 12:15 pm

yes, because there are only two GPIO that already have pullups. The two that are used for I2C communication.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Push buttons - should I be using a resistor or not?

Sun Jun 09, 2013 5:42 pm

mendhak wrote:Thanks for the replies everyone. While going with the last one (simple, no resistors), I noticed that having three buttons just wouldn't work. The third button would keep receiving 'phantom' highs/lows which the code was picking up, so it was as though the button was being pressed all by itself. With two, it would work just fine.

In the end, I went back with the adafruit circuit (first example), using the 3.3V rail, buttons, and 10K resistors.

Image
You can enable the internal pull-ups with the gpio program if you have wiringPi installed:

gpio mode 5 up

will enable the pull-up on pin 5 (wiringPi pin number). to use the broadcom gpio numbers use:

gpio -g mode 24 up

you can do it in a program too, but that's easy for testing.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
Mosmas
Posts: 11
Joined: Tue Oct 01, 2013 9:23 am
Location: Essen, Germany
Contact: Website

Re: Push buttons - should I be using a resistor or not? (yes

Sun Jan 19, 2014 11:50 am

I do it this way: I use the on-board pull-up (1k8) at GPIO 3 (header P1, pin 5), and add a 560R in series to a button to ground:

Code: Select all

3v3
 |
 O 1k8 (on-board)
 |
 +---o GPIO3
 |
 O 560R
 |
/
 |
-+- GND
This protects the GPIO-pin from short circuit when accindentally configured as output, and gives a proper high/low signal.

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

Re: Push buttons - should I be using a resistor or not? (yes

Sun Jan 19, 2014 1:17 pm

that isn't a good way to do it, as the GPIO signal probably doesn't go low enough when you close the switch.

With the switch closed a resistor divider is created that divides the 3.3V up to something like 0.85Volt
The problem is that the PI probably won't see 0.85V as a proper "low".

If you move the resistor to be simply in series with the output, then you will get a proper low voltage at the GPIO, and still protect it.

User avatar
Mosmas
Posts: 11
Joined: Tue Oct 01, 2013 9:23 am
Location: Essen, Germany
Contact: Website

Re: Push buttons - should I be using a resistor or not? (yes

Sun Jan 19, 2014 6:12 pm

My calculation gives 0.78V (560*3.3/[1800+560]) at the GPIO pin with the button pressed -- and that is exactly what my voltmeter says. This should be a "proper low" (the maximum low voltage is specified at 0.8V, see http://www.mosaic-industries.com/embedd ... -circuitry). Apart from that: I do read a stable low when the button is pressed, and a stable high otherwise.

Since I use GPIO3 with the on-board pull-up I cannot place the additional resistor where you suggest. Hence my solution.

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

Re: Push buttons - should I be using a resistor or not? (yes

Sun Jan 19, 2014 6:43 pm

My calculation gives 0.78V (560*3.3/[1800+560]) at the GPIO pin with the button pressed -- and that is exactly what my voltmeter says. This should be a "proper low" (the maximum low voltage is specified at 0.8V, see http://www.mosaic-industries.com/embedd ... -circuitry). Apart from that: I do read a stable low when the button is pressed, and a stable high otherwise.
a low so close to the theoretical maximum is asking for problems. if the 1.8K resistor and the 560 ohm both have their 5% tolerance in the wrong direction you could have a 10% different voltage as a result.
Since I use GPIO3 with the on-board pull-up I cannot place the additional resistor where you suggest. Hence my solution.
Not completely 100% true, as the 1K8 resistor is simply a resistor outside the SoC. It's an an-board pull-up not a built in pull-up, but yes, you would need to cut a track to insert the resistor, probably not a good idea. But I think that even with this particular GPIO pin, where inserting the protection resistor "isn't possible" I would try use a 10% smaller resistor than 560E to ensure the low would be something like 0.6V, as 0.78V is way too close to the "grey zone", even a very small noise (20 mV) could already cause troubles. And yes, using such a lowered resistor would offer a more limited output short protection, but its much better than nothing. I would use 470E.

User avatar
Mosmas
Posts: 11
Joined: Tue Oct 01, 2013 9:23 am
Location: Essen, Germany
Contact: Website

Re: Push buttons - should I be using a resistor or not? (yes

Sun Jan 19, 2014 7:24 pm

Thanks, I'll take 470R.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Push buttons - should I be using a resistor or not? (yes

Sun Jan 19, 2014 7:32 pm

Mosmas wrote:Thanks, I'll take 470R.
Why not work out the lowest that is "safe" for you and use that rather than seemingly pick values at random.

So - given that the max. safe current in/out of a GPIO pin is 15mA, then ohms law gives us R=V/I => 3.3/0.015 = 220Ω

Put that into your resistor divider calculation and you get 0.36v with the button closed - way below the 0.8v grey area.

Or just don't bother. I don't bother for my own projects. Really - you need three things to happen before you get close to having a problem: (a) pin to output mode, (b) write 1 to the pin and (c) push the button. What's the probability of you doing that vs. the effort of adding in an extra resistor?

-Gordon
--
Gordons projects: https://projects.drogon.net/


shkaff
Posts: 6
Joined: Mon Aug 31, 2015 9:42 am

Re: Push buttons - should I be using a resistor or not? (yes

Mon Dec 28, 2015 11:45 am

Nice video

Is my understanding correct that I don't really have to use an external pullup resistor and may configure an internal one via commands like the following?

GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)

However, as I understand, since that moment all the responsibility is on me and the program which should execute that code before I push the button, because in worst case I have a risk if the program didn't start and no onboard resistor was pulled up then the nasty situation of short circuit happens.

So from the reliability point of view it is more safe to explicitly place an external pullup resistor. Right?

gordon77
Posts: 4110
Joined: Sun Aug 05, 2012 3:12 pm

Re: Push buttons - should I be using a resistor or not? (yes

Mon Dec 28, 2015 12:10 pm

The problems occur if you connect the switch and set the GPIO as an output ( by mistake).

The pull up / down or not won't cause a problem, it will stop spurious noise on the input giving false indications.

FlashT
Posts: 64
Joined: Fri Jul 24, 2015 3:51 pm

Re: Push buttons - should I be using a resistor or not? (yes

Tue Dec 06, 2016 7:30 pm

Do I have to use resistor when using button only to change GPIO state? 3.3V PIN -> GPIO? I guess nothing bad could happen? Or am I wrong?

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Push buttons - should I be using a resistor or not? (yes

Wed Dec 07, 2016 10:59 am

FlashT wrote:Do I have to use resistor when using button only to change GPIO state? 3.3V PIN -> GPIO? I guess nothing bad could happen? Or am I wrong?
It's always a good idea to have a current limiting resistor. If you set an input pin to output or vice versa bad things can happen, a resistor will protect you. The pins can only source or sink 16mA so limiting the current to that level (with at least 220 ohms) makes sense.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Push buttons - should I be using a resistor or not? (yes

Wed Dec 07, 2016 12:25 pm

DougieLawson wrote:
FlashT wrote:Do I have to use resistor when using button only to change GPIO state? 3.3V PIN -> GPIO? I guess nothing bad could happen? Or am I wrong?
It's always a good idea to have a current limiting resistor. If you set an input pin to output or vice versa bad things can happen, a resistor will protect you. The pins can only source or sink 16mA so limiting the current to that level (with at least 220 ohms) makes sense.
Just to add a bit more here... The Pi pins have the ability to source or sink a lot more than 16mA. I measured 35mA at one point in time and have also caused them to pull so much current it pulled the 3.3v supply low enough to reboot the Pi - which had the beneficial effect of resetting the pin to input mode to help prevent further damage. (I'd not like to test this on a new Pi with the SMPS 3.3v supply though)

The 16mA "limit" is to do with the pin drive strength setting being set to max and the output voltage staying sufficiently high to maintain a logic 1 in a 3.3v system. The normal pin drive strengths are set at power on time such that they only allow for up to 8mA output to guarantee that the output voltage stays at a logic 1 in a 3.3v system. (It's a drive strength system, not a current limiting system) The gpio program in wiringPi can fiddle with the drive strength settings, but I do not recommend you fiddle with them.

I still don't use limiting resistors for button or other digital sensor inputs, but I try to think that I know what I'm doing...

-Gordon
--
Gordons projects: https://projects.drogon.net/

billytheone
Posts: 1
Joined: Fri Apr 13, 2018 4:05 pm

Re: Push buttons - should I be using a resistor or not? (yes)

Fri Apr 13, 2018 4:05 pm

Will 4.7K resistors be ok for this?

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

Re: Push buttons - should I be using a resistor or not? (yes)

Sun Apr 15, 2018 9:25 am

billytheone wrote:
Fri Apr 13, 2018 4:05 pm
Will 4.7K resistors be ok for this?
Image

These are the diagrams for pull-up and pull-down switch circuits you can safely change the 10K resistor for a 4.7K but the 1K resistors should stay at 1K
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
adun
Posts: 102
Joined: Fri Mar 20, 2015 9:25 am
Location: Switzerland

Re: Push buttons - should I be using a resistor or not? (yes)

Sun Apr 15, 2018 10:56 am

If I recall correctly there were two push buttons (sw2 and sw3) on the Raspberry Pi alpha board connected to the GPIOs. If one could tell how they were wired up there we could do it the same way.

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