HOME - - - - - - - - Table of contents, my Arduino "How To" articles
Other material for programmers

Simple Inputs

Connecting switches, buttons and the like



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.

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.

Diagram of connecting a switch

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.


What's happening, in the hardware, is....

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.


A clever convenience

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



Diagram of connecting a switch

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

pinMode reference.

digitalRead reference.

General discussion of the pins, their use and function.



   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Open Office version 2. (If you experienced Adabas with Star Office, ooBase is nothing like it!)
Some pages for programmers.
Using the parallel port of a Windows computer.


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.




Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

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?

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.

Link to editor's oldest homepage.


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....