Lesson 3 - Blink led





The first program we will be writing to get started with Arduino will be Blink LED. The Blink LED program is a physically computed program similar to a common beginner program known as 'Hello World!'.


An LED is an electronic component and stands for Light Emitting Diode. It is a small light which has a very low power consumption. The Arduino has one built in LED which is internally connected to digital pin 13.


First, we are going to declare a global variable name to digital pin 13. This is so we can use an alias name instead of 'pin 13' throughout our code. We need to also define the data type for this variable which will be an Integer. An integer can be read and write or just read-only. For this variable, we would like it to be read-only, and this would be called and constant integer.




const int LED = 13;







In the next part of the program, we will be focusing on the 'void setup' function. This will configure the Arduino with initial instructions before it proceeds to the main instruction set. For this program, we only need to setup the serial monitor and pin mode, for the digital pin.


So that we can use the serial monitor throughout our program, we must set a specific baud rate for serial data transmission between the PC and Arduino. The baud rate is commonly set at 9600 bits per second.




Serial.begin(9600);



Whenever you use any digital pin on the Arduino, you must justify in the setup whether it is being used as an input or an output. This is how we would declare the pin as an output, and similarly replace OUTPUT with INPUT if the pin was being used as an input:




pinMode(LED, OUTPUT);





Now that we have setup our program, we are ready to write code within 'void loop' to instruct the Arduino. We need to blink the LED, and we do this using the digitalwrite() function. Two of these are required to turn the LED on and off. If these two functions were just alternated, we will not be able to visually see the LED blink, therefore we must include intervals between the functions. This is done by using 'delay()' which works in milliseconds.

HIGH - Digital Logic 1 - ON

LOW - Digital Logic 0 - OFF




digitalWrite(LED, HIGH); delay(1000);



To use the serial monitor to see what is happening at each stage of the code we can add messages to pinpoint a certain stage. 'Serial.print()' displays the written message within the function in to the serial monitor. To have a neater view within the window use 'Serial.println()' to print the message on a new line.




Serial.println("LED is on/off");



The code is complete and is ready to be compiled and uploaded to the Arduino. If there are any errors or bugs within the program, these will be displayed at the bottom in the status area. Once the code is compiled and loaded to the Arduino, open up the serial monitor, at 9600 baud, and see what happens. You will be able to see messages, simultaneous to when the LED is on or off.







Source Code



We recommend copying the source code to the IDE



Code


By Zaqyas Mahmood, Electronics Engineer