Now let us jump into some hands-on activity. In this section we will study an existing Arduino sketch that shipped with the Arduino IDE. The sketch we are going to study is the world famous "blink" program, which is like the Hello World of the embedded programming world. The Arduino IDE installation comes preloaded with a lot of example sketches. All the example sketches are available for use from the File > Examples menu.
Once you attain a minimum level of understanding, you will be able to start exploring more of these examples on your own. Let us inspect and understand the blink program. Fire up the Arduino IDE on your computer and navigate to the File > Examples > 01.Basic > Blink menu. The Blink program will open in a new Arduino IDE window.
The first point to note in the program is the one time configuration that has been done in the setup() function:
pinMode(13, OUTPUT);
The above line of code tells the Arduino board to configure digital pin number 13 in output mode. This is an important step, as it sets up digital pin 13 for output of signals from the microcontroller board.
In the blink program's case, the onboard LED is internally wired to Pin 13; hence we do not have to make any additional wiring or connections for this setup. Hence all we need to do is to compile and load the sketch.
When writing an Arduino sketch the first thing to do is to identify and appropriately setup the input/output modes of the pins being used in the device prototype. The Arduino pins connected to a peripheral device must be explicitly set in input/output mode.
The next important part of the sketch is the loop() function, which houses the instructions that are repeated over and over again. This function runs infinitely and is exactly how a micro-controller program operates. In the blink program's loop() function you will be introduced to two important in-built functions that are very commonly used in Arduino sketches. The first function is:
digitalWrite(13, HIGH);
This above function sends a HIGH digital signal on the pin number specified. In this case a HIGH signal is sent out via digital pin 13. In turn the HIGH signal is received by the on-board LED and it starts to blink as the HIGH signal passes through it.
The second function used in the loop() function is:
delay(1000);
The delay function halts the program flow for the specified number of milliseconds. In this case the program execution halts for 1 second (or 1000 milliseconds). After waiting for 1 second the following line of code gets executed:
digitalWrite(13, LOW);
The above line of code results in a digital LOW signal being written on digital pin number 13, which in turn results in the onboard LED to stop glowing. Once again the program executes the following line of code:
delay(1000);
Thus the program execution waits for 1 second (or 1000 milliseconds). After waiting for 1 second the loop function is invoked all over again. Thus the sequence of code in the loop() function keeps switching the on-board LED ON and OFF continuously.
We shall see such an example in the hands on sketch that involves blinking an external LED in the Chapter 3, Day 1 - Building a Simple Prototype. Similarly, one should not connect a heavy duty component like a DC motor directly to an Arduino pin, as in this case the Arduino pin may get damaged. We will study this aspect in Chapter 7, Day 5 - Using Actuators.
Now that we have understood the onboard LED blinking program in detail, let us follow the steps below for loading the above program into the Arduino Uno board.