What you'll need for this lesson
  • Arduino IDE 1.6.11 or later installed on your system
  • Chip Snips Metro Mini Bridge
  • Chip Snips LED module
  • Micro USB cable

Set Up

Step 1:  If you have not already, connect the Metro Mini, bridge board included, to your PC with the micro USB cable.

Chip Snips Bridge for Metro Mini connected to a USB port

 

Step 2:  Open Arduino IDE.  Make sure Arduino IDE knows the board is "Arduino/Genuino Uno" and the programmer is "Arduino as ISP."  This ensures the Arduino IDE can compile your human readable code into the correct microcontroller readable code and download it correctly.

Configuring Arduino IDE tools

 

Make sure "Port" is set to the COM port your Metro Mini is attached to.  Your system will choose a COM port number which may be any number (COM1, COM3, COM8, COM25, etc.).  See Lesson 1 if you need help identifying the port number.

Step 3:  Open the Blink program.  Go to "File" -> "Examples" -> "01.Basics" -> "Blink"

Finding the Blink program

 

You'll notice the Arduino IDE comes with numerous sample programs that can control your Arduino-compatible board's behaviors.  Most of these depend on having certain components attached to your microcontroller.  Conveniently, Both the Arduino Uno and and Metro Mini have an on-board light-emitting diode (LED) connected to pin 13 so they will execute Blink and an LED will blink.

Wait, what's a "pin?"

Microcontrollers allow your programs to sense and respond to the outside world through "pins" that are electrically conductive metal legs or bumps which transmit voltage and can provide enough electrical current to turn on an LED.  The picture below shows the microcontroller's visible pins on 3 of 4 sides enclosed by blue brackets.  These pins are numbered sequentially and may be configured to perform one or more functions depending on how your program chooses to use them.

Microcontroller pins

 

The Arduino Uno and Metro Mini both have pin 13 connected to an LED.  The Blink program will use this connection to turn the LED on and off repeatedly or "blink."

Blink Walk-through

All programming languages support author comments.  Programming languages are much less complex than the language this tutorial is written in.  Simplicity is good for the compiler that generates machine code and the microcontroller, but not so helpful when we are trying to read the code to understand what its purpose is.  Comments in code allow authors to insert text that the compiler will ignore and can tell human readers what the code is doing.  Blink, as you can see, is mostly comments which are highlighted by blue boxes below.

Take a moment to read the comments either here or in the Arduino IDE.

Blink program with comments identified

 

