0
Offline
Sr. Member
Karma: 0
Posts: 340
Arduino rocks
|
 |
« on: February 09, 2013, 06:01:35 pm » |
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
Faraday Member
Karma: 47
Posts: 2575
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #1 on: February 09, 2013, 06:30:39 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 340
Arduino rocks
|
 |
« Reply #2 on: February 09, 2013, 06:50:22 pm » |
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
Brattain Member
Karma: 283
Posts: 15443
Measurement changes behavior
|
 |
« Reply #3 on: February 09, 2013, 07:19:36 pm » |
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
Sr. Member
Karma: 10
Posts: 383
|
 |
« Reply #4 on: February 09, 2013, 07:20:01 pm » |
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
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 340
Arduino rocks
|
 |
« Reply #5 on: February 09, 2013, 07:51:08 pm » |
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
Sr. Member
Karma: 10
Posts: 383
|
 |
« Reply #6 on: February 09, 2013, 08:18:27 pm » |
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
|
|
|
|
|
Anaheim CA.
Offline
Edison Member
Karma: 34
Posts: 2405
Experienced old Whitebeard with a Full head of Hair...
|
 |
« Reply #7 on: February 09, 2013, 11:36:26 pm » |
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
|
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
|
|
|
|
Valencia, Spain
Offline
Faraday Member
Karma: 72
Posts: 2503
|
 |
« Reply #8 on: February 10, 2013, 12:06:05 am » |
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...
|
|
|
|
Offline
Sr. Member
Karma: 9
Posts: 295
|
 |
« Reply #9 on: February 10, 2013, 01:44:46 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
United Kingdom
Online
Faraday Member
Karma: 146
Posts: 4908
|
 |
« Reply #10 on: February 10, 2013, 12:28:51 pm » |
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. http://www.eschertech.com
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 85
|
 |
« Reply #11 on: February 12, 2013, 09:30:03 am » |
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
Edison Member
Karma: 15
Posts: 1587
|
 |
« Reply #12 on: February 12, 2013, 12:48:31 pm » |
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
|
|
|
|
|
|