Skill level:     Beginner

Arduino Starter Kit Tutorial #1: Blinking LEDs

by ryowens84  |  
September 17, 2009  |  

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.

http://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_1_Small.JPGhttp://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_1a_Small.JPGhttp://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_1b_Small.JPG

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.

http://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_2_Small.JPG

What's that?

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.

http://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_3_Small.JPG

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).

http://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_4a_Small.JPGhttp://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_4b_Small.JPG

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).

http://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_5_Small.JPGhttp://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_5a_Small.JPGhttp://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_5b_Small.JPG

6. Plug the USB cable into the computer and the Arduino board.

http://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Step_6_Small.JPG




Sketch It!
1. Open up Arduino by double clicking the Arduino icon

https://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/loading.gif

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').

What's that?

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.

https://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Play.jpg

What's that?

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.

https://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Download.jpg

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:

//These two numbers will tell the Arduino Board how long to keepeach LED on for each cycle. The numbers are given in milleseconds.
//Hint: There are 1000 milleseconds in 1 second.

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.
//This will be the number of milliseconds the red LED stays on.

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.”

What's that?

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.

What's that?

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.

What's that?

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?

int Yellow_On_Time = Red_On_Time/2;//This tells the Arduino Board to keep track of a number named "Yellow_On_Time." Here we've assigned the number "Yellow_On_Timer" to be a half of "Red_On_Timer" (or Yellow_On_Timer = 1000/2).
//This will be the number of milliseconds the yellow LED stays on.

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?

int Red_Cycle_Time=2000;//This tells the Arduino Board to keep track of a number named "Red_Cycle_Time."
//Here we've assigned the number "Red_Cycle_Time" to be 2000.
//HINT: Notice that 2000 = 2 * Red_On_Time. This makes it so that the Red LED will be off for the same amount of time that it is on.
int Yellow_Cycle_Time=Red_Cycle_Time//This will be the number of milliseconds the yellow LED stays on.

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:

int Yellow_Cycle_Time=Red_Cycle_Time;//This tells the Arduino Board to keep track of a number named "Yellow_Cycle_Time.
//Here we've assigned the number "Yellow_Cycle_Time" to be equal to "Red_Cycle_Time"(or Yellow_Cycle_Time=10000).

Change your sketch so that it looks like the highlighted part of the sketch below:

int Yellow_Cycle_Time=Red_Cycle_Time/2;//This tells the Arduino Board to keep track of a number named "Yellow_Cycle_Time.

//Here we've assigned the number "Yellow_Cycle_Time" to be equal to "Red_Cycle_Time"(or Yellow_Cycle_Time=10000).

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.

https://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Play.jpg

4. Upload the sketch. Press the 'Upload' button to upload the sketch to the Arduino Board.

https://www.sparkfun.com/tutorial/Arduino_Tut_1/Pictures/Download.jpg

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:
  • Make sure the short ends of the LEDs are plugged into the same row of the breadboard as the black wire. Also make sure that the black wire is plugged into the GND hole of the Arduino board.
  • Make sure the green and blue wires each go to one of the long legs of the LEDs. Also double check that the green and blue wires are plugged into pins 2 and 3 of the Arduino board.
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

Back to Tutorials

 

Comments

19 comments


Log in to post comments.

by
md's rank:
+1
|   Oct  9, 2009 at  4:28am
Comment rating:
0
I was able to get the sketch to compile on ubuntu by changing:

Run_Project_1.cpp line 26 to:

#include "../LED/LED.h"

Instead of

