The following shows you how to connect an input to your Arduino. The switch can be momentary, like a doorbell's switch, or a toggle switch, like a light switch.
If you know the basics, but need help with the issue of contact "bounce", I have a page about that for you, too.
The resistor can be a common 1/4 watt resistor, or even a 1/8 watt resistor. (Although these tend to be a little fragile.) It's resistance should be about 10k, or something like that.... 1k would work, but drain batteries faster than necessary and 20k or even 50k would probably also work, but you may begin to get false readings of "switch closed" if the resistance gets absurdly high.
So much for the hardware. We'll look at why it works in a moment. But first the software to go with it. The following is Badly Written, but it will suffice to illustrate some elements of reading an input. For the program below to work, the switch and resistor must be connected to pin 4, and an LED must be connected, with the normal resistor, to pin 13... the pin that has an LED and resistor already, on the PCB, in a Diecimila, a ModernDevice BBB and an Adafruit Boarduino.
void setup()
{
pinMode(13,OUTPUT);
pinMode(4,INPUT);
}
void loop()
{
if (digitalRead(4)==HIGH)
{digitalWrite(13,HIGH);}
else
{digitalWrite(13,LOW);};
}
N.B. It is very easy to get things backwards in your mind. The way things have been done in this "How To", when the switch is closed the input reads "low". Most push buttons are "normally open" (i.e. open until you press them, like a doorbell push button). You can, however, buy normally closed push buttons. (Don't wire up your doorbell with one. The bell will ring until someone pushes the button!) You might want to pick up a few normally closed push buttons the next time you are shopping. If you have a serial device attached to your Arduino via pin 0 or pin 1, then messages to/ from it during the programming of the Arduino can be a nuisance. (The programming messages also flow over pins 0 and 1... although, with care, such "double use" is possible.) Putting a normally closed pushbutton on the line to (from) the device means that you can temporarily disconnect it, during a program upload, simply by pressing down on the pushbutton during the programming process.
Virtually no electricity flows into (or out of) the Arduino over a pin that has been set for input with the pinMode command. But that pin can "read" any voltage connected to the pin. It is almost as if it were a voltmeter, but it only returns "high" or "low" for an answer.
An aside: If you want to read the voltage as "none", "very low", "quite low", etc up to "high", then you can... but you need to use one of the analog inputs. But that's a story for another page. (The "answer" is, of course, expressed as a number. Between 0 and 1023, as it happens.) End of aside.
When the switch is open, the Arduino's input "sees" the 5v it is connected to, even though the connection is through a resistor. Because almost no current is flowing, the voltage drops very little between the 5v supply and the Arduino pin.
On the other hand, as soon as the switch is closed, some current does flow. (It flows through the switch, to ground.) Now the Arduino "sees" the virtually no resistance path between its pin and ground; it "sees" the zero volts. The 5v it is still connected to is "hidden" behind the resistor because the voltage "drops" when there's significant current through the resistor.
Such resistors are called "pull up" resistors because they pull the voltage at the input up when there's not low-resistance connection to ground.
You can, if you wish, wire up your inputs as above. The circuits will work just fine.
The nice Arduino (or was it the Atmel) people have built something into the Arduino in a really clever, easy to use, and easy to not use, when not wanted, manner
Look at the following....
There are 20k pull up resistors built into the Arduino, inside it. Not only are they present, but you can "connect" and "disconnect" them!
When the Arduino is powered up from "dead" all of the pull up resistors are disconnected. The following illustrates how to connect one....
void setup()
{
pinMode(13,OUTPUT);
pinMode(4,INPUT);
digitalWrite(4,HIGH); //Only this line new
}
void loop()
{
if (digitalRead(4)==HIGH)
{digitalWrite(13,HIGH);}
else
{digitalWrite(13,LOW);};
}
You see the line just after......
pinMode(4,INPUT);
The one that says.....
digitalWrite(4,HIGH); //Only this line new
It doesn't make "sense". Why would you write to a pin that has just been set up for input? That digitalWrite(4,HIGH); doesn't do what you might think it does. It doesn't make the pin high. It "connects" the internal pull up resistor to the circuit, so that we can simply hook up a switch to ground, and have a reliable input without needing an external pull up resistor.
without a pull up resistor, be it internal or external, we know that the input will read "LOW" when the switch is closed, connecting the input to zero volts. But when the switch is open, the input isn't connected to anything, and it will sometimes report "HIGH" sometimes "LOW"... unpredictably, and unreliably. Such an input is said to be "floating".
(It is, by the way, okay to have floating inputs on unused lines. Just don't look for sensible information from a floating input.)
I hope that has made things more clear for you. The official Arduino pages have a similar resource, if you want to read more on the subject.
There's also the following at the Arduino site....
General discussion of the pins, their use and function.
If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.
Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.
Click here to visit the homepage of my biggest site.
Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....