Controlling the LEDs on the Interface Nanoshield

In this experience, let's explore the topic "pins". Each LED (just like the other inputs and outputs of your Beginner's Nanoshields Kit) is connected to a specific pin.

!Important Information

The Arduino features 20 pins, which are numbered from D2 to D13 and from A0 to A7. Each input or output device that one wants to control (such as LEDs and buttons, for example) uses one of these pins, such as D2, D4, D11, A1, etc.

Each pin can be controlled separately. For instance: the D4 pin can be defined as HIGH at the same time that the pin D5 is set as LOW. Some pins (like we will see later) have special properties, but almost every one of them (except A6 and A7) are, at least, capable of performing digital input and output functions.

In this project, we'll work with two LEDs that are available on the Interface Nanoshield. By clicking on the link you can check all the informations that are needed about this shield. The yellow LED is connected to A1 pin and the green LED is conected to A2 pin.

!Identifying the Components

On the figure below, we can see the yellow and green LEDs being used in this project.

LEDs Interface

!Code Components

In our last section, we quickly explained the pin parameter. At this point, though, a better explanation is useful, since we are going to use it more than once.

Pin Mode: pinMode([pin], [INPUT/OUTPUT])

Each pin in an Arduino can be used both as input and output, and one of the programmer's (your) tasks is to tell each pin's function. In order to tell which the function of a pin is, you use the pinMode() function. pinMode() receives two parameters inside the parentheses. The first value is the number of the pin that you wish to define as input or output. The second parameter is the mode, that can be input - INPUT - or output - OUTPUT. These parameters are case-sensitive, which means that the upper case letters are considered different from the lower case ones in the parameter's name. In this case, the values INPUTand OUTPUT need to be all written in upper case.

On the Arduino, the pins named from D2 to D13 are called by their numbers, that is, without the "D" letter in front of it. The pins named from A0 to A7 need to be called by their complete name, that is, preceded by the "A" letter.

We can then say that the Arduino function that configures the D4 pin as output is pinMode(4, OUTPUT);, and the function that sets pin A3 as input is pinMode(A3, INPUT);.

Normally, you'll put these calls to pinMode() at the beginning of the program, inside the setup() function. The declaration just needs to be executed once, right at the beginning of your program, and the pin will keep its state until the end of execution of the program.

In this program, since we are going to use the pins to turn the LEDs on and off, they'll be configured as output.

!Running on Arduino

Run the code below on your Arduino in order to control the LEDs. In the code you can check out the comments to better understand how it works.

/*
LEDs: Control Program
How to turn a LED on and off 
*/

// Matching the LEDs to the corresponding pins
int yellowLed = A1;        
int greenLed = A2;         

void setup() 
{          
    // Initialise each LED pin as output
    pinMode(yellowLed,OUTPUT); 
    pinMode(greenLed,OUTPUT);
}

void loop() 
{
    // Turns yellow LED on
    digitalWrite(yellowLed, HIGH);                   
    // Waits one second    
    delay(1000);                             
    // Turns yellow LED off
    digitalWrite(yellowLed, LOW);  
    // Waits one more second
    delay(1000);                             
    // Turns green LED on
    digitalWrite(greenLed, HIGH); 
    // Waits one second 
    delay(1000);                             
    // Turns the green LED off
    digitalWrite(greenLed, LOW);  
    // Waits one more second
    delay(1000);                             
}

!Your turn!

Try to modify the code above so that the LEDs turn on and off at the same time. You can check the answer by clicking the tab below.

Answer

/*
LEDs: Control Program
How to turn each LED on and off 
*/

// Matching the LEDs to the corresponding pins
int yellowLed = A1;      

int greenLed = A2;         

void setup() 
{          
    // Initialise each LED pin as output
    pinMode(yellowLed,OUTPUT); 
    pinMode(greenLed,OUTPUT);
    digitalWrite(yellowLed, LOW);
    digitalWrite(greenLed, LOW);
}


void loop() 
{
    // Turn the LEDs on
    digitalWrite(yellowLed, HIGH);                 

    digitalWrite(greenLed, HIGH); 
    // Waits one second    
    delay(1000);                           

    // Turn the LEDs off 
    digitalWrite(yellowLed, LOW);  

    digitalWrite(greenLed, LOW);

    // Waits one more second 
    delay(1000);                             
}