Button, why a connection to ground? - Noob questions

I'm new to the stuff and while not completely an idiot in that regard (did some things that did work ... kinda at least)
I still have some unanswered beginner questions I couldn't find the answer.

In all arduino examples with buttons they are also connected (usually with a 10k resistor) to ground. Making the power flow not only between 5V and input, but also to ground.

Why is that so? Shouldn't the circuit be complete with just a connection to the input pin?

I certainly don't want to have energy flow more than necessary (plan to do a few things that run on battery)

if I have to, what is "best" value? (in Tinkercad it still works with a 100MOhm)

2 Likes

Lots of ways to connect switches. Have a look (thanks larryd):

Each method has pros and cons. Which configuration are you referring to?

This one?

S1 In which I would (as someone who has no clue) would just put the switch in between 5V and pin 8 (in your example) and would not connect to ground.

What I can't find why I should connect it to ground (only that I should).

thaaat_guy:
In all arduino examples with buttons they are also connected (usually with a 10k resistor) to ground. Making the power flow not only between 5V and input, but also to ground.

Why is that so? Shouldn't the circuit be complete with just a connection to the input pin?

I certainly don't want to have energy flow more than necessary (plan to do a few things that run on battery)

if I have to, what is "best" value? (in Tinkercad it still works with a 100MOhm)

Because an Arduino input is unbiased. To anthropomorphize it: It doesn't care if the voltage, applied to it, is HIGH or LOW. That's what makes it an Input.

As such, it also doesn't influence the voltage applied to it. So, if you just connect a switch to it, with it's other end connected to either HIGH [~5V], or LOW [~0V] -- when that switch is closed, the nearly 0 resistance will pull the input to HIGH, if the switch is connected to ~5V, and to LOW, if the switch is connected to ~0V.

But, what happens when the switch is opened? There's the rub. An open switch has a VERY high resistance -- essentially, the resistance of air. Now, an Arduino input also has a very high resistance. But, in comparison, the resistance of air is VERY high! In other words MUCH higher than the resistance of the Arduino, which is around 100MΩ, whereas air...Resistance Of Air

So, what does the very high resistance of air have to do with what happens to that input when the switch is opened? It means that when the switch is opened, there is nothing to pull the input anywhere. The input, to use the actual technical term: floats. That's just a fancy way of saying, the state of an open input is unpredictable.

So, we add a pull-up, or pull-down resistor, on the input, to create a known condition for when the switch is opened. If the switch is connected to ~5V [the side of the switch not connected to the Arduino input], a pull-down resistor is added so when the switch is open, a LOW state is insured. The same for when the switch is connected to ground [i.e. ~0V]. A Pull-Up resistor is added to insure a HIGH state, when the switch is opened.

