CHAPTER 3: IN-BUILT LED BLINK
Things required for this chapter
Arduino Uno/Uno (Arduino compatible board)
USB Cable (Type A/B Standard USB 2.0 cable)
If you have learnt programming, the first thing you will learn is to write a program to print “Hello World”. Similarly, while learning to develop using Arduino platform, we start off by writing a program to blink the Debug LED, which is in-built in an Arduino Uno board and connected to pin 13. As mentioned earlier, when we program with Arduino, we will have to make hardware setup and write codes accordingly. Next, let us do these steps.
Hardware setup: Since we are using the in-built LED, we just have to connect the Arduino to the PC using the USB cable.
Sketch setup: Writing a sketch is simple. STEP 1: Open a new sketch
Open the Arduino IDE and open a new sketch. Now we will get something like this
Figure: A new Arduino Sketch
STEP 2: Setting up an Arduino Board in Arduino IDE To set the type of board you are using, you should go to - Tools menu > Boards > “Select your board”, as mentioned in the figure below.
Figure: Setting up an Arduino board in Arduino IDE
STEP 3: Writing a sketch Have a look at the sketch below and try to understand it out before looking at the explanation (If you are already familiar with it, you can skip to the next step).
Figure: Sketch for in-built LED Blink Program
Explanation of the sketch:
Writing Comments in our code is a good practice as you can refer to it at any time in your sketch. To write a comments in our sketch, we use these symbols – // (for single line comment) or */ /* (for multi-line comment).
Here we are introducing a new variable called LED and assigning it a value of 13. We assign this at the beginning in global variable so as to make easy changes to the assigned pin number throughout the sketch when we have any changes in our INPUT/OUTPUT pin later on during our project, saving us the time required to change each and every place were the pin was assigned.
void setup () runs only once when the sketch is started or reset. Here we declare the mode of a pin, or initialize any other functions like Serial monitor etc.
pinMode(LED, OUTPUT); function sets the pin as an INPUT or OUTPUT. LED defines the pin it is connected to (In our case pin 13) and OUTPUT defines the pin as an OUTPUT pin.
void loop () runs the program continuously unless its stopped.
digitalWrite(LED, HIGH); function defines which pin will do what. Here, the pin defined is LED (or pin 13) and HIGH defines that the pin value is HIGH.
delay(500); function defines the time that the function previous to delay() will run. This is in milliseconds. In our case digitalWrite(LED, HIGH); will run for 500ms and then it will go to digitalWrite(LED, LOW);
STEP 4: Compiling and Uploading the sketch to Arduino Uno First save your project in your desired destination and then press the compile button. During the compile process, the Arduino IDE will show you if there are any errors in your sketch. If there are no errors, click Upload button to upload sketch to your Arduino board.
Here is a representation of what the Arduino will output after running the sketch on it.
At any point of time, if you feel that learning through this blog is not your thing, please feel free to explore other mediums like Books, YouTube videos, Arduino Forums or other Blog posts.
If you want to add something interesting or want to point out any errors on this article, please feel free to comment below.
The Arduino Community logo is used under the Creative Commons license CC-SA-BY-NC 3.0