Arduino: Understanding Blink

This is part of a series of posts on getting started with Arduino. Others in the series include:

This post focuses understanding “Blink”, the first program that should be loaded to test your Arduino.

Getting Ready to Start

In order to use this tutorial, you should have loaded the Arduino IDE onto your computer, and you should have an Arduino and cable.

You should select the appropriate board under the menu “Tools” on the Arduino IDE. For most beginners, the board will be the “Arduino UNO” although others will work with this tutorial.

After plugging the cable into the Arduino and into the computer, you should also select the appropriate port, which is also found under the “Tools” menu, just below the board. Typically, you will only have one choice of port. It should look something like “COM4”.

Under the “File” menu, there is a submenu labeled “Examples”. And under the “Examples” submenu, there is a submenu labeled “01. Basics”. There you will find the “Blink” program.

Again, the path to the “Blink” program is:

File > Examples > 01. Basics > Blink

When you select the Blink program, you should see some code pop up in the main window.

You can upload this code to your Arduino by clicking the circle with the right arrow. You will see the LED flash a few times in an irregular manner as the program is being uploaded. Then you will see the LED begin blinking regularly once a second.

Here is the entire code for Blink:

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Comments

The first thing to realize is that the code above contains instructions that the computer can understand and information that people can understand but the computer ignores. This information is called “comments”.

There are two ways to indicate comments. The first is a block comment which starts with /* and ends with */.

All of this is a block comment. The computer completely ignores this.

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/

This second comment is an in-line comment, which is marked by //.

The comment below takes up the entire line.

// the loop function runs over and over again forever

However, this comment contains computer code followed by a comment. The computer pays attention to the first part and ignores everything on the line after //.

  delay(1000);                     // wait for a second

Comments are very helpful to understanding the program. You should get in the habit of adding comments to your programs.

The Program Behind the Program

To help you see what the computer is paying attention to, here is the code without comments.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                    
  digitalWrite(LED_BUILTIN, LOW);   
  delay(1000);          
}

This code looks very simple. It actually takes a lot more of a program to get the Arduino to work. Fortunately, the larger program is hidden, and you don’t need to worry about it much.

However, you should know that there is a hidden program doing a lot of work in the background. This program does several things for us when running “Blink”.

First, it defines variables called LED_BUILTIN, OUTPUT, HIGH and LOW. These variables just contain numbers. For instance, on the Arduino Uno, LED_BUILTIN is equal to 13. However, on other Arduino boards, LED_BUILTIN may be a different number. Using this variable enables the code to work on many different Arduino boards.

Second, the hidden program defines several functions, such as digitalWrite(), pinMode(), and delay(). There is computer code in the Arduino for these functions, but we can use these functions without seeing that code.

Finally, the hidden program uses two functions that we can modify, called “setup()” and “loop()”. The hidden program uses setup() once whenever the Arduino first gets turned on and then it uses loop() over and over again. We can change the code inside setup() and loop() to control the Arduino.

The Three Built-in Functions

The Blink program uses three built-in functions: pinMode(), digitalWrite(), and delay().

pinMode(LED_BUILTIN, OUTPUT);

The pinMode() function tells the Arduino whether a specific pin should be used for input or output.

digitalWrite(LED_BUILTIN, HIGH);  

The digitalWrite() function tells the Arduino whether the specific pin should be set High or Low. On the Arduino, HIGH is 5 volts and LOW is 0 volts.

delay(1000);

The delay() function tells the Arduino to wait for the specified amount of time, in milliseconds. So, this code tells the Arduino to wait for 1000 milliseconds, which is equal to 1 second.

Putting It All Together

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                    
  digitalWrite(LED_BUILTIN, LOW);   
  delay(1000);          
}

The hidden program defines the variables LED_BUILTIN, OUTPUT, HIGH and LOW. It also defines the functions pinMode(), digitalWrite() and delay(). It then calls the function setup().

setup() then calls the function pinMode() which sets the mode of pin 13 to OUTPUT.

The hidden program then calls the loop() function.

The loop() function sets the voltage on pin 13 to 5V, which turns the LED on. It then waits 1 second. It then sets the voltage on pin 13 to 0V which turns the LED off. It waits another second. Then the loop() function repeats, forever.

Comments

Leave a comment