What is the minimum wiring for two switches?

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.

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:

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

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?

David82:
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

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.

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.

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 ?

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

David82:
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.

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.

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.

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 Arduino IDE 1.0.1 – New Features « Adafruit Industries – Makers, hackers, artists, designers and engineers!). 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. :slight_smile:

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.