Controlling Digital I/O

As the very first operation, we will turn on the LED that is already mounted to the Arduino Board next to pin 13, as shown in the figure below.

image

Each digital pin on the right side of the board is labeled 0 to 13. The pin labeled 13 is significant since it is the only one with an LED built on to the board. By turning on pin 13 to a HIGH, we can light the LED.

The first step is to set the digital pin as an output. We do this with the pinMode command. This will identify the pin and how it will be used, as an INPUT or an OUTPUT.

Add the line pinMode(13, OUTPUT); in the set up () section. The next step is to write the digital value to the pin with the digitalWrite(13, HIGH); command. The actual sketch looks like that below.

void setup() {

  // put your setup code here, to run once:

pinMode(13,OUTPUT);

}

 

void loop() {

  // put your main code here, to run repeatedly:

digitalWrite(13,HIGH);

}

 

To run the sketch, just push the upload button and the light on the board will turn on. The board will look like the figure below.

image

Congratulations, you have just written your first sketch and made the Arduino do something.

Important debug note: You will have noticed that each line of the code you entered has to end with a semicolon, “ ; “. This is the first thing to check

Important debug tip: If the LED is on but very dim, this is usually because you forgot to include the pinMode(13,OUTPUT) command in the setup() section. The default setting for the pinMode() command is INPUT_PULLUP, which puts a 10k resistor in series with the LED.

 

Turn the LED off by changing the word “HIGH” to “LOW” in the digitalWrite command.