The GeekDad Arduino Guide: Getting Blinky With It

Geek Culture

In the last installment of The GeekDad Arduino Guide, I introduced you to the Arduino hardware and why it is so cool. Now, let’s start actually using an Arduino. How do you get started? Well, first you have to buy an Arduino!

Actually, the Arduino hardware is Open Source Hardware so you don’t have to buy an Arduino, you could buy the parts yourself and build it up on a breadboard. I’m going to assume, however, that you aren’t at that level yet you’ll want to go ahead and buy a board. The latest version of the Arduino is the Arduino Uno (Revision 3). For this tutorial, I recommend the following parts:

When you get your Arduino you’ll find that you have a power plug option and a USB option. I would just go ahead and connect your Arduino to your computer via the USB connector. This will allow for input power and a data connection on one cable. Without doing anything else, you should be able to connect your Arduino to a USB port on your computer and see something happen. A Light Emitting Diode (LED) should light up once the Arduino has power and after a second or so, an LED on the board should start slowly blinking. The image below points out these LEDs on the board.

Arduino LED OverviewArduino LED Overview

An overview of some of the LEDs on the Arduino Uno board.

The power LED is on because, well, the Arduino has power! Why is the other LED blinking? Because the Arduino is executing the pre-installed software called Blink. Congratulations, you’ve now gotten you’re Arduino powered and running the Blink program that is flashing the LED. Next thing you need to do is install the Arduino Integrated Development Environment (IDE). The software is open source, free, and is available from the Arduino website for Windows, OS X, and Linux along with instructions for installation.

Now that you’ve got the IDE up and running, lets take a look at this little piece of code that is already executing on your Arduino. In the IDE go to File menu and then select Examples and the 1. Basics submenu. In that submenu, you will find Blink. Go ahead and select it and the Arduino IDE will open the source code file. Let’s take a look at the file.

/*
 Blink
 Turns on an LED on for one second, then off for one second, repeatedly.
 This example code is in the public domain.
*/
void setup() {
 // initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(13, OUTPUT);
}
void loop() {
 digitalWrite(13, HIGH); // set the LED on
 delay(1000);            // wait for a second
 digitalWrite(13, LOW);  // set the LED off
 delay(1000);            // wait for a second
}

If we step through the code we can see what is executing on the Arduino board. First of all, there are code comments. Anywhere you see “//” tells you that you are at the start of a comment. Anything to the right of the “//” is purely for the programmer sharing their code and is not included in the final binary file. The same thing goes for the “/*” and “*/” pairing. The “/*” starts a comment block and then anything that is found in the source code is ignored until a “*/” is encountered. From that, we can see that the first 6 lines of this program are comments for you to read and not for the computer. These lines aren’t actually on the microcontroller. What are these lines telling us? Exactly what they say: the name of the program, what it does, and some licensing information.

As a side note here, remember what I said above, about comment text being ignored by the computer? That means that comments are completely optional and your program will compile and execute just fine with no comments. If you ask me, however, comments are not optional and are, truly, mandatory. They help you when you go back and are asking yourself, “why did I do that this way?” They also help others who are looking at your code and asking themselves, “why did they do that this way?” They are no different than documenting an experiment, a drawing, a schematic (we’ll get there), or any other technical data you are capturing and/or sharing. To those that say you can self-document code with good coding standards I say that three years of grading C++ projects in college and ten years of professional experience reviewing other’s code tells me it just doesn’t hold up in reality. Developing on the Arduino is all about open source and open hardware. Commenting your code well is just common courtesy.

