Home Project Ideas Project Guide Ask An Expert Blog Careers Teachers Parents Students

Getting Started with Arduino

It is fun to build and create things, and even more fun to give your creations a "brain." Then they can act without your direct control all the time. The Arduino can help you put "brain power" in your project, even if this is your first time building or programming a contraption. The Arduino (see Figure 1 below) is a type of microprocessor popular with hobbyists. A microprocessor is a computer, often as small as a fingernail, that runs without the parts we often think of when we say "computer" —namely, a keyboard, screen, and hard drive. A microprocessor uses electronic memory instead of a hard drive and doesn't need a keyboard or screen. In fact, your computer keyboard has a microprocessor to tell your computer what keys you pressed. Cool, right?

Arduino Tutorial image- the Arduino UNO is a popular model in the Arduino line
Figure 1. The Arduino UNO is a popular model in the Arduino line. The UNO is often chosen over other models because it offers a good number of inputs and outputs, is inexpensive, and the board shape attaches easily to add-ons. In the Arduino world these add-on boards are called shields. Shields are available to do all sorts of interesting things, like WiFi to communicate wirelessly to your computer or adding electronic drum pads. Some Arduino models are bigger, with more inputs and outputs, and some are smaller. For many projects, the UNO is just right.

One of the nice things about the Arduino is that it is an open source project. That means it is not owned by any company, although any company can make things for it, write programs for it, and contribute to making it more powerful and user friendly. (If video game consoles worked that way, you could get games just about anywhere and run them on just about any console.) For the Arduino, being open source means that the programs' source code is readily available on the Web for anyone to download and use, or modify, for free. This means that if you want your Arduino to do something (like control a motor for a secret sign) but do not know how to program it to do this, you can look at other programs and learn from them, or download one and change it to suit your needs. This makes getting started with the Arduino even faster and easier.

Arduino projects are typically made with breadboard techniques, where parts are pushed into holes in sockets rather than soldered in place. If you are unfamiliar with a breadboard, see Figure 2 of the Science Buddies' Electronics Primer for a brief explanation. This simplified wiring technique along with the ease of programming are what make the Arduino popular. It is not as simple as snapping together and programming robot kits like LEGO® MINDSTORMS®, but it is far easier than having to solder boards and then program the microcontroller with dedicated programming hardware. When someone writes source code to make the Arduino do something new, they can put it in a collection of code called a support library which the Arduino software will then be able to use just as easily as any other code function. Some of the most popular support libraries allow the Arduino to control servo motors, blink LEDs, and read pushbuttons. The Arduino support libraries also provide source code for most standard hobbyist robot-project parts (easily found online) —eliminating the need to write that code yourself.

In the other tabs of this tutorial we will guide you through how to start your first Arduino project. Everything from buying parts, installing an Arduino, and writing your first program is covered. In addition, the Troubleshooting tab has tips on what to do if you get stuck while working on an Arduino project. Enjoy!

Mail order and Internet sources can be excellent places to buy Arduinos and the necessary accessories. Table 1 below shows some common items you may want along with Amazon.com links to them.

Arduinos & Related Accessories
Arduino UNO http://www.amazon.com/Arduino-Rev-3-Uno-R3/dp/B006H06TVG The Arduino UNO is a popular choice with hobbyists because it is compatible with more Arduino projects and accessories than other models.
Solderless breadboard http://www.amazon.com/BB830-Solderless-Plug--BreadBoard-tie-points/dp/B0040Z4QN8 A "breadboard" is a tool for testing out ideas and makes it easier to try out different circuits. If you don't have a breadboard, you have to make a circuit board and solder parts together. With a breadboard, you simply stick electronic parts and wires in holes to make connections.
Breadboard wires http://www.amazon.com/Breadboard-jumper-wire-70pcs-pack/dp/B0040DEI9M To make connections between points on a breadboard, you need wires with rigid pins at the end. These wires are one style. Another option is to buy precut and bent wires as a set.
Another form of breadboard wires http://www.amazon.com/Elenco-Piece-Pre-formed-Jumper-Wire/dp/B0002H7AIG As an alternative to flexible breadboard wires, these wires are solid pieces of wire — precut, stripped, and bent. Some people prefer working with these instead of the flexible ones.
USB A-B cable http://www.amazon.com/Amico-Scanner-Printer-Parts-Cable/dp/B007Q9XYPG The USB cable connects the Arduino to your laptop or desktop computer. The Arduino uses the square-type connector (USB B); your computer USB ports of course use the familiar rectangular ones (USB A). This type of cable is used for printers or scanners as well, and it is known as an A-B cable because it goes between an A and a B connector. Any good-quality USB A-B cable will do.
Wall power supply http://www.amazon.com/9V-DC-Power-Adapter-Supply/dp/B004LX146Q When the Arduino is plugged into the computer, it gets power through the USB cable. After it is programmed, you may want to run it without attaching it to a computer. It is a computer by itself, after all. This is a 120-volt (V) wall adapter that provides the right connection and voltage for an Arduino.
9V battery connector http://www.amazon.com/Gino-5-5mm-Male-Plug-Battery/dp/B005D65LEG Instead of being connected to a wall outlet or your computer, you can power the Arduino from a standard 9V battery if you have a power cable with the right clip.
One possible Arduino starter kit http://www.amazon.com/Arduino-Uno-Starter---Solderless-Breadboard/dp/B0051QHPJM This kit contains an Arduino UNO, breadboard, set of breadboard wires, a USB cable, and a plastic holder to keep the Arduino and breadboard together.
Free software http://arduino.cc/en/Main/Software To convert (compile) your source code to a binary computer program for the Arduino, and to transfer that program to the Arduino, you need special software for your laptop or desktop computer. Fortunately, that program is free and is available for newer computers running Microsoft Windows, Mac OSX, or some versions of Linux.
Table 1. Online sources for the Arduino and related accessories

The Arduino was designed to be used with the programming software provided free by the same people who designed the boards. The steps below talk about how to install the Arduino and use this free software to test out and write your first Arduino program. Besides this traditional software, you can program your Arduino in other ways; see the Alternative Programming tab for more details.

Arduino First Steps

  1. Unpack and look over your Arduino board. Locate the USB connector and the battery connector; if you are using a UNO, the parts are labeled below in Figure 2 below. When the Arduino is connected to your computer via USB, it gets power from your computer. After it is programmed, you can disconnect the Arduino from the computer and run your project from a battery supply (7.5 to 9 V) or a wall "wart" power cube.
Arduino Tutorial image- Arduino UNO with input and output connectors labeled
Figure 2. The Arduino UNO has a number of connectors for attaching inputs and outputs from your project as well as supplying the board with power. Note the LED; The Arduino documentation refers to it as the "User" LED and it is useful for testing your board with the BLINK program and debugging your own code later on.
  1. Go to the Download the Arduino Software web page and download the latest version of the Arduino development software for your computer. Software like this contains everything you need to write, compile, and test your own programs and is often referred to as an integrated development environment or IDE. The Arduino IDE needs one USB port free on your computer to transfer your program to the Arduino board.
    1. The Arduino uses your USB port for power, so it is important that you use a USB port that is not just part of a USB hub, unless that hub is plugged into the wall with its own power supply. Ideally, you should plug the Arduino into USB ports that are built into the computer.
    2. You also need to be running a supported version of Microsoft Windows, Mac OSX, or Linux (the website will explain the current requirements) and have enough disk space to do the installation.
    3. Finally, make sure you have permission from your parents or teacher or whoever owns the computer to install software and plug-in devices on the unit.
  2. Once the software is installed, test it out. Start the Arduino application and connect the USB cord from the Arduino to your computer.
  3. In the Arduino software menu, open the Arduino sketch (in Arduino world, a sketch is a collection of programs for one project) called "Blink." It is usually in the "Examples" folder. Look around a bit and you will find it. Once it loads, it will display the code on your screen. The Arduino software refers to available communication ports as serial ports which are standard parts of a computer used to communicate with other computers. Modern computers don't have actual serial ports but USB connections are often used to simulate them. For your purposes, it's enough to know that installing the Arduino added one of these "fake" serial ports and that you've selected it in the menu, as shown in Figure 3 below
