Pages: [1]   Go Down
Author Topic: What is the minimum wiring for two switches?  (Read 1311 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 340
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would the wiring diagram below be extended to add a switch to the circuit? I already have a pushbutton on pin 2. I want to also put a simple, on/off switch on pin 3.

Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4058
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The minimum wiring for a switch is to connect the switch between the microcontroller pin and ground, then in the sketch, turn on the internal pullup resistor. This will cause the pin to be HIGH when the switch is open (or if it's a normally open button switch, when the button is not pushed), and LOW when the switch is closed (button pushed). That might be backwards from the obvious way to think about it, but it saves the external resistor. For two or more, just connect each switch between a pin and ground.

Set the pin up as follows:

Code:
void setup(void)
{
    pinMode(2, INPUT_PULLUP);    //set pin 2 as input with internal pullup resistor enabled
}
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 340
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so I could've done the same for the push button too? why do they show the inferior resistor schematic if it is possible to to it without the resistor?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so I could've done the same for the push button too? why do they show the inferior resistor schematic if it is possible to to it without the resistor?

Who knows. First there is no 'they', most tutorials and guides are the result of one persons effort. Perhaps he/she was trying to build up the users knowledge of basic electronics and the components one uses to built circuits, and using a hidden, software enabled, pull-up resistor was not what the author though was a better way to teach such concepts as pull-ups and pull-downs. One should understand both methods and when one method is better then the other. Note there is no software pull-down option, so learning about how they work is a useful lesson.

 But yes, when interfacing to simple passive switch contacts, using the internal pull-ups is the better method.

Lefty
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 49
Posts: 1611
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One can only guess to answer that question, as you don't tell us who has told you to do so.
One reason was already given:
If you or someone else has trouble with the fact that a non pressed button will result in a HIGH and a pressed button will result in a LOW, this is a hardware way to solve that.
Ofcourse you'd have to consider cost of the resistor (a few cents to buy it, some more to integrate it in your design and (relatively) a lot more space on your board) to that of programming in such way that you (so you, the Arduino doesn't give a rats *ss) will keep track of the status of that button / switch.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 340
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This tutorial  http://arduino.cc/en/tutorial/button should instead have the method proposed by Jack Christensen in it instead of what it currently says or at least, both.
« Last Edit: February 09, 2013, 08:02:13 pm by David82 » Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 49
Posts: 1611
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes and no.

You are right that this should be added to that thread and the code should be offered as an alternative.
So the text in it is OK, because it is correctly explaining what is happening (and why) and also what happens if you change the pulldown to a pullup.
It just doesn't tell you about internal pullup and the advantages to it.
I think (so i'm not sure) you can do that yourself, or else request this to be done while offering the text, pictures and sketch to get it done.

Do you realize that this page and the sketch was aready created 8 years ago ?
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2883
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And neither one talks about the disadvantages of the weak pull-up in a noisy environment which is why the first method with the external resistor is preferred... It Always works because the pull-up is tailored to the noise and environment. Although usually with a small bypass cap and a bead if really noisy. And that is about 99% of the knowledge on the subject. The only un-broached subject here is ESD protection for the pin and yes I Have killed processors with a poor quality keypad and extreme ESD generated by sliding across a car seat and touching the keypad. So If you are real concerned use 1N4148 diodes from the input pin to Vcc and to ground and a 10 K resistor to the processor input itself. I would.

Bob
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 145
Posts: 5456
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so I could've done the same for the push button too? why do they show the inferior resistor schematic if it is possible to to it without the resistor?

The resistor inverts the signal. When you press the button the input goes HIGH.

Without the resistor the input goes LOW when you press the button, this is confusing to newbies.

Logged

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

Offline Offline
Sr. Member
****
Karma: 9
Posts: 296
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pictures! Maybe this will make it clearer.

Personally, I prefer using an external resistor. It makes the schematic more clear, in what it's trying to do.



* switch.jpg (38.11 KB, 800x598 - viewed 39 times.)
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have long wires between the switch or pushbutton and the Arduino, use an external pullup resistor, or at least make provision for one when designing a PCB. If they are close to each other, use the internal pullup resistor - that's what it's there for.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Full Member
***
Karma: 1
Posts: 157
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
   pinMode(2, INPUT_PULLUP);    //set pin 2 as input with internal pullup resistor enabled

Btw. are the internal pullups, once activated by "pinMode(x, INPUT_PULLUP);" reseted after uploading a new sketch?  I know auto-deactivating is now (IDE >= 1.0.1) done by explicitly setting the pin in the same sketch to output (see last paragraph http://www.adafruit.com/blog/2012/05/25/arduino-ide-1-0-1-new-features/). But what is happening after uploading a new sketch? I assume they are deactivated but I'm not a deep Atmel guy so I will not count on Guessing. :-)
Logged

Dubuque, Iowa, USA
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2500
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The IO pins are all set to input / high impedence when the chip is first powered on or after a reset. Nothing will be "on" until you explicitly command it in your code.

It's a safety thing, or just makes sense I guess.
Logged

Pages: [1]   Go Up
Jump to: