Search
Getting Started with Arduino
This is the first project in the Arduino Starter Project series. The Arduino Project series will be a set of projects of growing complexity that are all able to be completely constructed using parts from the Arduino Starter Kit. The series is aimed at the beginner Arduino user, though everyone is invited to read and contribute via comments and/or additional project ideas and instructions. In the first project we will look at LEDs and how to load sketches. We won't go much further than building a basic circuit and learning how to use the Arduino board to light up the LEDs; but some of the basic tools needed to understand how the Arduino is programmed will be covered completely. In order to complete this project, you will need to have the Arduino software installed on your computer. For instructions on how to download and install the Arduino software, check out the instructions posted on their website. There are also 3 folders you will need to install. Download and unzip these two folders to the "hardware\libraries\' directory of your Arduino installation (For example, in Windows this would be C:\Program Files\arduino-0017\hardware\libraries). Finally, download and unzip the project sketch to your sketchbook folder. By default the sketchbook is placed in 'My Documents' on Windows systems. Parts: (all Parts are included in the Arduino Starter Kit, or can be obtained individually) Wire It Up! 1. Using the black jumper wire, connect one end to the hole labeled 'GND' on the Arduino. Connect the other end of the jumper wire to the first row of the bread board. 2. Put the short leg of the red LED into the first row of the breadboard (Tip: put it in the hole right next to the black jumper wire). Put the long leg of the red LED into the second row of the breadboard. LED (Light Emitting Diode) Description: An LED has two parts, the short leg and the long leg. They have scientific names too: the anode (the long leg) and the cathode (the short leg). You see LEDs all over the place, usually in electronic items like cell phones, MP3 players and Video Game consoles. But the list goes on an on. If you didn't already know, an LED can light up! How does it work? The short end always gets plugged into ground, or GND. The long end gets plugged into one of the spots on the Arduino Board. In the sketch (we'll talk about sketches in a little bit) we tell the LED if we want it to turn on or off. 3. Put the short leg of the yellow LED into the first row of the breadboard. Put the long leg of the yellow LED into the third row of the breadboard. 4. Using the green jumper wire, connect one end of the jumper wire to the hole labeled Digital 2 and the other end of the jumper wire to the second row of the breadboard (Tip: put it in the hole right next to the long leg of the red LED). 5. Using the blue jumper wire, connect one end of the jumper wire to the hole labeled Digital 3 and the other end of the jumper wire to the third row of the breadboard (Tip: put it in the hole right next to the long leg of the yellow LED). 6. Plug the USB cable into the computer and the Arduino board. Sketch It! 1. Open up Arduino by double clicking the Arduino icon ![]() 2. Load the sketch. Click on File->Sketchbook->Project_1 (If you haven't downloaded the project code, check out the top section titled 'Getting Started'). A Sketch Description: A sketch is a list of 'instructions' for the Arduino board. Sketches are written using the Arduino software. After a sketch is written it can be uploaded to an Arduino board. After a sketch is uploaded to an Arduino board, the Arduino board will begin to execute the instructions that are written in the sketch. 3. Compile the sketch. To compile the sketch, press the 'Play' button. ![]() Compile Description: When the sketch is compiled the Arduino software performs two important steps. First, it checks the sketch for any problems. Problems with the sketch might be misspelled words, missing signs or symbols, or instructions that the software doesn't understand. If Arduino finds any problems in the sketch it will stop and report the errors it found. The other thing that the Arduino software does when it compiles the sketch is change the sketch to 'machine code. The Arduino board doesn't understand the English words in the sketch, so the Arduino software converts the English to a language that the Arduino board can understand. 4. Upload the sketch. Press the 'Upload' button to upload the sketch to the Arduino Board. ![]() Once the sketch has finished uploading onto the Arduino board you will see text appear in the bottom window of the Arduino software. The text will say “Done uploading.” (If it doesn't say this, then there was a problem. Don't worry about it! Go check out the 'Troubleshooting' section at the end of this lab). Now that the sketch has been downloaded onto the Arduino board, the Arduino board begins performing the instructions that were written in the sketch. Let's take a look at the sketch to see why the LEDs are blinking at their current rate. Back in the Arduino Software, find this section of the sketch:
Whoah! What is all that stuff!? Notice how there are 3 different colors of text. Each color indicates something to the Arduino software. The sentences that are brown are called “comments.” Commenting Code Description: A comment is part of a sketch that is ignored by the Arduino Board when the sketch is running. Comments are used to write notes to yourself about the sketch. Everything to the right of the comment, on the same line as the '//' is ignored by Arduino, everything to the left of the '//' is still part of the sketch. How do I use it: A comment can be put into a sketch placing two forward slashes (//) before the notes that are to be commented. Show me an example: runExperiment(); //runExperiment is called to cause the LED to start blinking You'll see comments in every sketch you use, and when you create your own sketches you should use comments as often as you can. Sometimes code can get complex or hard to understand. Comments are used to remind us what the sketch is doing. Besides the comment in this section we see three red letters “int” in the code. This part of the sketch tells Arduino to keep track of a variable that is an integer named Red_On_Time. A Variable Description: A variable is a placeholder. It simply tells the Arduino Board that it needs to keep track of something. A variable always has a type, a name and a value. There are several different types of variables, the type tells the Arduino Board what it needs to keep track of. 'int' is short for integer and allows Arduino to keep track of a regular number. The name of the variable can be anything ('volume_level', 'color', 'blah', 'temp', 'x'). Choose a name for each variable that helps you remember what the variable is keeping track of. We choose the value of a variable. We can also change the value of the variable at any time in the sketch, which is why it's called variable. Whenever we use the name of the variable in the sketch, the Arduino board can look under that variable name and tell us the value stored in that variable. Show me an example: int Red_On_Time = 1000; In this example the variable is named 'Red_On_Time' and the type of variable is 'int,' or an integer. The value of the variable is 1000. An Integer Description: An integer is a type of variable which represents a number. It can be a positive number or a negative number. The highest number that can be given to an integer is 32,767 and the lowest number is -32,768. An integer can only be a whole number (no fractions or decimals such as 1/4 or 34.6). How do I use it: The letters 'int' tell Arduino that you want to create an Integer type variable. The name of the variable follows the 'int' letters. When you create an integer you can also assign the variable a value by using '=XX' after entering the name. Show me an example: int Red_On_Time=1000; Alright, enough with the learning for a little while. Let's get back to the sketch. So far we've created a variable named 'Red_On_Time' in the sketch, and we've assigned the number 1000 to this variable. So what does this mean? Well the comment tells us that it will be the number of milliseconds the red LED stays on. Look at the Arduino board again now that it's running the sketch. About how long do you think the Red LED is staying on? If you guessed that it's about 1 second, you're right! Now what's next in the sketch?
Another variable, this one is named Yellow_On_Time. Check out the number that we gave it though! It's not even a number, it's an equation! Well, actually it is a number. Remember, Red_On_Time is a variable that has a value. We said that Red_On_Time = 1000. So really the sketch is saying that Yellow_On_Time = 1000/2; or if you do the math really quickly, it means that Yellow_On_Time = 500. So the sketch is saying that Yellow_On_Time should be half of Red_On_Time. Check the Arduino board running the sketch again, is this true? Of course it is! Now what does the rest of the sketch do?
By reading the comments we see that theses variables tell the Arduino what the cycle time for each LED is. The cycle time is the total time it takes for an LED to blink on and off. The cycle times for both LEDs are the same! They are both set to 2000. Mod the Sketch! Now that we understand how this sketch works, lets change it up a bit. Right now we can see that the yellow LED is only on for half of the time that the red LED is on, so how come it's not blinking twice as fast as the red LED? The Yellow_Cycle_Time and the Red_Cycle_Time are the same. Let's make the yellow LED blink two times for every one time the red LED blinks. To make the yellow LED blink faster we have to make the Yellow_Cycle_Time shorter. If the cycle time is shorter, the led will blink on and off faster. Find this part of the sketch:
Change your sketch so that it looks like the highlighted part of the sketch below:
Don't forget to leave the semicolon after the instruction! We'll talk about semicolons in a bit. What have we done here? We've changed Yellow_Cycle_Time to be Red_Cycle_Time/2; or in other words we're telling the Arduino board that the Yellow_Cycle_Time should be half of Red_Cycle_Time. Now we need to download the sketch to the Arduino Board again so we can see the changes. 3. Compile the sketch. To compile the sketch, press the 'Play' button. ![]() 4. Upload the sketch. Press the 'Upload' button to upload the sketch to the Arduino Board. ![]() You should now see the yellow LED blinking twice as fast as the red LED. If you do, well done! You've made your very first blinking program! Next Step (aka homework): 1.) Make both LEDs blink at the same rate. (Hint: You will need to change two variables; Yellow_On_Time and Yellow_Cycle_Time) 2.) Make the Red LED blink 4 times for every yellow blink. What other patterns can you make by changing the values of the variables? Troubleshooting: When I go to 'File->Sketchbook' I don't see the 'Project 1' Sketch! If you don't see the Project 1 Sketch in the sketchbook then you probably haven't downloaded it yet! Go back to the top of the tutorial and check out the 'Getting Started' section. You need to download the "Project_1.zip" folder. Once you've downloaded it, unzip the folder in your sketchbook (which is usually located in 'My Documents'). When I try to compile, Arduino says "Error Compiling" and then lists a bunch of errors that say "'LED was not declared in this scope" or "Run_Project_1.h: No such file or directory." If you get this error, then you probably haven't downloaded the library files for this project. Go back to the top of the tutorial and check out the 'Getting Started' section. You need to download two different folders: LED Library and Run Project Library. Once you've downloaded them, unzip them in the 'hardware/libraries' folder of your Arduino directory (usually this is C:\Program Files\arduino-0017\hardware\libraries on windows machines). When I try to upload the sketch to the Arduino board, Arduino takes forever then says something like "Problem uploading to board..." This error can mean a couple different things. The most likely cause is that you haven't picked the correct COM port for your Arduino board. To fix it, first double check that your USB cable is plugged into the computer and into the Arduino board. If it's plugged in, make sure the green PWR LED is lit up on the Arduino board. Once you've double checked this, go to the Tools menu at the top of the Arduino window and then go to Serial Port. Make sure the correct COM port for the Arduino board is selected. (Hint: If you're using windows, this is usually the highest COM port number that's listed.). If you have the right COM port selected and you are still seeing this error then you may have the wrong Board selected. To fix this, go to the Tools menu in the Arduino window and then go to Board. Select 'Arduino Duemilanove or Nano w/ ATmega328' then try again. I've gotten the sketch to successfully upload to the Arduino board, but one or both of the LEDs are not coming on! What do I do? Ah, welcome to the world of hardware. If the sketch has successfully uploaded but the LEDs are not lighting up, it means that the wiring is incorrect. Go back to the 'Wiring It Up' section and carefully double check the pictures in the tutorial and compare them to the wiring you've created. Some things to pay special attention to are:
When I try to compile my code after modifying it, I get an error from the Arduino software that says "error: expected ',' or ';' before 'int.' No problem! You've forgotten a semicolon after one of the instructions in the sketch. Check the line above the line that gets highlighted and make sure there is a semicolon at the end. That wraps up the first Arduino Starter Project everyone! If you have any more questions about the project feel free to ask in the comments section. -Ryan |
||||||||||||||||||||||||||||||
Feeds
Currency
Display prices in
Feedback
If you would like to tell us more, you can fill out our form if you need some psycho-suggestive questions. Go to the form.









Run_Project_1.cpp line 26 to:
#include "../LED/LED.h"
Instead of
#include "..LEDLED.h"
Trying the HTML escape char instead...
For OS x and Linux change
Run_Project_1.cpp line 26 to:
#include "../LED/LED.h"
Instead of
#include "..LEDLED.h"
Great tutorial, and I hope you guys do several more. Especially useful would be a tutorial involving using intermediate circuits (like an H-bridge) to drive motors.
Is there any way you could post these in .pdf form as well? It doesn't come out very well when I try and extract the content from the page myself :/
int Red_On_Time = 1000; //This tells the Arduino Board tokeep track of a number named "Red_On_Time." We can also assign a number to "Red_On_Time," here we've assigned 5000.
So I guess we are assiging Red_On_Time a variable with an improper name called 1000 which is an integer value of 5000.
However, I ran into a few issues with some of the steps.
In the latest Arduino IDE version 0017 the library folder has been moved to the user folder. Here is the post on the Arduino site that describes the change. http://arduino.cc/blog/?p=313
I'm on a Mac, so for me the library folder is '/Users/brian/Documents/Arduino/libraries/'. I copied the unzipped library folders (provided above) to this location, restarted the Arudino App and that did the trick. I can now see them in the Sketch -> Import Library... menu list along with the other libraries that come pre-installed.
I got this error:
/Users/brian/Documents/Arduino/libraries/Run_Project_1/Run_Project_1.cpp:26:24: error: ..LEDLED.h: No such file or directory
Along with many "'LED' was not declared in this scope" errors.
When I opened the Run_Project_1.cpp file I realized that since I'm not on Windows I needed to edit the line in the #includes from:
#include "..LEDLED.h" to #include "../LED/LED.h"
Windows uses backslashes for folders whereas Macs use forward slashes. A small but annoying difference. After I made this change I was able to compile without any errors. A better solution would be to not use relative paths but let the compiler find the LED.h library. Since the IDE knows about it the compiler should be able to resolve the location and we can avoid having *any* path reference. In my copy I removed all the path information and just have #include "LED.h" instead. This worked just fine for me and it makes the tutorial code less platform dependent.
Hope this helps. SparkFun is teh r0x0rs!
The goal, presumably, is not just to make the lights blink, it is to understand HOW to make the lights blink, and if you do all the work for me so that all I have to do is download your sketch, I haven't learned much.
http://www.sparkfun.com/tutorial/AIK/ARDX-EG-SPAR-WEB.pdf
FYI, in CIRC08 (the potentiometer tutorial) you can substitute the force senstitive resistor for the potentiometer; they work the same way.
Hope this helps!