Basic question about switches

Hi.

It's a very simple and basic question:

Why shouldn't I use a direct connection from 5v pin to an input digital pin, and separetelly only by a switch, instead of adding a resistor and connect to ground that circuit?

I mean, the general suggested circuit to use with a switch is very simple, but I can't understand why don't use a even much more simple em direct approach like, for example, take a battery and plug it in the digital pin with a switch button (and of course plug the negative of the battery in the ground of the Arduino).

I don't know how stupid could be this question, but I've got different answers for some people and I feel here's the best place to get the right answer.

Thanks!

[]s
Alex

Because without the resistor to pull up or pull down, when the switch is disconnected the circuit is open, which means the input is floating and can give undefined results. The pull up or pull down ensure that you have a known condition when the circuit disconnects (open circuit).

alexander_ino:
Why shouldn't I use a direct connection from 5v pin to an input digital pin, and separetelly only by a switch, instead of adding a resistor and connect to ground that circuit?

You could... but the usual convention is to do do it the other way around. That's what people expect... and it's a good idea not to change things randomly.

There's some other circuit designs where switching to ground works much better - very long wires, electrically noisy environments, etc. This doesn't really apply to Arduinos but again it's better to use one design everywhere, not pick one randomly. That way the people who have to look at other people's circuits don't have to spend five minutes figuring out how basic things like switches are wired up.

PS: You don't need a resistor when connecting to ground, there's a resistor inside the chip. You would have to add an extra "pull down" resistor if you were going to do it your way and connect to +5V.

take a battery and plug it in the digital pin with a switch button

You can, but how many batteries do you have?

The standard approach (of using a pull-up resistor to rail) is essentially the same thing, except that they moved one step further and used that rail + resistor as the battery - much simpler, lower cost and takes up far less space / maintenance.

Hard to think anyone favoring the battery over the pull-up resistor.