#include "..LEDLED.h"
Breakpointer's rank:
+1.7
|   Nov  8, 2009 at  6:31pm
Comment rating:
0
Ah, it looks as if the comments system strips out (or ignores) the backslash character... It makes what should be #include "..(backslash)LED(backslash)LED.h" and makes it #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"
Breakpointer's rank:
+1.7
|   Nov  8, 2009 at  6:35pm
Comment rating:
0
Looks like the html escape char for backslash doesn't work here either. It is ampersand#92semicolon for those interested.
by
mellis's rank:
+4.1
|   Oct 13, 2009 at  7:30pm
Comment rating:
0
The libraries folder has changed in Arduino 0017. Libraries should be installed in the "libraries" sub-folder of your sketchbook folder. This means that they will continue to be found when you upgrade to a newer version of the Arduino software. It's particularly important on the Mac, where the default libraries are inside the Arduino.app package.
by
BenP's rank:
+2.8
|   Oct 14, 2009 at  6:57am
Comment rating:
0
Resistors?
by
Exonerd's rank:
+3.1
|   Oct 16, 2009 at  1:07pm
Comment rating:
0
Hi Ryan,
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 :/
by
GR81's rank:
+1
|   Dec  2, 2009 at 10:34pm
Comment rating:
0
I love it

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.
Striker121's rank:
+2.7
|   Nov  8, 2009 at 10:02am
Comment rating:
+0.15
I really have to agree with the guy asking for H-Bridge tutorials for the arduino, would be awesome. Also, you made a mistake when you described what int REDONTIME did, you set it to 1000 NOT 5000.
Breakpointer's rank:
+1.7
|   Nov  8, 2009 at  6:01pm
Comment rating:
0
Thanks for the good starter tutorial!
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.
Breakpointer's rank:
+1.7
|   Nov  8, 2009 at  6:02pm
Comment rating:
0
The second problem I ran into was with the Run_Project_1 library itself.
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!
by
sirkha's rank:
+2.5
|   Nov 13, 2009 at 10:55pm
Comment rating:
0
The forward slashes should work on any platform, including Windows, I think. On unix-based platforms is used as an escape character, usually. Other than that, and a few platform based hangups, I got this project going, pretty easily. I found this tutorial a little too basic, so I looked into the Run_Project file, saw what was going on, and managed to go a little further. I got my arduino to drive a 3-color LED to cycle through the colors. I am pretty happy with Arduino so far. Thanks for the tutorial.
by
peterbb's rank:
+1.5
|   Dec 29, 2009 at  8:11pm
Comment rating:
0
I feel that this tutorial would be much more useful if the actual source code for the setupExperiment() and runExperiment() functions were used in the main project file, rather than just the calls to these functions. This would allow the user to see what is actually happening in the program (why is it called a "sketch"??) and would allow him to change things besides the LED on times - like make the LEDs alternate. Showing the source for these functions would also provide useful guidance to assist the user in developing his own programs.
by
beb's rank:
+1.4
|   Jan  1, 2010 at  8:51pm
Comment rating:
0
I agree with this. In an attempt to make the tutorial transparent for newbies we appear to have obfuscated things for those conversant in C.
by
ctdahle's rank:
+1.3
|   Mar 11, 2010 at  1:45am
Comment rating:
0
I agree as well. I appreciate that these tutorials are available, but I'd rather not download the folders and just run files prepared by someone else. As an ambitious beginner, I'd much prefer, at this stage, to see the code and type it myself, or cut and paste it. I don't really learn how it works or gain a lot of understanding by just opening up someone else's sketch and running it.

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.
by
Ehudz's rank:
+1
|   Feb 11, 2010 at  5:50pm
Comment rating:
0
wait...why don't you need resistors? Don't you have more than the max voltage drop across your diodes?
driver13's rank:
+1.4
|   Mar 13, 2010 at  1:13am
Comment rating:
0
Hey I'm trying to do this with Arduino 0018 and it's not working, I get a big long list of errors while compiling. Any solutions?
driver13's rank:
+1.4
|   Mar 13, 2010 at  7:11pm
Comment rating:
0
Haha, I guess it helps to have the IDE installed properly. Problem solved.
by
delgesu's rank:
+1
|   Jul  2, 2010 at  4:30am
Comment rating:
0
Hey - thanks for the starter kit and the tutorial. My 10yo is very excited by making a computer follow his commands. Hope we can have some more tutorials utilising some of the other items in the kit.
|   Jul  2, 2010 at 10:14am
The Inventor's Kit is another Arduino beginners kit that shares many of the same components with the starter kit that you have (though it has a few more). There are a whole bunch of circuits/tutorials that you can find in the documentation for the Inventor's kit that will apply to the Starter kit. You can download it here:

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!


Feedback

What's on your mind?

Please include your email address if you'd like us to respond to a specific question.

submit


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.