Arduino Tutorial image-  Blink sketch for the Arduino
Figure 3. The sample sketch "Blink" loaded into the Arduino IDE under Windows XP. In addition, we can see that the Arduino board USB connection is available and selected as COM9. This sample sketch will look similar in other operating systems.
  1. The Blink sketch takes advantage of a built-in LED on the UNO board (see Figure 2 above). Without having to wire any extra parts, this code will make the LED blink slowly. This is a quick and easy test to make sure your Arduino is functioning well.
    1. The first few lines of the program are a comment. Comments are a part of computer language. The computer ignores them, but they give the programmer a chance to explain what they are trying to do so that other people can understand (see Figure 4 below). You may know exactly what you want the computer to do and you can program it to do that, but another person reading your program will want to know why and the comment is where you tell them. Also think about rereading your own program a year from now. The comment will help you remember what your program does. Use comments wherever you can!
      Arduino Tutorial image-  An example of a comment in an Arduino sketch
      Figure 4. A comment in the Arduino version of the C programming language (a multipurpose computer language developed by Dennis Ritchie) allows for large multi-line comments. Use these to describe the concepts and strategies of sections of your programs: What will it be doing? Why is it doing this? How does it get the job done? When you read your code weeks or years from now, you will be glad you took the time to document it in a comment.
    2. Comments appear in gray on the screen and use the C language conventions for comments. Anything that starts with "/*" and ends with "*/" is considered a comment and will be ignored by the Arduino compiler (a translator for turning human-readable source code into something the computer can use).
    3. A line that starts with "//" is a one line comment. Anything that follows the double slashes and goes to the end of the line is ignored. The comment shown in Figure 5 below explains that pin 13 on the Arduino is connected to the onboard LED. It says that the following code simply names the pin.
      Arduino Tutorial image-  An example of a comment in an Arduino sketch
      Figure 5. To make a short comment of just one line, begin the line with a double slash mark. The compiler, which simply ignores anything to the right of a double slash, will ignore the line when it compiles your program. You can also use double slashes at the end of a line (to the right of final punctuation) in your program to make the rest of that line a comment.
    4. The phrase "int led = 13;" (see Figure 6 below) means that the code is reserving space for an integer value (a counting number with no fractions). It is naming it "led" and assigning it the value 13.
      Arduino Tutorial image-  An example of a variable in an Arduino sketch
      Figure 6. To have the Arduino store a number somewhere for later use, you need to create a variable (a place for the computer to remember something it will need later on). The compiler needs to know what type of stuff you intend to store (a number or a piece of text). When you create a variable you can also preset it to a number if you like. This is optional, but you will see it used often when you read Arduino programs. In this case, you are creating an integer (a number with no decimal point that can range from -32768 up to 32767), naming it "led" and assigning it the value 13.
    5. The next comment (Figure 7 below) introduces a routine (a chunk of code that the program goes back to repeatedly each time, directing the computer to do the same set of steps) called setup().
      Arduino Tutorial image- An example of a comment in the Arduino blink sketch
      Figure 7. A single line comment introduces the following section as the setup() routine in the code.
    6. In Arduino programming (or any C programming, for that matter), a block of statements that should be treated as a single thing is surrounded by braces ("{"and "}"). In the same way the code previously created storage for "led," the "void setup() {" line and the lines following it until the "}" will create a piece of code called "setup." Normally you can call your routines anything you want as long as you have not already used the name in the program, but the Arduino reserves "setup" and "loop" as special names that you cannot use. "Setup" is a special name in an Arduino sketch because it is automatically executed once without your doing anything special. This is where you direct the computer to do things to set up pins or other initialization that you do once (see Figure 8, below). In this case, the only statement in the block is "pinMode(led, OUTPUT)," which tells the Arduino that pin 13 (remember "led = 13"?) will be used for output rather than input.
      Arduino Tutorial image-  An example of a setup routine in the Arduino blink sketch
      Figure 8. The setup() routine in this program is very simple. The only thing you need to tell the Arduino is that pin 13 will be used to output data to the LED on the board. The Arduino statement "pinMode" tells the microprocessor that a pin will be used for either input or output. In this case the designer of the UNO already put an LED on pin 13 for you, so you just have to tell the compiler that you are going to use it.
    7. The last group of statements is the code for "loop" (the second special routine name) which is also defined as a block of statements. The Arduino will repeat these statements over and over again until you disconnect the power or you load in a new sketch using your computer and USB cable. Once setup() has been executed by the Arduino, it immediately executes loop(). Once that is completed, it goes back and executes loop() again and again forever.

      The loop statements make up the meat of our sketch (if you think of this as a kind of code sandwich). In the order shown in Figure 9 below, the first "digitalWrite" changes the voltage on the LED pin from 0 volts (low) to 5 volts (high) to light the LED. The "delay" waits 1000 milliseconds, or one second, before proceeding. The second digitalWrite sets the led pin low (0 volts or ground) to turn the LED off. The second delay waits another second. That ends the loop code but not the blinking.

      Arduino Tutorial image-  An example of a loop routine in the Arduino blink sketch
      Figure 9. The main part of an Arduino program is the loop() routine. The Arduino will start at the top of this block of code, execute it to the bottom, then go back up and do it all again. Forever. ("Forever" to a microprocessor is until the power goes off or you reprogram it.) Blink is a simple program that just turns on the LED, waits 1000 milliseconds (one second), turns off the LED, waits 1000 milliseconds and then repeats this loop forever.
    8. The Arduino runs "loop" forever, remember? The code in the loop block simply blinks the LED once but when running, the Arduino will repeat it over and over until you power it off or reload it with another sketch.

The Arduino software comes with dozens of interesting sketches or programs that you can learn a lot from. But the idea here, after all, is to use the Arduino to give your creation a brain. Once you get familiar with the process of compiling and loading sketches into the Arduino, you will want to write your own code (stored on your computer) and use the Arduino software to program the Arduino with it.

Wiring Devices and Writing Your Code

If you can load sketches and execute them on the Arduino, you are ready to connect stuff to the board and write your own code. Remember that the Arduino is a small chip and cannot drive anything big like bright lamps or motors directly. Switches and LEDs are fine. Do some Web research and see how other people have connected whatever it is you want to connect as well.

If you are new to working with the Arduino, go slow at first. If your project requires attaching 100 LEDs with a whole bunch of circuitry and a dozen switches, start with a few and add circuitry slowly. Write a test program to test each level as you go. When you write your code, start with basic functionality and add bells and whistles later. It is always good programming practice to use comments to explain what your code does — a month or a year from now, will you remember how it works?

Have fun!

Above everything else, have fun! The Arduino is a rich resource and a fun little computer. You will learn a lot about programming as you work with it and you learn even more by reading forum questions on Arduino and other hobbyist websites. Ask questions. When you know the answer to one, answer it. Share your code with others. That is one of the wonderful things about open-source development - people freely share their work so that everyone can benefit. And it is just plain fun to see what people come up with!

Graphical programming is an alternative way of taking your ideas and turning them into computer programs. The standard method of writing your program in a computer language does require that you follow all of the detailed rules in the language. This is similar to when you write a paper for one of your classes; you have to be sure to use proper punctuation and spell everything correctly. Your teacher will look at these details and make that a part of your grade. Computer languages also have rules. You have to spell the special keywords correctly, put parentheses, braces, commas and other punctuation in the right place, and make sure to put all the right things in the right order. Unlike your school papers, where a sloppy job might get you a B instead of an A, the computer compiler will give you an F if you get even one detail wrong. The program simply will not work. As you program more, you will learn to get these details right the first (or maybe second) time.

In a graphical programming environment, you don't have to deal with all of the details. You write your program by picking blocks of program code from a menu and arranging them to build your program. If you are familiar with programming a LEGO MINDSTORMS® system, you have seen one form of a graphical programming environment. Engineers have begun making graphical environments for the Arduino as well, and here are two that you might want to learn more about. You may notice that these two software packages call themselves "Beta" versions. In the software world, that means they are not really finished, but they are good enough for you to work with if you forgive them when they make a mistake or two. You have to decide if the benefits outweigh the possible problems — and the best way of doing that is to just try them. The two graphical programming options are Minibloq and Modkit Micro.

Minibloq is an Arduino programming environment that you can get for free from http://blog.minibloq.org. The screen shows you the possible statements or functions you would want for your Arduino program. You simply drag them into the programming area. A split screen shows you what your program looks like in blocks and what it would look like in the conventional Arduino programming software. Figure 11 below shows a Minibloq version of the same Blink program you saw in Figure 3 in the Programming tab of this tutorial. The blocks use imagery to replace the words — a pencil and paper represent "write" and a clock represents time or "delay."

Arduino Tutorial image-  : Programming Arduinos with Minibloq
Figure 10. The example program Blink expressed in Minibloq version 0.81. Each function (such as digitalWrite or delay) is shown as a purely graphical icon. You simply arrange them in the order you want these functions executed.

Another graphical environment for the Arduino is Modkit Micro. The look and feel of Modkit Micro (available from http://www.modkit.com) comes directly from the popular programming environment Scratch, developed at MIT's Media Lab. Scratch was designed to make it easy to write animated stories and games. The developers of Modkit Micro (who also worked on Scratch) made their environment look a lot like Scratch, and it is just as easy to use. Figure 12 below shows the Blink program written as it would look in Modkit Micro. Each of the function blocks are labeled with words instead of icons, but it takes care of the pesky punctuation and formatting. The blocks are also shaped a bit like puzzle pieces, which helps you see which blocks fit together. Modkit Micro first became available in the summer of 2012 as more of a "sneak peek" Alpha (the test version before Beta) and continues to improve.

Arduino Tutorial image-  Programming Arduinos with Modkit Micro
Figure 11. Writing Blink using Modkit Micro. The function blocks are shaped like puzzle pieces and help you see how things flow and fit together. The blocks are labeled with word descriptions instead of icons.

Advantages and Disadvantages of Graphical Programming

No one solution to anything is ever perfect. The Arduino programming language is based on the C language (standard in the software industry), but it has been changed a bit here and there to make it easier for new programmers to learn. In the Arduino world, when a company designs a new sensor of some kind, they usually provide the Arduino programming code (as a support library) necessary to make that sensor work. You can also find many thousands of available Arduino programs on the Web that other people have written and are willing to share with you.

Graphical environments let you write your programs without having to mess with the nitpicking details of the Arduino language. If you choose to work with one, you will be using Beta software that could have a bug or two. You will also not find the vast libraries of free code on the Web, and if you find a new and totally fun sensor you want to put on your creation, you may have to write the code for it yourself.

Troubleshooting Arduino programs is one of the few drawbacks of the Arduino platform. It doesn't have a debugger program that helps you find programming errors like many other environments. A lot of people also report difficulty getting their computer to recognize the Arduino's USB port. Here are a few tips to get you past these issues.

  • The Arduino User Forum has a lot of hints and tips to help you figure out why something does not work. The Arduino users themselves are quite helpful, and if someone hasn't already reported your same problem, you can just ask and someone will likely come to the rescue. Search the user forum first, then try some other Web searches to see if the answer is already out there.
  • If the problem is your Arduino program, you can have the Arduino open a window on your computer and display messages. Open the sketch (in the Examples folder) "ASCIITable". The line "Serial.begin(9600);" sets up a connection between the Arduino and your computer through the USB. In your program, use statements like "Serial.println" to send messages to your computer. As the Arduino program runs, these messages will tell you what it is "thinking" and with a little thinking of your own, you can figure out why it may not be working.
  • If the problem is the USB port not communicating, it is most likely a driver issue. The device manager in Windows (see Figure 10 below) will identify the Arduino "serial port" (it is a fake port made with the USB connection) in the control panel. Utilities on the Mac and Linux operating systems will display the available ports. Under Linux you may have to be logged in with root or administrator access to use the ports. The Arduino user forums and documentation will be of some help here. But if you do not get the Arduino board talking with your computer, you cannot load any of your code.
Arduino Tutorial image-  Seeing Arduino boards with the device manager
Figure 12. The device manager in Windows XP shows the special Arduino port. In this case, COM9 is the name assigned to the USB. Select that in the Arduino IDE.

Resources

The Arduino's popularity means that many in-depth resources exist for learning how to program the Arduino and apply it to lots of different, fun creations. Here are a few of those resources: