Pages: [1]   Go Down
Author Topic: Basic question about switches  (Read 356 times)
0 Members and 1 Guest are viewing this topic.
Brazil - Rio de Janeiro
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Valencia, Spain
Online Online
Faraday Member
**
Karma: 146
Posts: 5506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: