Are you ready to jump into the world of creating electronic solutions? Have you heard the terms “Maker” or “Internet of Things” and wondered what it would take to get started? The reality is that anyone with a little bit of programming experience can get started in these areas quickly, easily, and most importantly cheaply. The best place to start is to buy an Arduino board and do a simple project. You can find Arduino boards at your local technology store (such as Frys) or online at sites like Amazon. You can learn how to get started and do a simple project by reading the rest of this article!
To get the most out of this article, you will need the following:
- Arduino Uno
- USB Cable (A to B)
- Arduino IDE
As a general note, you can buy an Arduino board by itself, or many companies sell them as a part of a kit that includes a circuit board and numerous sensors. The kits usually end up being the better way to go if you’ve never bought a board before as they will give you the LEDs and other sensors you’ll want to use!
Overview of Arduino Uno
The Arduino Uno is currently the most popular Arduino prototyping board. It is based on the ATmega328 microcontroller. It has 14 digital input/outputs (6 of those pins can be used for PWM, or pulse wave modulation), 6 analog inputs, ICSP header, USB connection and a power jack. Arduino boards are modular, so you can stack “shields” on top of the Arduino using the headers. Common shields include Ethernet Shield, Motor Shields, and many other different types of shields.
The Arduino Uno has a built in USB to serial converter (either the ATmega16U2 or ATmega8U2 depending on version) so there is no need for an FTDI driver chip to program the Uno. The Uno requires 5V of power, which can be supplied from either the USB connection or from the power port using a 5V AC to DC power supply or battery pack. Power is available on the headers; both 5V and 3.3V are available including grounding.
The Arduino Uno board is very easy to navigate; the designers did a great job at labeling all the connections clearly.
Installing the Arduino IDE
You can use the Arduino IDE to build apps for your Arduino board. The Arduino IDE is available from the Arduino.cc website. It is available on Windows, OSx, and Linux. You should install the most current, stable edition (1.0.5 as of writing). You can download the nightly builds, however they are not considered stable. I chose to download the Windows Installer version since it’s the easiest and we can get started right away. Installation is straight forward; there shouldn’t be any need to change any options in the installer. At the end of the installer it will ask if you want to install the drivers, click yes.
Once the install is finished, open up the Arduino IDE and you should see something that looks like this:
Arduino IDE
The Arduino IDE is very minimalistic and easy to learn. Plug in your Arduino Uno using the USB cable. The Arduino “ON” LED should light up telling you that it has power. Go to the Tools menu then Board menu and make sure that Arduino Uno is selected.
Select Arduino Uno
Next, make sure the correct serial port is selected for the Arduino in the Tools > Serial Port menu. The Ardunio Uno uses serial communication over USB thanks to the built in USB to Serial converter.
Serial Port Menu
The Arduino IDE is now setup and we are ready to start programming!
Basic Code Structure for Arduino Programming
The Arduino programming language is similar to C/C++. The code base can be categorized in three main groups: structure, variables, and functions. You can read more about these on the Arduino Reference homepage at http://arduino.cc/en/Reference/HomePage.
The Arduino Hello World; Blinking an LED
In the programming world, it is tradition to start off with the Hello World application. In the hardware programming world, the Hello World equivalent is the Blink LED program. Let me start by showing the entire Blink LED code, then I will break it down. You can easily access this code from the Arduino IDE Example repository by clicking File > 0.1Basics > Blink.
/* 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 setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // 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 }
Every Arduino project requires the setup function and the loop function. The setup function runs every time the Arduino is turned on. Everything inside the loop function will loop over and over until you turn it off.
The program starts off by declaring an integer “led” which is 13. 13 corresponds to digital pin 13 on the Arduino board, which has a build in LED next to it; we are just giving it a name here, “led”. On most Arduino boards, you’ll be able to see numbers next to each pin, so you should be able to identify pin 13.
Pin 13 on the Arduino board
In the setup function we use the function pinMode() to tell the Arduino that we are going to be using led (pin 13) as an output (as opposed to an input). That takes care of everything you need to do in the setup function.
In the loop function, you are doing three things in order to blink the LED. You are turning the LED on, turning the LED off, and waiting. The function digitalWrite() is used when you want to write to the digital pins. You can either write HIGH or you can write LOW to the pin. HIGH means you are giving the pin power and turning the LED on. LOW turns off the power and thus turns off the LED. The delay() function pauses the code for however long you set (in milliseconds), in our case, 1000 milliseconds, which is one second.
So this program is going to turn the LED on for 1 second, then off for 1 second in a loop forever.
Loading a Program onto Your Arduino
With your program created, now you need to upload this program to your Arduino board. Near the menu there are a few buttons, the button with the right arrow is the Upload button. Pressing it will compile your code, show you any errors that might exist, and upload the program to the board.
Arduino Board
Look at your Arduino. Is the light blinking? If everything worked, then it should be! Congrats on your first Arduino project! If it is not blinking, go back to the code and make sure you entered everything correctly.
As you saw, it only took a few lines of code to control the LED on your Arduino. For fun, you can change the timing from 1000 milliseconds to something else. For fun, you can replace the loop function with the following. What does this code do?
void loop() { for (int x = 1000; x > 100; x -= 100) { digitalWrite(led, HIGH); delay(x); digitalWrite(led, LOW); delay(x); } }
In Conclusion
As you saw in this article, it is easy to get started with an Arduino. In this article you learned how to write a simple program to control the built in LED on pin 13. While this might have seemed overly simple, it really is the foundation for controlling other Arduino sensors as well.
Dylan Sweaza |