Arduino 3: Digital Input

Image made with Fritzing (

To make the Arduino react to the outside world, it must be able to read input data. The simplest way to do this is to use digital input – reading whether a switch is on or off, and using that input to switch between doing two actions. Pins 1-12 along the top edge of the Arduino can all read digital input, but note that pin 13 can’t , because it’s got a resistor and LED on the board attached to it – only use 13 for output.

For a simple example, let’s use a button and two LED’s. When the button isn’t pressed, a red LED lights while a green LED remains off. When the button is pressed, the red turns off and the green one lights. The parts include two LED’s, the long leads going to Arduino pins 6 (green) and 7 (red), the short leads of each connected to 220-ohm resistors (red-red-brown) and then to the ground pin. There’s also a push button, one end connected to +5V, the other end connected both to the Arduino pin 8, and also to a 10K resistor (brown-black-orange) connected to ground. See the  diagram above.

Enter the program:

int button;
void setup(){
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
void loop(){
button  = digitalRead(8);
if (button==HIGH) {
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
else {
digitalWrite(6, HIGH);
digitalWrite(7, LOW);

The first line

int button;

declares a variable button to hold integer numbers.  The function


read the wire connected to Arduino pin pin_number, and returns either a 0 if the pin is LOW (voltage between 0 and 2.5V ), or a 1 if the pin is HIGH (voltage between 2.5 and 5V). In this case the value is stored in the variable button. Then the conditional statement

if (logical test) {
what to do if true
} else {
what to do if false

decides which of two actions to perform. In Arduino, LOW and HIGH are the same as the integers 0 and 1. So the statement button == HIGH tests the value of button to see if it equals 1. Note two equal signs == perform a test. If we just typed button=HIGH, it would store the value 1 into the variable button. You could also use symbols <, >, <=, >=, != (not equal) in the test as well.

6 responses to “Arduino 3: Digital Input

  1. Sorry forgot to post the error!(:
    expected unqualified-id before ‘else’
    error: expected declaration before ‘}’ token

  2. It works but this error keeps on appearing.

    P.S. Im receiving the signal from an Xbee and a internet shield.

  3. Where did you get the spiffy graphics for the Arduino and proto board. I would love to add something like that to labs along with a schematic…

  4. Thanks for the great diagram. I’m having issues running several (3) digital switches together. All switches work independently, but when running together they do not deliver any input. Any advice will greatly be appreciated, I’m a student and relatively new to this.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s