Go Down

Topic: What is the minimum wiring for two switches? (Read 1 time) previous topic - next topic

David82

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.


Jack Christensen

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: [Select]
void setup(void)
{
   pinMode(2, INPUT_PULLUP);    //set pin 2 as input with internal pullup resistor enabled
}
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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?

retrolefty


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

MAS3

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

David82

#5
Feb 10, 2013, 01:51 am Last Edit: Feb 10, 2013, 02:02 am by David82 Reason: 1
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.

MAS3

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 ?
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

Docedison

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

fungus


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.

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

vasquo

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.


dc42

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

Clemens



Code: [Select]

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

Chagrin

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.

Go Up