Correct way to wire a button

Hi!
I'm refering to this tutorial:

Arduino - Button

Isn't there a resistor missing on the GPIO side of the button?

I mean... if the code accidentally is setting the pin to output instead of input - wouldn't that create a short?

Well, technically speaking... you are correct... its not a good idea for you to flip the pin to output mode and then press the button. That failure would be entirely you and your codes fault.

Generally, pins default to inputs... so there is some reason to feel comfortable knowing pins only get set to outputs "on purpose".

If, however, you feel this is a real possibility "in your situation", there is no harm in adding a 180 to 220 ohm resistor in series with the pin to protect it from "you". The button sensing will not be impacted by the extra resistor provided that it is low enough in value to allow the button switch to be unimpaired and high enough not to overload the pin on a 'short'.

Its usually not necessary and does not need to be part of a final design. Simple enough to add while you are goofing around with your own designs.

DIYglenn:
I'm refering to this tutorial:
Arduino - Button

The big blunder with that tutorial is that it suggests connecting the button to Vcc as the first option. It is almost always preferable, simpler in design and safer to have buttons or switches connected to ground.

DIYglenn:
I mean... if the code accidentally is setting the pin to output instead of input - wouldn't that create a short?

True enough but seriously, if you are going to make that sort of mistake in code, you are going to have a lot of problems and not just this one!

Paul__B:
The big blunder with that tutorial is that it suggests connecting the button to Vcc as the first option. It is almost always preferable, simpler in design and safer to have buttons or switches connected to ground.

+1

It’s the first Google result for “Arduino button”, the official Arduino tutorial, and it fails to mention that the internal pull-up resistors are a thing.
There are way too many beginners who think that they need an external resistor for every switch they use.

Pieter

The primary failure with these kinds of examples is that it makes people think that you need to assert a signal, as in... make a PIN see a "1" value to create a "truth" related to the button being pressed. I think the belief is that it is easier for beginners to grasp.

It actually only serves to confuse in the long run. Sensing a button press is only about the arduino sensing that a pin status has changed. It makes no difference if it goes from logic "1" to logic "0" or visa versa. Making use of the signal levels is simply a matter of coding.

Built-in pullups should be mentioned and discussed right away... judging by the neverending barrage of questions that begin with "What are Pullups Resistors for?" This way we can also explain the few cases where pulldowns are needed.

Thanks guys.
I’ve never used pull-downs, always pull-ups for my projects when using buttons. So I guess in the example I linked to, it’s the opposite of what I would do.
So would it even be possible with a short in this exact example?

But in general - yes, I see what you mean. I for one wouldn’t typically do such an error, but for prototyping I’m pretty sure I could suddenly switch two wires on a busy breadboard or similar, and create a kill button for my Arduino if not being careful.
I’m creating a small PCB for tinkering which would be used by newbies, and thought this would be nice to have in the design there as well.

So would it even be possible with a short in this exact example?

Yes, pin in output mode, write high, press button (connects to ground) and you have a short.

I'm creating a small PCB for tinkering which would be used by newbies, and thought this would be nice to have in the design there as well.

Not wanting to take your fun away, but there is the ruggeduino

PieterP:
…There are way too many beginners who think that they need an external resistor for every switch they use.

this is correct (IMO).

until they understand BASICS - no need to “spoil” them with INPUT_PULLUP

sterretje:
Yes, pin in output mode, write high, press button (connects to ground) and you have a short.
Not wanting to take your fun away, but there is the ruggeduino

Hmm, I’m not sure I see it in this photo. I’m not trying to argue, just trying to understand.

Wouldn’t the way they wired the button in the example make sure that a short isn’t possible?

The example uses a pull-down resistor, and while +5V is at the button, wouldn’t setting Pin2 to “OUTPUT” make it send +5V as well?

The only path to GND would be via the resistor, and therefore not a short?

I started this thread based on pull-ups, where I see the potential issue, but I can’t see it on the pull-down example…

If you set the pin to output mode, the voltage on the pin will be 0V, with a low output impedance. If you connect that to +5V by pressing the button, you create a short.

I don't think it's a good idea to use external pull-down resistors, I'd use the internal pull-ups, even if the board is targeted at beginners.
You wouldn't have your kids learn driving in a "learning car" that has the accelerator pedal on the left and the brake on the right, if in a real car, the accelerator pedal will be on the right and brake on the left.
The consequences might not be as severe, but it's good to pick up good habits early on.

Another problem is that you can't turn off the external pull-down resistor, so if the user would want to use the input with a sensor that has an open-collector output (which is very common), that's not possible because of the resistor.

If you really want to prevent people from shorting out the pin through your button, you could add a 470Ω resistor in series with the button.

Paul__B:
The big blunder with that tutorial is that it suggests connecting the button to Vcc as the first option. It is almost always preferable, simpler in design and safer to have buttons or switches connected to ground.
True enough but seriously, if you are going to make that sort of mistake in code, you are going to have a lot of problems and not just this one!

There a school of thought that says any hardware that can damage itself due to a software bug is
simply a broken design. Its usually false economy in a commercial setting - accidentally uploading
the wrong firmware is not unknown after all...

PieterP:
If you set the pin to output mode, the voltage on the pin will be 0V, with a low output impedance. If you connect that to +5V by pressing the button, you create a short.

I don't think it's a good idea to use external pull-down resistors, I'd use the internal pull-ups, even if the board is targeted at beginners.
You wouldn't have your kids learn driving in a "learning car" that has the accelerator pedal on the left and the brake on the right, if in a real car, the accelerator pedal will be on the right and brake on the left.
The consequences might not be as severe, but it's good to pick up good habits early on.

Another problem is that you can't turn off the external pull-down resistor, so if the user would want to use the input with a sensor that has an open-collector output (which is very common), that's not possible because of the resistor.

If you really want to prevent people from shorting out the pin through your button, you could add a 470Ω resistor in series with the button.

But how does it work when you use pull-ups instead? I generally don't like internal pull-ups, because the end design would typically need an external one.

I just don't get how the pin will be 0V... What if I use pull-ups; It's connected between GND (0V) and a GPIO pin (0V)... and a pull up to 5V?

I thought the GPIO had to be 5V.

DIYglenn:
I generally don't like internal pull-ups, because the end design would typically need an external one.

No, that's not true. 99% of Arduino projects work perfectly fine with the internal pull-up resistors.

In the rare case that the internal pull-ups are too weak, you can use external pull-ups. (E.g. when using long wires or switches that require some wetting current.)
Pull-down resistors are rarely ever used.


R2 is the internal pull-up resistor.
When the switch is open (released), the voltage is 5V, so it is HIGH. When the switch is closed (pressed), the voltage is 0V, so it is LOW.

Hi,
Can I suggest, as I seem to be doing a lot of late, that you get a DMM, even a cheap under $20 unit will do what you need, to look at the low voltages you are dealing with.

Then you can measure the voltage at the various points on your circuit to see how it operates.
Also the resistance or continuity range will help to make sure you have the tactile button oriented correctly.

Tom... :slight_smile: