Understanding the first Arduino sketch

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.

Note that when a digital pin is configured in output mode, the Arduino board is ready to transmit a digital voltage out via the digital pin. A digital signal is transmitted by sending an equivalent output voltage from a digital pin. This digital voltage in turn is received by the pin of a peripheral component that is attached to the digital pin that sends the digital signal.

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.

I/O pin setup:
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.

It is important to keep in mind that logical HIGH and LOW levels on a Pin are defined by the magnitude of the voltage on the pin. Typically, a HIGH state is identified by more than 3 volts and a LOW state is identified by less than 3 volts.

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.

It is worthwhile to note that Arduino Uno pins can supply a maximum of ~50 mA (some pins supply even less) of current (commonly referred to as "power" in online reference materials). Be careful when connecting Arduino pins to a component that cannot tolerate too much current and might burn out if too much current is provided--like an external LED.
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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.245.196