The eighth line defines a function called setup. If you think back to basic algebra and remember all those math functions, labeled as f(x) or f(x,y), where ‘f‘ is an identifier of the mathematic function and the variable, or variables, in the parentheses are the values that the function uses to arrive at an answer. Well, we are looking at the same thing in this example code. We are defining a function called ‘setup‘ that takes no input variables. The void at the beginning of the line means that the function returns no value. The setup function must appear in every Arduino program. All of the code between the curly brackets ‘{‘ and ‘}‘ define the code that executes when the setup function is called. The first two lines of the Blink setup function are comments and the last line is executable code. The code calls another function called pinMode that takes two input values, pin and mode. The pin represents one of the input/output pins on the Arduino. In this case, digital pin 13. The mode variable is one of the two constants INPUT or OUTPUT. When INPUT is used, the pin is set to expect to read the incoming signal on the pin. If it is set to OUTPUT, the pin is set to output a signal on the pin. In this case, pin 13, which is already tied to an LED as described before, is set to OUTPUT. When the code is loaded to the Arduino, the setup function is executed once when the microcontroller is booted.

After the setup function, we find another function, loop. This function is executed continuously by the microcontroller. As soon as all of the code in the function is finished executing, the function starts all over again. The first line calls a function called digitalWrite that takes two input variables, a pin and a value, similar to the pinMode function discussed above and just as above, pin represents an input/output pin on the Arduino. The variable value is generally set to one of two constants, HIGH or LOW. The constant HIGH represents logic level high, or +5 volts for normal transistor-to-transistor logic (TTL) and LOW represents the ground state, or zero volts. In this case we are setting digital pin 13 to OUTPUT +5 volts.

The next line of code calls the function delay. This function pauses execution for the number of milliseconds specified. In this case 1000 microseconds, or 1 second. The next line is another call to digitalWrite, this time setting pin 13 to LOW. We then delay again for 1 second and we hit the end of the loop function. So what next? We go back to the top of the loop function, set digital pin 13 to HIGH, and do it all over again. So what is this physically doing? This is the code that is making the LED on the board blink. Let’s look at the basic idea of the circuit in use.

A simple schematic of the pin 13 LED.A simple schematic of the pin 13 LED.

This is a basic schematic of the LED connected to pin 13. Everything inside the dotted line represents a simulation of the Arduino. Just trust that the 500 mHz clock is the same as the blink code executing, changing the value of pin 13 every second. Following the circuit around from the pin 13 output we find a resistor, followed by an LED, and then a connection to the Arduino ground. When pin 13 is set to HIGH, +5 volts is supplied through the pin and wants to flow to the ground. Now, if we just connected the pin directly to ground we would see all of the current that pin 13 can supply flow directly to ground and we would have something called short-circuit. This is like dry-firing a bow in archery. It is something you never want to do. We need to put something in the circuit to use some of the current. Since we want to blink an LED, why not put an LED in the circuit? Good idea! The only problem is, the LED works best with something less than a +5 V differential and would still allow far too much current to flow through the circuit, potentially damaging the LED and the board. So, we put the resistor (represented by the squiggly line) in the circuit too. We can put this resistor either before or after the LED because it will have the same limiting affect at either location.

So what happens when this circuit runs? Well, you can clearly see the LED blinking away happily on your Arduino board. If you take a look at the circuit above on CircuitLab.com, you can actually simulate the circuit and look at the voltage and current running through the circuit, like an oscilloscope. The output from a 10 second run using the Time Domain Simulation is shown below.

Arduino Blink Physical OutputArduino Blink Physical Output

The orange line represents the voltage with the scale on the right side of the plot. The current is plotted in light blue with a scale on the left. The current spikes are ok, those are bursts that can occur on switching and are very short. As you can see, every second, the digital pin is getting set to HIGH and the voltage is going to +5 volts. When that happens, the current also rises to about 14 milliamps. A perfectly safe current for the LED. After 1 second, the voltage and current both go to zero and the LED is now off. There you have it! Your Arduino is blinking away and you already understand why from the perspective of the source code and the physical circuit. You’re now on your way to more physical computing! Next time, we will talk more about these neat little Light Emitting Diodes, voltage differential, and sourcing versus sinking current. You’re also now on the road to earning three Adafruit Skill Badges, the LED badge, the microcontroller badge, and the learn to program badge. Give yourself a pat on the back!

Liked it? Take a second to support GeekDad and GeekMom on Patreon!
Become a patron at Patreon!