The compiler knows to ignore any text between a slash-star block (/*    */) or behind double slashes (//). Write anything you want in comments, the compiler won't waste time thinking about it.  The Arduino IDE also changes the text color to gray to further help us identify comments.

Functions

The Blink program defines two functions, setup() and loop().  You can think of a function as a block of code that does something.  It may take input data or return output data or not (void before setup() and loop() means they will not return data).  In our case, setup() and loop() take and receive no data, they just do work.

Setup() will execute the code within its curly braces "{}" before it does anything in the loop() function.  Setup() executes once and then it is done.

Loop() will execute after setup(), but unlike setup(), loop() will execute over, and over, again.  Loop() won't stop until you turn off the microcontroller or reset it and download a new program.

The setup() and loop() sequence

 

Microcontroller code often loops certain code sections infinitely (or until power is turned off) because we typically want them to continuously do something.

PinMode() is not defined in the Blink program, but is defined in a standard library that Arduino imports for us.  PinMode() takes two inputs or "arguments." (Don't worry, there will typically be no yelling or hurt feelings over arguments in code).

The first argument identifies which microcontroller pin we want to configure and the second tells the program what the configuration will be.  Pin 13 is configured as output in setup() so loop() can electrically charge it with a positive voltage or 0 voltage.  If there is a high positive voltage on pin 13, the LED will sink current and the light will turn on.  If there is low voltage on pin 13, the LED won't sink current and we'll see no light.

DigitalWrite() function will write voltage level to the pin given in the arguments.  "digitalWrite(13, HIGH);" will charge pin 13 with a high voltage, turning on the LED.  "digitalWrite(13, LOW);" will discharge pin 13 with a low voltage, turning off the LED.

Delay() function takes a number of milliseconds to delay.  Since 1000 miliseconds equals 1 second an argument of "1000" will delay for 1 second.  After the delay is complete, the microcontroller will exit the function so your program can continue.

Compile and Download the Blink Program

The Arduino IDE was designed to be very easy to use.  The toolbar contains two round buttons.  The first contains a check mark meant to suggest this button will "check" or verify your Arduino program to ensure you followed programming rules and the compiler can understand how to convert or compile your program to machine code.

Wave your pointer over the check mark button and you'll see the word "Verify" appear nearby to remind you this will verify your program.  Arduino IDE will actually do more than "verify." it will go through an entire compile to make sure there are no errors, but will not download your compiled program to the microcontroller on your board.

The Verify button

Go ahead and click on the check mark button.  The words "Compiling sketch..." will appear with a green status bar which will disappear after the compile task is complete.

Compiling

What is "sketch?"

"Sketch" is synonymous with "program" or more accurately "source code."  Arduino IDE was derived from another programming IDE called Processing.  Processing was designed for artists to make drawings with program code and thus called programs "sketches."  Arduino IDE kept calling programs "sketches" causing a bit of initial confusion for experienced students and professionals, but a sketch is nothing more than C++ code.

Compiler output

If you didn't accidentally change anything in Blink, it should compile successfully.  Let's take a look at the result reported in Arduino IDE.

Successful compile

 

You'll notice the "Done compiling." message and the black field at the bottom states the "Sketch uses 928 bytes (2%) of program storage space. Maximum is 32, 256 bytes."  This means that your Blink program was compiled into machine code and the result was only 928 bytes, a very small program.  However, microcontrollers often have a very small amount of storage space for programs and data.  Unlike your personal computer or laptop which may have many gigabytes (billions of bytes) in a hard drive, your relatively inexpensive microcontroller will often have only thousands of bytes.  In this case, our microcontroller has 32,256 bytes.

The good news is the Blink program only uses 928 bytes leaving lots of room for more programming awesomeness if we choose to add other functionality later.

The final line states our variables use up only 9 bytes out of 2,048 dynamic memory available.  Dynamic memory is similar to your personal computer's DRAM.  This memory is lost if you pull power to your microcontroller and starts off empty every time you power up.  (Variables can be global or local and we'll talk about the difference in a future lesson.)

Let's download our compiled sketch..er program.  Just next to the "Verify" button is the "Upload" button with a right-pointing arrow accompanied by the word "Upload."  The arrow may suggest our program is transitioning and going somewhere, hopefully to your microcontroller as a fully compiled program.  Clicking this button will do everything the check mark or verify button does and try to send the compiled code to your microcontroller where it will run.

Download or "Upload" your program

 

Upload or Download?

In my engineering day job, we typically say "download" to mean sending compiled machine code or firmware to a low-level microcontroller and "upload" for sending data to a high-level system, such as a server network.  Arduino terminology thereby elevates the microcontroller in the technological hierarchy. Two words with seemingly opposite meanings are used synonymously.  "Download your program" is the same as "Upload your sketch" in Arduino speak.

What success looks like

The LED on pin 13 is the red LED marked with an "L."  The LED will toggle between on and off until you turn off power or download a new program.

Blink

Changing the program

Your kit includes an LED module that simplifies adding new hardware features for your program to control.  Looking down at the white circuit board, you'll see three input sockets labeled A1, A2, and A2 on one side and 3~, 5~, 9~ on the other.

Metro Mini Bridge with LED module on pin 9

The 3, 5, and 9 represent pin numbers configurable as outputs. The "~" next to them means these pins support Pulse Width Modulation (PWM) which we will talk about in a later lesson. Since they can be configured as outputs, changing all the 13s to 9s in the Blink program will blink the LED module on pin 9.  Be sure you change all of them, or the led won't blink right.

Going Further

  • Change the Blink program to toggle the LED module on pin 3, 5, or 9
  • Double the blink rate
  • Alternate blinking between LEDs on pin 13 and pin 9