And, to address the question, how high a resistance [pull-down or pull-up] is too high? Well, that's a question somewhat outside my expertise. I can say this, though, the higher the resistance, the less immune to noise, the input is [as Yoda would say ;)]. Theoretically, using the math of voltage dividers, and the knowledge of the input resistance, this can be computed. Now, everyone is fond of saying that the input resistance is 100MΩ, but actually, according to the Atmega328P datasheet [Table 28-8], that's the Analog Input Resistance. The Digital Input Resistance is more like [worst case, over a temperature range of –40°C to +125°C] 5MΩ [Table 28-2: Input leakage Current I/O pin: 1µA -- at 5V, that's 5MΩ].

Also from the datasheet are the definitions of Input HIGH & Input LOW:

  • Lowest Voltage Guaranteed to be Interpreted as Input HIGH: 0.9*VCC (for a VCC of 5V, that's 4.5V)

  • Highest Voltage Guaranteed to be Interpreted as Input LOW: 0.1*VCC (for a VCC of 5V, that's 0.5V)

Lets take the case of 4.5V as the lowest HIGH:

In order to insure at least 4.5V on the input, R1 needs to be:

** **R1 = RI(VCC - VRI)/VRI = 5M(5.0V - 4.5V)/4.5V = [u]555k[/u]** **

So, there you go. For worst case conditions [the only thing specified in the Data Sheet], the maximum Pull-Up resistance to insure a HIGH state on the input, is 555k. You can do similar math to determine the Max Pull-Down resistance, but it will probably come out to be the same.

If anyone finds an error in my math, or my philosophy, feel free to post corrections :wink:

Well, two things. Firstly, if you do not connect the resistor to ground, then the input to the Arduino may never go LOW. Why would you expect it to if the resistor is not there? If you press the switch, the input is pulled HIGH and without the resistor, it will just stay HIGH when you release the switch.

Second, "In all Arduino examples with buttons" they are written by people who are not particularly competent, just learners like yourself.

The preferred way to connect a button is from the pin to ground. The resistor would then go from the pin to 5 V, but you may not need it because setting pinMode to INPUT_PULLUP will enable a built-in 47k (approximately) "resistor" in the Arduino chip to pull up to Vcc.

Why is this preferred? it is a rather bad idea to take the 5 V line anywhere you do not need to away from the Arduino itself, such as to connect to buttons outside, as there is a possible - even if minor - risk that it might be shorted to ground and it may pick up interference. If the button wire or ground is shorted to ground, that is clearly not so dangerous.

1 Like

The Fritzing drawing you posted is as ambiguous as it gets, a horrible representation of reality due to the nature of the switch contacts. This is one of the many reasons why Frtzing drawings are universally despised by those that can read schematics, they cause far more trouble than the problems they solve.

It’s no wonder you’re confused by the image, that’s why I posted the drawing. The Fritzing is attempting to illustrate the S1 circuit but you’ve a 50-50 chance of getting wrong by rotating the switch 90 degrees. It’s also unclear what the resistor value is.

The S3 circuit is the preferred method for the reasons already well explained by paul__b.

ReverseEMF:
[snip]

Thanks a lot. That's what I needed ... I think I understand now (might still the Dunning-Kruger speaking, tho)

WattsThat:
It’s also unclear what the resistor value is.

No, not really ... not if you can read resistor color code. It is even more clear than in RL ... Though I rather measure them than try to read the code. (always mistake one for the other color ... and forget what color stands for what, had a cheat sheet at some point but long lost)

I was attempting to provide the benefit of doubt for the creator of the image. Fritzings are made by people that have no idea of how to draw a schematic so they’re just as clueless when it comes to design rules. Having memorized the resistor color codes for some fifty plus years ago, I know that brown-black-violet-gold is 100 megohms at 5%. That’s only 4 orders of magnitude higher in resistance than it should be. Minor detail, right?

The Web is full of mis-information and complete nonsense. Choose your sources carefully. Anything with a Frizing is suspect with perhaps the notable exceptions of the Adafruit and Sparkfun websites.

WattsThat:
I was attempting to provide the benefit of doubt for the creator of the image. Fritzings are made by people that have no idea of how to draw a schematic so they’re just as clueless when it comes to design rules. Having memorized the resistor color codes for some fifty plus years ago, I know that brown-black-violet-gold is 100 megohms at 5%. That’s only 4 orders of magnitude higher in resistance than it should be. Minor detail, right?

The Web is full of mis-information and complete nonsense. Choose your sources carefully. Anything with a Frizing is suspect with perhaps the notable exceptions of the Adafruit and Sparkfun websites.

And yet exactly what I set it to

You can trust this site, I wrote it:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
It tells you why using a resistor to ground is a shit idea and that real professionals never wire a button like this.

Hello all,

I have an in-car multimedia head unit that has all buttons connected in this way (to ground), and I noticed that all OEM and aftermarket car parts that are equipped with buttons, have their buttons connected to ground, so I concluded that this is the preferred way to connect them.

Now, I also know why, and the article in the link above was very explanatory.

Knowing that, I want to "tap in" with my Arduino and detect when a button was pressed on the head unit, without installing a second switch under the plastic cover of the button.
Basically, what I want to do, is to connect one wire from the right terminal of the button to an input pin of the Arduino (which already shares the ground with the head unit - i.e. their ground is the same as that of the whole car).

Will this be possible?

Grumpy Mike, great write up, just wanted to add... don’t do this without a pull down.

Silkworm:
Basically, what I want to do, is to connect one wire from the right terminal of the button to an input pin of the Arduino (which already shares the ground with the head unit - i.e. their ground is the same as that of the whole car).

Will this be possible?

What is the voltage on that terminal when the button is NOT pressed? Probably 12V+ so you'll need a voltage divider on each pin to avoid blowing up the Arduino which can only take 5V max (or maybe 3.3V depending on which it is).

Steve

Yes, I am aware of that... but I was wondering if it really could be done, the way I proposed.

For simplicity, let's consider two Arduinos that share the same button.

Which of the two schematics (from the attached picture) would be correct?

Thanks.

Arduino_shared_button_correct_schematic.png

Both are the same if you read the fine print in the diagram

What fine print? The diagrams in my previous post are made by me. :slight_smile:
Are you referring to the mention that they are powered from the same PSU?

Arduino_shared_button_correct_schematic.png

The two sides of the image show the same arrangement as far as the input is concerned.

If the note about the power supplies means that Arduino 2 is connected to the same power supply as Arduino 1 then they are identical. But then you have not said how the power supply is connected to the Arduino/ You have left out the "interesting bit"

If the power supplies go into both of the Vin pins of each Arduino Arduino then don't connect the two 5V lines together as they will be different voltages given by different regulators, very close but not the same voltage.

Never connect the + voltages together unless you know what you are doing.
Always connect the grounds together unless you know what you are doing.

Grumpy_Mike:
Never connect the + voltages together unless you know what you are doing.
Always connect the grounds together unless you know what you are doing.

Thanks. Sounds logical. I will keep that in mind, as basic rules. :slight_smile:

Well, actually, there will not going to be two Arduinos. I made that schematic for simplicity. What I am actually trying to achieve is to detect with Arduino if a button was pressed on my car's Head Unit (which has the buttons connected to ground, on the same principle), so there will be the Arduino on one side, and the head unit on the other side.

And my question was if I need to add the pull-up resistor and link to 5V on Arduino, since most likely this is already done on the head unit. My intuition tells me that I don't need to add the pull-up resistor/5V connection on Arduino... but then, I'm afraid that, in this way, the input pin on Arduino will become 'floating'.

Thanks.

Internal pull-up

If the external switch connects to ground, and no where else, then connect the button to an input and have a 3K pull up resistor to 5V on the Arduino, or in the case of multiple Arduinos with all their grounds connected, to one of the Arduinos, it does not matter which one.

However, if there is any other wiring to the non ground side of the button you must remove it before connecting it to your Arduino.

It is likely that this could be the lights powered at one end, but not lit due to the fact that the button connecting it to ground is open. In that case the non ground side of the switch will be at a high voltage when the lights are off and connecting that to the Arduino it will, I guarantee, instantly fry your Arduino, or at best that input pin. You need to measure this before you connect anything up.

If there is a voltage there, and you still want the lights to turn on when the button is pressed, then you will need something to either cut down this voltage, like a potential divider, OR isolate the voltage ( this is best ) with an opto isolator.

I think in this case of a hostile environment the internal pull up will not be strong enough and will be prone to picking up interference.