Turning on a LED

Turning a LED on and off is a basic experiment when dealing with Arduino. But why? Well, blinking a LED is what we can call "Hello, world!" of electronics. Normally, showing the "Hello, world!" on the screen is the first task a programmer has to complete when he/she is learning to program. However, in this case, since we are using an Arduino, let's make the LED blink instead of printing a message on the screen.

!Important Information

In this project we'll use two Arduino functions: digitalWrite() and delay()

The word "digital" means that the output can only have two states: on or off. We can also refer to these two states as HIGH/LOW or 1/0. On the circuit we are going to use, when the output is at logic high level (HIGH or 1), the LED turns on; and when the output is at low level (LOW or 0), the LED turns off.

The word "write" means to set a digital state (HIGH/LOW) in one of the Arduino pins. This is a command that is used when the pin has function of output (we'll see more about inputs and outputs on the next projects).

The command "delay" is used to keep the processor idle for a certain time.

!Identifying the Components

The figure below shows the position of the LED that we are going to use in this experiment. The LED is identified with the L letter. Right above it, there are 3 LEDs, which are identified as TX, RX and ON. The LEDs TX and RX are used to indicate the functioning of the serial bus; and the ON LED keeps on while there is power supply to the Arduino board. This LEDs work independently, that is, it is not possible to control it directly, only the L LED can be turned on and off through our software.

LED Mosfet

!Code Components

In this part, we show the two functions that are needed in order to turn the LED on and off, digitalWrite() and delay().

Digital Output: digitalWrite([pin], [HIGH/LOW])

The digitalWrite() function is used to define the state of a pin - high or low.

The first parameter, pin, is used to identify the pin that is triggered and you'll explore it more in the next experiments. For now, let's concentrate only on the second parameter.

When a pin is HIGH, it produces about 5V on the output. This voltage makes a current go through the LED and it turns on. When the pin is LOW, the output voltage is 0V, which causes the current to stop going through the LED and it turns off.

Before using digitalWrite(), it is very important to make sure that the used pin was previously configured as output, because on the contrary, it'll not work properly. In order to configure the pin, we use the command pinMode(), which is inserted usually inside setup(). This way, we have to insert the code line pinMode(13, OUTPUT);, for example, in case we want to use the pin D13 as output (we'll see more about Arduino pins on the next projects).

Milliseconds Delay: delay([ms])

The Arduino executes the code so fast that sometimes we need to make it slower by adding a delay. This function will interrupt the program for a certain amount of milliseconds. One second means 1000 milliseconds (ms). Thus, if the parameter is 1000 (ms), the delay will be one second.

!Running on Arduino

Run the code below on your Arduino to turn the LED on and off. On the code below you can check the comments in order to understand better how it works.

void setup() 
{
  // Configures d13 pin as output
  pinMode(13, OUTPUT);
}

void loop() 
{
  // Configures pin 13 as HIGH
  digitalWrite(13, HIGH);
  // Waits 1000 ms (1 second)
  delay(1000);             
  // Configures pin 13 as  LOW
  digitalWrite(13, LOW);   
  // Waits 1000 ms (1 second)
  delay(1000);            
}

!Your turn!

Try to modify the code above so that the LED stays on for 5 seconds. The answer you can check by clicking on the tab below.

Answer

void setup() 
{
  // Configures d13 pin as output 
  pinMode(13, OUTPUT);
}

void loop() 
{
  // Configures pin 13 as HIGH
  digitalWrite(13, HIGH);
  // Waits 5000 ms (5 seconds), that is, the LED remains on for this time
  delay(5000);             
  // Configures pin 13 as LOW
  digitalWrite(13, LOW);   
  // Waits 1000 ms (1 second)
  delay(1000);            
}