ACROBOTIC Industries ACROBOTIC Industries

Arduino Activity 1: Blink

Overview

Time to complete: 2–10min; Level of difficulty: Beginner

In this activity we will learn how to program an Arduino UNO board in order to turn on and off the built-in LED using example code that’s included with the Arduino software.

List of Materials

Item NameQty.
Arduino Uno R3 1

The Built-in LED

After a quick glance over the Arduino Uno board, we can see that it contains quite a few different electronic components (chips, diodes, resistors, capacitors, etc.). Amongst them, we find a very useful LED that allows us to test the operation of the Arduino Uno immediately!

We can identify the often called the ‘L’ LED by its label on the silkscreen (the white letters that) of the board.  It is positioned near pin 13 on the top-side of the board as shown on the image above.  By loading a program to the microcontroller on the Arduino board, we can turn on and off the ‘L’ LED without having to connect any additional components.


Loading the Example Code

Once we've setup our computer correctly (check out our Getting Started tutorial for a walkthrough), we can connect the Arduino Uno board to the USB port of the computer using the USB-A to USB-B cable.  After opening the Arduino environment (a.k.a Arduino IDE) in our computer we can use the menu bar to select File → Examples → 01.Basics → Blink and open up our first program:

Selecting the Blink Program

After selecting Blink, a new Window pops open containing the code for the program we’ve just selected.  The Window is a basic Text Editor where we can write pieces of code to later upload them to the Arduino board.


Configuring the Arduino Environment

Once we open the Blink program, we're almost ready to load it onto the Arduino Uno board.  We need to first check that the Arduino IDE is ready to communicate with the board. Using the menu bar, we specify the board we're going to be using.  In this case we select Tools → Board → Arduino Uno:

Then, we choose the serial port to which the Arduino Uno is connected by selecting Tools → Serial Port → yourserialport (can't see the serial port?):


Uploading the Code to the Arduino Board

The Arduino environment makes it pretty easy to load the program to the Arduino board.  Simply click the Upload button and that's it.  

Keep an eye out on the messages at the bottom of the Window to know when the program has finished uploading.  After it's done uploading a message near the bottom of the window pops-up letting yout know.  The exact numbers shown in the image below may vary depending on your own setup!

Done Uploading Message

After the message pops-up, it's blinking time!  The  ‘L’ LED should turn on for 1 second and then turn off for 1 second:


Code Walkthrough

As we see in this example, well-written programs include instructions and brief explanations alongside the code itself. We can identify the comments sections easily as they must be enclosed between /* */ or preceded by //.

/* 
Blink Turns on an LED on for one second, 
then off for one second, repeatedly. This 
example code is in the public domain. 
*/
// Pin 13 has an LED connected on most Arduino boards. 
// Give it a name:
int led = 13; 

The first line of code, as described in the program, assigns the value 13 to the variable led. This allows us to use the name of the variable throughout the code, and if we want to change its value, we need to only do it once.

// the setup routine runs once when you press reset: 
void setup()
{
  // initialize the digital pin as an output.  
  pinMode(led, OUTPUT);
}

Every Arduino sketch must have both a setup and a loop function. The former runs once when the board resets or is first powered-on, and the latter runs continuously afterward until the next reset (while power is available, of course). In this case, we set up the mode of operation of pin #13.

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

Because pin #13 was set up to be an output, we can set it to be at a high (5VDC) or low (0VDC) voltage level. We do this by writing the values 1 or 0 to it, but instead of using the numbers directly, we can make use of the pre-defined constants HIGH and LOW. In addition, a 1000ms delay is included before each digital value is written. The end result is a ‘blinking’ LED that turns on for 1 second, and off for 1 second.


Making a Copy of the Example Code

So that we can always go back to the ‘original’ code if needed, the Arduino IDE examples cannot be overwritten by default. And so, in order to make changes to the code and keep our modified version, we need to save our own copy.

As in most computer software, we can save our own copy using the menu bar to select File → Save As…, and then choosing a directory location. In our case we chose the name BlinkMod for our copy.


Code Tinkering

As we've made our own copy of the code, we're now free to modify it. We can always go back to the original if (when) we break it beyond repair.

// the loop routine runs over and over again forever:
void loop()
{
  digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(250); // wait for 250ms
  digitalWrite(led, LOW);// turn the LED off by making the voltage LOW
  delay(1500); // wait for a second and a half
}

For this example, we can play around with the delay values. We can for instance decrease the first delay, and increase the second one turning the blinking action of the LED into a something more of a light beacon.


Troubleshooting

We've written a very comprehensive guide on how to set up your computer (Mac OS X, Windows, and Linux) for working with an Arduino Board.  Check it out for additional troubleshooting tips.

Linux
  • Serial Ports option is grayed out

On a default Linux installation, if we download the Arduino environment rather than installing it from the package manager, we might not be able to access the serial port from the program because of a permissions issue.

The simplest solution to this problem is to run the Arduino environment from a Terminal window by issuing the commands:

cd path-of-your-arduino-install && sudo ./arduino

By running the Arduino environment as superuser (by way of preceding the command with sudo) we can have access to the serial ports. However, a better way of doing this is by adding the current user to a group that has write-access to them.  To determine the correct group we can issue the command:

ls -l /dev/tty* | grep 'USB\|ACM'

which returns some details about the serial ports currently in use (ensure that the Arduino board is connected beforehand). Inspecting the 4th column tells us that the group name in question (Ubuntu 14.10) is dialout.

To add the current user to the group we issue the command:

sudo usermod -a -G dialout username && newgrp dialout

And we can confirm that we've succeeded by isuing:

groups username

which should return all the groups to which username belongs.

If everything goes correctly we should be able to see the serial port to which our Arduino Uno is connected listed on the Serial Ports option.


Comments, questions, or concerns? Drop us a line!.