facebook

Visit Facebook page!

follow on facebook to get instant news and updates.

Friday, 1 August 2014

Lesson 4 The serial library and binary data

Introduction
Ah, Arduino, I remember when you were just crawling around and blinking LEDs. Now you're ready to learn how to speak! In this lesson we'll learn how to use the Serial Library to communicate from the Arduino board back to the computer over the USB port. Then we'll learn how to manipulate numbers and data.
For this lesson we won't be using the shield, so simply remove it (keeping the mood light LEDs on it you'd like). The shield doesn't contain any programs or data, it is just our way of connecing up the LEDs and resistors. We'll use the shield again but for now, we can examine the RX and TX LEDs on the main Arduino board which will help you with debugging
What is a Library?
Libraries are great places, and not yet illegal in the United States! If you ever need to learn how to do something, like say fix a motorcycle, you can go to your local library and take out a book. Sure you could buy the book but the library is nice because as a resource you can get the book whenever you need it, keeping your house uncluttered.
Software Libraries are very similar. We already studied what a procedure is, in lesson 3: a procedure is a list of things to do. A library is a big collection of procedures, where all the procedures are related! If you, say, want to control a motor, you may want to find a Motor Control Library: a collection of procedures that have already been written for you that you can use without having to do the dirty work of learning the nuances of motors.
The library we will be using is the Serial Library, which allows the Arduino to send data back to the computer:
What is Serial?
Serial may sound like a tasty breakfast food, but its actually quite different. The word serial means "one after the other." For example, a serial killer doesn't stop with one murder, but stabs many people one after the other. Serial data transfer is when we transfer data one bit at a time, one right after the other.
Information is passed back & forth between the computer and Arduino by, essentially, setting a pin high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it. It's a little like Morse code, where you can use dits and dahs to send messages by telegram. In this case, instead of a long cable, its only a few feet.

This is as good as Microsoft Visio can do, yay!
(Now, people who are all geeked-out will probably get angry at this point because I'm simplifying things. Well guess what, its an Arduino tutorial, not a OSI Physical Network Architecture tutorial.)
Bits & Bytes
The world isn't run by weapons anymore, or energy, or money. It's run by little ones and zeroes, little bits of data. It's all just electrons. - Sneakers
Now is a good time to review how data is measured. For example, we measure weight with "ounces" and "pounds" (or grams and kilograms) and distances with "inches," "feet," and "miles" (or centimeters, meters and kilometers). Information has its own system of measurements:
single bit is either a zero or a one
You can group bits together into 8 bits which is 1 byte
1024 bytes (8192 bits) is one Kilobyte (sometimes written KB).
1024 KB (1048576 bytes) is one Megabyte (MB)
1024 MB is 1 Gigabyte (GB)
An interesting thing to note is while 1000 grams is a kilogram, nearly all computer systems consider 1024 bytes to be a kilobyte. That is, a 1.0 Kilobyte file on your computer is 1024 bytes:
If your hard disk is 200 Gigabytes, how many bytes is that? Use a calculator with lots of digits! 
Highlight the text below for the answer
200 GB * 1024 = 204800 MB
204800 MB * 1024 = 209715200 KB
209715200 KB * 1024 = 214748364800 bytes!
Hard drive makers are quite sneaky, you'll notice that they define GB as being 1000 MB, and 1 MB = 1000 KB, etc. Given this fact, how many bytes can you really store in your 200GB drive?
Highlight the text below for the answer 
200 GB * 1000 = 200000 MB
200000 MB * 1000 = 200000000 KB
How much less storage do you get thanks to the marketing guy who came up with this trick?
Highlight the text below for the answer 
About 4.6% less than you'd expect 
A familiar friend?
We've actually used the Serial communications capability already quite a bit...that's how we send sketches to the Arduino! When you Compile/Verify what you're really doing is turning the sketch into binary data (ones and zeros). When you Upload it to the Arduino, the bits are shoved out one at a time through the USB cable to the Arduino where they are stored in the main chip.
Next time you upload a sketch, look carefully at the two LEDs near the USB connector, they'll blink when data is being transmitted. One blinks when the Arduino is receiving data (RX) and one blinks when the Arduino is transmitting data (TX)
Time for our first sketch
Enough chatting amongst ourselves, its time to get the Arduino talking. Our first sketch is going to be the hello world!program. When it starts up, it will say "hello world!"
Create a New Sketch... and save it as HelloWorld
Into the new sketch, copy and paste the following source code, then save it
/*
 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 */


void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Hello world!");  // prints hello with ending line break 
}

void loop()                       // run over and over again
{
                                  // do nothing!
}

OK first thing to notice is that there's nothing in the loop procedure! We've gutted it...and put some stuff into thesetup procedure.
Even if we have nothing in the setup or loop procedures, the Arduino requires them to be there. That way it knows you really mean to do nothing, as opposed to forgetting to include them!
The first line of code in the setup procedure is this one:
   Serial.begin(9600);           // set up Serial library at 9600 bps
We definately see that there is a Serial thing going on, and it looks like there is a procedure call as well. This is alibrary procedure call. The library is called Serial and inside the library is a procedure called begin.
library name
.
procedure name
(input values)
;
Serial
.
begin
(9600)
;
If there's no library name, it means that the procedure is in the 'default' collection of procedures we use. For example,delay() is so common, the designers of the Arduino software didn't bother putting it into a library.
So there's some mystery procedure that's called begin, well it's not too tough to figure out what it might do. It's the procedure that gets the Serial stuff ready. But what's the 9600 about? 
The comment says 9600 bps, and just so you know bps stands for bits-per-second (we will refer to this as the baud rate)
If you have broadband connection, you may remember reading somewhere that it has, say 350 kbps download rate. This is how fast the connection can read and write bits on the wire. (Needless to say, your broadband connection can transfer data a lot faster than an Arduino!)
OK so Serial.begin sets up the Arduino with the transfer rate we want, in this case 9600 bits per second.
Lets move on to the next line.
Serial.println("Hello world!");  // prints hello with ending line break 
This line also uses the Serial library, this time it's calling a procedure called println which is just a shorthand for "print line". Note that the 6th letter in println is the letter L not the number 1. This time the input is a quotation, the line of text we would like it to print. We use two "'s (double quotes) to indicate the beginning and end of a line of text.
  • If the Arduino transfers data at 9600 bits per second and you're sending 12 bytes of data, how long does it take to send over this information?Highlight the text below for the answer 12 bytes of data equals 12 * 8 = 96 bits of data. If we can transfer 9600 bits per second, then 96 bits takes 1/100th of a second!
  • If the Arduino transfers data at 19200 bits per second (19200 baud) and you're sending 12 bytes of data, how long does it take to send over this information?Highlight the text below for the answer This is twice as fast as before, so it will take half the time, about 1/200th of a second.
Good, now compile the sketch and upload it to your Arduino....
And then...nothing???
It looks like not much is going on here. Somewhat disappointing since we had so much fun with blinking colored lights before. The trick here is that while you can see blinking lights quite easily, seeing serial data requires a monitor, which like your display monitor will show us what data is being transfered.
Lucky for us, there's a serial monitor built into the Arduino software!
I'm not quite sure what the icon means, but regardless if you click that button you will replace the black Program Notification area with a Serial Monitor.
So...click it! 
Hello...world?
What happens next is, sadly, quite dependent on which kind of Arduino you have
 WindowsMac OS XLinux
NGArduino does not reset.Arduino does not reset.Arduino does not reset.
DiecimilaArduino resets, starts the sketch a few seconds laterArduino resets, starts the sketch a few seconds laterArduino resets, starts the sketch a few seconds later
In the very common case of having a Diecimila Arduino, the serial monitor will auto-reset the Arduino. The sketch will start up a couple of seconds later
Otherwise, the Arduino does not reset itself. Either way, once you've switched to the serial monitor, press the reset button. If you have an NG Arduino you'll have to wait 7 seconds for the sketch to start.
Voila! It is our sketch!
Baud rate match up!
If you ever find that you're getting a whole lot of gibberish instead of proper text, make sure that you have the correct baud rate selected in the drop down menu of the Serial Monitor. Note that this communication baud rate is indepedent of the upload process, which is fixed at 19200 bps.
Next, try pressing the reset button a few times to make more Hello Worlds! appear. If you have an NG, this may be a bit annoying but do it anyways.
Each time you reset the Arduino, it performs the setup procedure, and prints out Hello again. If you look closely at the Arduino, you will also see the little TX LED blink just as it prints out this message. That's your indication that data was sent.
What's Send do?
When you println you are sending data from the Arduino to the computer. The Send button (and the text input next to it) are used to send data to the Arduino. We aren't going to be using it in this lesson so don't be surprised that it doesn't do anything when you click it!
10 PRINT HELLO
20 GOTO 10
Our next sketch will be a minor modification of this one. Instead of printing out Hello World just once, we'd like it to print it out over and over and over again.
  • What simple modification should we perform to make the Arduino print Hello World over and over again? Highlight the text below for the answer 
    Simply move the Serial.println("Hello world!"); statement from the setup procedure to the loop procedure.
Perform this modification and then compile and upload the new hyper-hello sketch. Then start up the serial monitor. You will see Hello World! scroll by super fast!
  • Whats going on with the TX LED? Highlight the text below for the answer 
    It's lit, not blinking
  • Try waving the Arduino around in a dark room, what do you see? Highlight the text below for the answerThere are little dotted light trails
  • What's going on here? Hint: Remember lesson 2Highlight the text below for the answer 
    The data is being transmitted so fast, that we can't see the TX LED blinking...it's sending data many times a second!
Make the Arduino chill out a little by adding a one second delay to the sketch, so that it only prints out Hello World once a second.
/*
 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 */


void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop()                       // run over and over again
{
  Serial.println("Hello world!");  // prints hello with ending line break
  delay(1000);
}

Now you should spend some time playing with println and making it display a message of your choice! Perhaps add some more println statements in order to make the message longer?
Math is hard, let's try programming!
We've played around with printing out phrases, but it turns out we can also print out numbers pretty easily too.
/*
 * Math is fun!
 */

int a = 5;
int b = 10;
int c = 20;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Here is some math: ");

  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.print("c = ");
  Serial.println(c);

  Serial.print("a + b = ");       // add
  Serial.println(a + b);

  Serial.print("a * c = ");       // multiply
  Serial.println(a * c);
  
  Serial.print("c / b = ");       // divide
  Serial.println(c / b);
  
  Serial.print("b - c = ");       // subtract
  Serial.println(b - c);
}

void loop()                     // we need this to be here even though its empty
{
}
Try out this sketch on your Arduino
Note that we're using 2 procedures here, the original println and now also print. The print procedure is just likeprintln except it does not print out a "carriage return" at the end, starting a new line. You can experiment with changing the print's to println's and looking at the Serial Monitor output to verify this for yourself.
Here's whats going on in the Arduino with this sketch. For example, lets look at this line:
  Serial.println(a);
We've seen that if you use a quoted line of text as input to println procedure, it will display that text. In this case you can see that if you use a variable to println it will look up what that variable contains and print that out!
It turns out that the Arduino is smart enough to also do math when asked:
  Serial.println(a + b);
In this case, the Arduino looks at what the input to println is, and finds its actually a calculation. It looks up what a is (5) and what b is (10) and then adds them together (+) and then uses that as the value to send to println
Note that for now, we can only do math using integers, which if you recall, are whole numbers. That means we can't yet print out numbers like 3.14 or 1.5.
I could go on and on about operators, its all very important stuff, but many people have written good tutorials on this topic already so I'm going to send you off to read them there!
Pythagorean party
Let's make our first simple calculator, to calculate a hypoteneuse. If you remember from grade school, if you have a right-triangle, the hypoteneuse h can be calculated from the lengths of the two legs, c1 and c2 (which we'll call a & b)
a2 + b 2 = h 2
h = (a2 + b2)
/*
 * Math is fun!
 */

#include "math.h"               // include the Math Library

int a = 3;
int b = 4;
int h;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Lets calculate a hypoteneuse");

  Serial.print("a = ");
  Serial.println(a);

  Serial.print("b = ");
  Serial.println(b);
  
  h = sqrt( a*a + b*b );
  
  Serial.print("h = ");
  Serial.println(h);
}

void loop()                // we need this to be here even though its empty
{
}
    
The first thing that's new here is this line at the very beginning of the sketch:
#include "math.h"               // include the Math Library header
Which basically says "We'd like to use the math procedures, which are in a library that requires us to include the file math.h where the sqrt procedure lives". Just ignore it for now, it's not important.
The second thing that's different here is that when we create the variable h we don't assign it a value.
int h;
It turns out that this is totally OK, it just means that we don't know what is going to store yet, because we're going to calculate it later. Since it's not assigned to a value upon creation, the Arduino just creates the box, the stuff inside is whatever was in left over in memory.
Default values
If you don't assign a value to a variable, it could be any value. Make sure you don't try to use the variable before you assign it a value!
Later on in the sketch, we assign it the value.
   h = sqrt( a*a + b*b );
In this line, we square a and b and then add them together, then we call the sqrt() procedure (which does exactly what you may think), to take the square root. Then we assign that to the variable h.
Variable
=
Value
;
Whatever was in before is lost, replaced by the new value.
You can nest procedures and functions all you want, calling a procedure on the return value of another procedure.
  • Lets say you have a variable "foo" which contains a number. You'd like to find the square root of the square root of this number. What line of code would print out this value?
    Highlight the text below for the answer
    Serial.println( sqrt( sqrt(foo) );
    First take the square root of foo, then take the square root of that and then use that value as the input to println
Now its your turn to create a calculator
You'll create a Ohm's law calculator. Ohm's law says that Voltage = Current * Resistance. (This is a pretty useful law which forms the basis of electronics, and we'll study it in depth more later.) Starting with two variables, i for current and for resistance, have it print out the amount of voltage that can be measured accross the resistor.
Drive size calculator
Let's write a program that will do that hard drive size calculation we did before. We'll start with the hard drive size in GB, and print out how many MB there are.
We'll start simple, just printing out the drive size in GB
/*
 * Drive size calculator!
 */

int drive_gb = 5;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("Your HD is ");
  Serial.print(drive_gb);
  Serial.println(" GB large.");
}

void loop()               // we need this to be here even though its empty
{

}
Copy and paste this sketch into Arduino software and name the sketch DriveCalc. Then compile and upload it.
OK, lets add a section of code that will print out the number of Megabytes in the hard drive.
/*
 * Drive size calculator!
 */

int drive_gb = 5;
int drive_mb;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("Your HD is ");
  Serial.print(drive_gb);
  Serial.println(" GB large.");

  drive_mb = 1024 * drive_gb;

  Serial.print("It can store ");
  Serial.print(drive_mb);
  Serial.println(" Megabytes!");
}

void loop()                 // we need this to be here even though its empty
{

}
This time if you compile and upload it you should get
Which is correct, yay! Now try a few different whole numbers for the drive size, from 1 GB to 100 GB.
You may notice that if you put in a 100GB drive size, something very, very strange happens:
A 100GB drive should have 102400MB in it, not some negative number. What's going on here?
Introduction to types (part 1)
What's happening is that we have an overflow problem. Think about your car odometer. The odometer has only 4 digits, it can display 0 miles to 9999 miles travelled. If you travel 10000 miles, the odometer will "roll over" to 0 again, and from then on it will display an incorrect value.
Keeping that in mind, remember in lesson 2 we said that when we define a variable we also define the box-type of the variable? The box is where we store the data, in this case the type is int. It turns out that an int type can store only 2 bytes.
Quick quiz!
How many bits are in 2 bytes?
Highlight the text below for the answer 
There are 8 bits in 1 byte so 2 bytes is 16 bits
To figure out how big a number we can store in a 2 byte-sized box use a calculator and take 2 to the power of the number of bits (since each bit can store 2 values, 0 or 1). Then we subtract 1 because like in the car odometer, you can't actually display the final value, 10000. So, in this case the largest number is 216 - 1 = 65535. Since the number we're trying to store (102400) is larger than that, we see that "rollover."
OK let's fix it! All we need to do is change the variable type so that it can store more than 2 bytes of data. Here is a short list of types we can use.
TypeSize (bits)Size (bytes)
byte
8
1
int
16
2
long
32
4
It looks like we want to use the long type. So lets make that change
/*
 * Drive size calculator!
 */

int drive_gb = 100;
long drive_mb;                  // we changed the type from "int" to "long"

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("Your HD is ");
  Serial.print(drive_gb);
  Serial.println(" GB large.");

  drive_mb = 1024 * drive_gb;

  Serial.print("It can store ");
  Serial.print(drive_mb);
  Serial.println(" Megabytes!");
}

void loop()                // we need this to be here even though its empty
{

}
Compile and upload this sketch....then run the Serial Monitor....
Uh oh, we didn't actually fix the problem!
How frustrating, we did the right thing and it still didn't work. The problem we have now is although the boxes are the right size, we're not handling them well.
 drive_mb = 1024 * drive_gb;
If you look at this line, what's happening here is that the Arduino looks up the value of the variable drive_gb to get 100. Then we multiply 100 by 1024 to get 102400 and put that in the drive_mb box. Except the way that the Arduino software does this is that it creates a temporary variable the same size as drive_gb to store that calculation result before it sticks it into drive_mb. So basically we are still getting an overflow, except now its happening as we do the calculation.
Here is one way to fix this insiduous bug:
/*
 * Drive size calculator!
 */

int drive_gb = 100;
long drive_mb;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("Your HD is ");
  Serial.print(drive_gb);
  Serial.println(" GB large.");

  drive_mb = drive_gb;
  drive_mb = drive_mb * 1024;

  Serial.print("It can store ");
  Serial.print(drive_mb);
  Serial.println(" Megabytes!");
}

void loop()            // we need this to be here even though its empty
{

}
Now when we do the calculation, the temporary result is stored in a box the same size as drive_mb (a long) instead of an int.
Compile and upload this sketch to try it out
There you go, now its working!
Introduction to types, part 2
It turns out that I wasn't completely honest in the previous section when I described all the different types. There's another important fact to know, and that has to do with storing negative numbers.
We know that a variable that is 2 bytes large (16 bits) can store 216 different values. We assumed before that these values were 0 - 65535 inclusive. But then how do we store negative numbers? It turns out that there are two kinds of variables, signed and unsigned.
Signed variables can have a positive or negative value, so you can store negative numbers. 
Unsigned 
variables can only store positive numbers.
By default, variables are signed.
TypeSize (bits)Size (bytes)Minimum ValueMaximum Value
unsigned byte
8
1
0
255
byte
8
1
-128
127
unsigned int
16
2
0
65535
int
16
2
-32768
32767
unsigned long
32
4
0
4294967295
long
32
4
-2147483648
2147483647
  • Lets say you have a program that stores the age of a human in years (which so far is no more than 122), whats a good data type to use?
    Highlight the text below for the answer 
    You probably want to use a byte type
  • Lets say you want to store the age of a human in seconds, what is an appropriate data type now?Highlight the text below for the answer 
    110 years = 3468960000 seconds. You'll need to store this in an unsigned long variable.
Why Types?
OK so you're probably wondering: "This is such a pain, why bother with different size types? Lets just have every variable be as big as possible and we'll never have roll-over problems."
Well, on your desktop computer, with gigabytes of memory (RAM), this is a reasonable thing to do. However, the tiny tiny computer in the Arduino has a grand total of 1 Kilobyte of memory. And some of that is used for background stuff you don't see. For small sketches sure you can make everything a long and be done with it, but if you have a bigger sketch, you'll run out of memory really fast and then you'll have major problems. So in this case, every byte counts!
What's weird about signed numbers is that if you reach the end of the positive value range you'll rollver into the negative values.
For example, lets say you have a signed int. If you have the value 32767 in that variable, and you add 1 to the variable, you'll actually rollover to -32768.
This sketch will test out this fact:
int test = 32767;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("Test value is: ");
  Serial.println(test);

  test = test + 1;
 
  Serial.print("Now it is ");
  Serial.println(test);
}

void loop()            // we need this to be here even though its empty
{

}
Compile and upload to run the test.
  • Let's say we have a variable that is byte type, it's signed by default. It starts out with the value 127 and we add one to the variable, what will the new variable value be?
    Highlight the text below for the answer 
    It's a signed variable so it will roll over to -128
  • Let's say now it is an unsigned byte type, what happens now?
    Highlight the text below for the answer 
    Since it is unsigned, it can store much more data, so it will be able to hold the value 128.
  • If we have an unsigned byte and it starts out with the value 250 and we add 10, what will the value be?Highlight the text below for the answer 
    Even though thie variable can store a lot, it can't store more than the number 255, so it will rollover and we'll end up with the number 4.
Now it's your turn!
Write some sketches that will help you understand variable sizes, try creating variables of different types and signedness and adding and subtracting.
Although this lesson part seems quite boring, and severely lacking in blinky lights, understanding this stuff now will save you from a lot of headaches later when you have data overflows and your program is all wonky and you're really frustrated because you can't figure out why. (Trust me on this one!)
Wrapping up, the final project!
Now its time for you to expand the drive size calculator. Starting with the DriveCalc sketch, modify it so that it will also calculate how many KB are stored in the hard drive. Test it out with a couple different drive sizes.
Once you've got that working, modify it again so that it will also display how much space the drive actually holds thanks to the sneaky math-trick that manufacturers use. Have the sketch display how much storage space is 'missing' (in KB) as well.
Here's one possible solution:
/*
 * Drive size calculator!
 */


int drive_gb = 100;
long drive_mb;
long drive_kb;
long real_drive_mb;
long real_drive_kb;


void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("Your HD is ");
  Serial.print(drive_gb);
  Serial.println(" GB large.");

  drive_mb = drive_gb;
  drive_mb = drive_mb * 1024;
  drive_kb = drive_mb * 1024;
  
  Serial.print("In theory, it can store ");
  Serial.print(drive_mb);
  Serial.print(" Megabytes, ");
  Serial.print(drive_kb);  
  Serial.println(" Kilobytes.");

  real_drive_mb = drive_gb;
  real_drive_mb = real_drive_mb * 1000;
  real_drive_kb = real_drive_mb * 1000;
  
  Serial.print("But it really only stores ");
  Serial.print(real_drive_mb);
  Serial.print(" Megabytes, ");
  Serial.print(real_drive_kb);  
  Serial.println(" Kilobytes.");
  
  Serial.print("You are missing ");
  Serial.print(drive_kb - real_drive_kb);  
  Serial.println(" Kilobytes!");
}

void loop()                       // run over and over again
{
}

Conclusion
Good work, you got through one of the more boring lessons. In this lesson you learned how to print text and data to the Serial Monitor. This is essential for debugging future projects! You also learned about data types and storage and how to use the Arduino to calculate stuff.

Lesson 3 - Breadboard and LEDs

Introduction
You've started modifying sketches, and played a bit with the onboard LED (or if you have an NG, an LED you added). The next step is to start adding onto the hardware component of the Arduino. We will do this by adding a solderless breadboard to our setup, connecting up new parts with wire.
Get your gear
Assembled Arduino board, preferrably a Diecimila (or whatever the latest version is) but NG is OK too
$35
USB Cable. Standard A-B cable is required. Any length is OK.
Or any computer supply store
$5
LEDs
For this lesson, a red, green and blue LED are best. Make sure you get a "5mm" or "3mm" LED, with two legs, as shown in the example image. "Ultrabright" LEDs (1000 mcd rating or higher) are preferred
Any electronics supply store
Three 1KΩ Resistors (brown black red gold)
Any values from 300 Ω to 2KΩ are probably OK.
Any electronics supply store
$1
Arduino Prototyping Shield with tiny breadboard 
Standard solderless breadboard
If you dont have a protoshield, this is a substitute
$5
Many hobby shops and electronics stores will have these
Hookup Wire
Get 22 gauge solid-core wire in red, black and some other color. Make sure its notstranded wire!
Any hardware store
Say hello to the solderless breadboard!
Solderless breadboards are an important tool in your quest for electronics mastery. They allow you to make quick circuits, test out ideas before making a more permanent Printed Circuit Board. They're also inexpensive and reusable.. You can pick on up at any hobby shop or electronics supply store. They often look like this
Basically, a chunk of plastic with a bunch of holes. However, something special is going on inside the breadboard! Although you can't see it, inside the breadboard are many strips of metal that connect the rows and columns together. The metal strips are springy so that when you poke a wire into the hole, the clips grab onto it.
In the images above you can see how there are two kinds of metal strips. There are short ones that connect 5 row holes at a time, and then there are very long ones that connect 25 (or more!) column holes at a time. The long columns are called rails and the short strips are called rows. Breadboards are almost always made so that they have two sets of 5-hole rows and on either side there are a pair of rails. For example the breadboard on the left has 30 row pairs and 2 sets of double rails on either side. The one on the right is quite small, it has only 17 row pairs and no rails.
In this lesson, we will show pictures of both the tiny breadboard on a protoshield and also using a 'standard' breadboard without a shield. However, after this lesson, you'll be more on your own to figure out how to connect up the standard breadboard, OK?
Warning!
Distressing as it may sound, solderless breadboards can be very flakey, especially as they age. If you're having problems with your circuit, it could be that the little metal clips on the inside aren't working well. Try poking it with your finger, or moving it to a different section.

Say hello to wires!
To use the breadboard, you'll need jumper wires. These are basically 22 gauge solid-core (not stranded) wires that are cut down and have the insulation pulled off. You can use a fingernail or, best of all, a real wirestripper tool to remove the insulation, just takes a few tries and then its really easy.
Heres how to do it with just diagonal cutters...Cut the wire first, using wire cutters
Nick the insulation, then pull it off.
To connect rows together, just stick the wire ends without insulation into the square holes!
Now is a good time to practice making jumpers, go forth and make a few 3" long jumpers!
Say hello to the resistor!
The resistor is the most basic and also most common electronic part. An electronic gadget, such as an mp3 player has easily a thousand resistors inside of it!

Behold...a resistor!
Resistors have one job to do, and that is to resist the flow of electricity (otherwise known as current). That's why they're called resistors. By resisting current they control where and how fast it flows.
One common way of thinking about this is if we were talking about water current, then pipes are like resistors. Thin pipes let less water through (high resistance), thick pipes let a lot of water through (low resistance). Wth a fire hydrant, you want low resistance. With a water fountain, you'd want high resistance. If you mixed up the two pipe sizes, you wouldnt be able to put out a fire and you'd hurt yourself while trying to get a drink.
Resistance is measured in ohms, often written as the symbol Î©. The bigger the resistance value (in ohms) the more it fights. Most resistors you'll see range between 1 ohm and 1 megaohm (1.0 MΩ). Since the resistive element is inside a ceramic casing, its not possible to tell the resistance of a resistor just by looking at it. You'll have to read it by looking at the colored stripes on the body of the resistor. This is known as the resistor color code, and its a real pain when you first start electronics. Eventually you'll get really good at telling the value of a resistor just by glance but to start off you'll want to use a reference chart. (Or you can use a multimeter to measure the resistance accurately) 

There are also website calculators that you may find very handy
Remember: Just because the stripes are in a certain order doesn't mean the resistor has a direction! Resistors are the same forward and backwards, it doesnt matter which way they are used.
  • What is the color code for a 5% 1.0KΩ resistor?
    Highlight the text below to see the answer 
    Brown - Black - Red - Gold
  • What is the color code for a 5% 220Ω resistor?
    Highlight the text below to see the answer 
    Red - Red - Brown - Gold
  • What is the value of this resistor?
    Highlight the text below to see the answer 
    The stripes are yellow (4) - violet (7) - red (* 100) = 4700 Ω = 4.7KΩ
  • What happens if you put a resistor in backwards?
    Highlight the text below to see the answer 
    Ha! Trick question, it is not possible to put a resistor in 'backwards'. They work either way!
Note on Wattages....
In all these examples, we use 1/4W resistors. Unless otherwise noted you can use 1/16 W or 1/2W or whatever you can get your hands on. Higher wattage resistors are larger and usually more expensive, but sometimes your local hobby shop will only have 1/2W.
Say hello to the LED!
We've had some time with the LED already, but lets get to know her a little better. The word LED stands for Light Emitting Diode. The light-emitting part, well, that makes sense. We've used the LED to make a blinking light in lessons 1 and 2. The LED component turns current into light, much like any sort of light bulb. But what is this mysterious diode?
A diode is basically a one-way street for current. Imagine such a one-way street with a traffic policeman in front. If you want to turn onto the street the wrong way, he will not let you. Likewise the diode simply does not let current go through it the wrong way. Current in a diode can only flow from the positive side to the negative side.
If you recall from lesson 1, Arduino NG users had to make sure that they inserted the LED in the right way. If you place the LED in backwards it won't work. Diecimila Arduino users already have the LED (a very very small one) soldered onto the circuit board the right way.

Look again! Its a tiny LED
As we mentioned before, its easy to figure out which side of an LED is positive and which one is negative. The positive leg is slightly longer and if you look inside, the chunk of metal is larger on the negaive side.

Light up my breadboard
We're going to now use the breadboard to light up an LED. You will need a breadboard, an LED and a 1.0K ohm resistor (brown black red gold). If you have a protoshield, make sure its assembled first. Then, place the tiny breadboard on top. You can remove the backing to stick it on (which is permanent) or you can just use double-sided tape. If you have a regular breadboard you'll need 2 jumper wires as well.
Important Note!
While LEDs will not work when placed backwards, you don't have to worry about whether it will be damaged: as long as there is a series resistor of at least 100 ohms next to it, the LED will survive the experience!
However, using an LED without a series resistor is a sure-fire way to kill it! (You'll see a bright flash and it may turn dark) 
Always
 use a resistor! A 1.0K ohm is a good place to start. We'll cover how to figure out the best resistor value later on.
Place the resistor and LED as shown. Make sure the longer leg of the LED is to the right, connected to the resistor. The resistor doesn't have a direction, so it doesnt matter which way it goes in.

Click for a high resolution photo if necessary!
If you're using a standard breadboard, you'll need to use wires to reach the Arduino. Run one wire (red) to the 5Vsocket on the Arduino. Run the other wire (black) to one of the GND sockets on the Arduino. The colors aren't essential but they will help you remember what the wires are connected to!
Plug in the Arduino, you should see the LED light up. If not, check the following:
  • Is the Arduino plugged in? (look for the little green light on the Arduino as in lesson 0)
  • Is the LED in backwards? Try flipping it around, just in case. This wont damage the LED.
  • Are the parts firmly placed in the breadboard? Loose parts are a common breadboard problem, try jiggling them with a finger and see if it starts working.
  • Is the LED on and its just very dim? Try turning down the lights or looking at it head on: some LEDs are very directional.
  • Is the red wire going into the hole labeled 5V? Is the black wire going into one of the holes labeled GND?
  • Try another LED in case this one is damaged
  • Make sure the parts are as shown in the image above, if you have a wire in one row and the resistor in the other, they aren't connected and it wont work!
Scheming schematic
Hooray, you just built your first circuit! Its quite simple but still worth explaining.
Basically you've connected the LED and resistor in series (one after the other) to a 5V 'battery'. The positive pin of the LED is connected to the positive terminal of the battery, then the negative pin is connected to a resistor which goes to the negative terminal of the battery. The battery is supplying the current that flows through the LED, making it light up.
The positive and negative battey terminals are often called the power supply, as they supply power to our circuit. The positive terminal is called power (as thats where current flows from) and the negative terminal is called ground, as it is where current flows to.
Lets say you want to "save" this design and send it to a friend to check out and build for herself...one way you could do that is to take a good photo. But a better way is to draw a wiring diagram. Then it wouldn't matter if your camera wasn't very good. A wiring diagram is also known as a schematic. Schematics are the standard method for people to trade information about circuits. Being able to read and write schematics is a key skill! Here is a schematic for a really big project, a Roland TB-303 synthesizer clone
Each electronic component has a schematic symbol, which is a simplified drawing of the part. For resistors the symbol looks like this:

Resistor symbol
And the symbol for LED's look like this:

LED symbol, positive pin on the left, negative pin on the right
You can see that the resistor symbol is symmetric, just like resistors themselves. The LED symbol, however, has an arrow thing going on. This is the direction in which current flows. The little arrows that are coming out of the symbol indicate that this is a diode that emits light.
Power and ground also have symbols:

Power and Ground symbols
The only thing we need to do now is indicate how the LED and resistor are hooked up and show the 5V and ground connections.

A barebones schematic
Next to symbols, we often write important information like what the resistor value is, what color and size the LED should be, and the voltage associated with the power supply.

A well documented schematic!
For practice, try drawing your own schematic on a piece of paper.
A Quick Rewiring...
We're going to make a very small modification to our wired up circuit
In our new schematic, instead of connecting the resistor to +5V power, we'll connect it to ground.
Before you change your breadboard, make a guess of what will happen:
Will the LED stay lit?
Will the LED go out?
Something else?
Now make the change to your breadboard:
You will notice that, in fact, the LED has gone out. That is because it is no longer connected to a power source and current is not flowing. By connecting the resistor to +5V or ground, you can turn the LED on and off. If you were very fast at it, you could make the LED blink!
Hmm....
Re-visiting an old friend
Start up the Arduino software again and open up the MyBlink sketch from lesson 2. If you left it with delay times of 10ms, you may want to modify it so its back to 500ms on and 500ms off. Upload the sketch to your Arduino. Now change your breadboard wiring so that it matches this schematic.
That is, instead of connecting the resistor to 5V or ground, connect it to the Arduino pin socket labeled 13. If you have an NG Arduino, you'll need to remove the old LED you used, if its still in the socket.
You should see the LED turn on and off. If you have a Diecimila Arduino, both the on-board LED and the wired LED will blink in unison. Lets look at that code again
void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(500);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(500);                  // waits for a second
}
We didn't quite explain what digitalWrite does, but now it should be clear: the digitalWrite procedure connects the pin indicated by the first input (ledPin) to either the +5V power supply or to ground depending on the second input (HIGH or LOW
This is a pretty awesome capability and is the basis of all electronics! You may want to think about how cool it is for a few moments.
A new pin
Now change the wiring so that the resistor is connected up to pin socket #12
The LED isn't be blinking anymore! Lets fix it!
Go back to the beginning of the sketch and find this line again
int ledPin = 13;                // LED connected to digital pin 13
This is the line of code that indicates which pin is connected to the LED. Change it so that it is now connected to pin 12
int ledPin = 12;                // LED connected to digital pin 12 now!
Re-compile and verify the sketch, then send it over the the Arduino. The LED should now be blinking again. Note that if you have a Diecimila Arduino you will not see any blinking on the on-board LED. Thats because its connected to pin 13 only!
  • Spend some time experimenting with different pins. Connect the LED to different pin sockets, and modify the sketch so that the LED blinks.
  • Change around your wiring so that it matches this schematic:

    Make sure to modify you sketch so that the ledPin is 13 again. Re-compile and upload it to the Arduino. What does the LED do?
    Highlight the text below to see the answer 
    It blinks just like before
    If you have a Diecimila Arduino, what do you notice about the breadboard LED and the on-board LED?
    Highlight the text below to see the answer 
    They are alternating when they blink
    Why do you think that is?Highlight the text below to see the answer When the pin is LOW (connected to ground) the breadboard LED is on: current is flowing from +5V to ground through the pin. When the pin is HIGH (connected to +5V) the on-board LED is on, just like before.
Adding a green LED...
OK sure you've had plenty of practice messing around with LEDs. It's time to go full color! Find a red, green and blue LED. If you have the Arduino Starter Pack they will be the three clear LEDs. You can't tell which one is which until they are lit so just build the circuit and then rearrange them if needed.

Red, green and blue LED schematic
In this schematic we will have three LEDs connected to three different pins: #10, #11 and #12.
Go back to your sketch and change it so it looks like this:
int redPin = 12;                  // Red LED connected to digital pin 12
int greenPin = 11;                // Green LED connected to digital pin 11

void setup()                      // run once, when the sketch starts
{
  pinMode(redPin, OUTPUT);        // sets the digital pin as output
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
}

void loop()                       // run over and over again
{
  digitalWrite(redPin, HIGH);     // sets the Red LED on
  digitalWrite(greenPin, HIGH);   // sets the Green LED on
  delay(500);                     // waits for half a second
  digitalWrite(redPin, LOW);      // sets the Red LED off
  digitalWrite(greenPin, LOW);    // sets the Green LED off
  delay(500);                     // waits for half a second
}
You can just copy and paste this text into your Arduino software window.
Quick quiz 
What does this sketch do? Compile and upload the sketch to test your hypothesis.
Highlight the text below to see the answer It blinks the two LEDs connected to pins 11 and 12 at the same time

If you are having problems getting this sketch to work, double check:
  • Is the sketch compiling properly? Did it upload correctly?
  • Are the LEDs in the right way?
  • Are the resistors in the right sockets?
  • Are the LEDs connected to ground on the other side?
  • Is the breadboard wired up right? Check your connections.
  • Change the code so that the LEDs alternate their blinks:

    Highlight the text below to see one possible solution
    Change the second digitalWrite() procedure call to set the pin LOW, and the fourth call to set the pin HIGH.
  • Change the loop() procedure code so that both LEDs are on for 500 ms, then only the red LED is on for 500 ms, then both LEDs are off, and finally only the green LED is on for 500 ms
    Highlight the text below to see one answer 
    void loop()                       // run over and over again
    {
      digitalWrite(redPin, HIGH);     // sets the Red LED on
      digitalWrite(greenPin, HIGH);   // sets the Green LED on
      delay(500);                     // waits for half a second
      digitalWrite(redPin, HIGH);     // sets the Red LED on
      digitalWrite(greenPin, LOW);    // sets the Green LED off
      delay(500);                     // waits for half a second
      digitalWrite(redPin, LOW);      // sets the Red LED off
      digitalWrite(greenPin, LOW);    // sets the Green LED off
      delay(500);                     // waits for half a second
      digitalWrite(redPin, LOW);      // sets the Red LED off
      digitalWrite(greenPin, HIGH);   // sets the Green LED on
      delay(500);                     // waits for half a second
    }
Full color adventures!
After successfully adding support for the green LED its time to add in the blue LED.
Go back to this sketch, the one from the last step
int redPin = 12;                  // Red LED connected to digital pin 12
int greenPin = 11;                // Green LED connected to digital pin 11

void setup()                      // run once, when the sketch starts
{
  pinMode(redPin, OUTPUT);        // sets the digital pin as output
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
}

void loop()                       // run over and over again
{
  digitalWrite(redPin, HIGH);     // sets the Red LED on
  digitalWrite(greenPin, HIGH);   // sets the Green LED on
  delay(500);                     // waits for half a second
  digitalWrite(redPin, LOW);      // sets the Red LED off
  digitalWrite(greenPin, LOW);    // sets the Green LED off
  delay(500);                     // waits for half a second
}
You can just copy and paste this text into your Arduino software window.
Now you will add the code for the Blue LED by yourself
Step 1. Add the line of code that will create a variable called bluePin. What pin should it be assigned? Examine the schematic above to find out.
Step 2. Add the line of code that will tell the Arduino that bluePin is a digital output.
Step 3. Add the 2 lines of code so that the blue LED will be lit when the red and green LEDs are lit
Compile and verify your code. Does it work? If not use your debugging skills to figure out what is wrong and fix it!
Color mixing
Now that you have red, green and blue light, you can start having fun with color mixing. Color mixing is the neat ability that our eyes have to combine different light colors and create a new color
 
A additive (light) color mixing diagram
According to this diagram, if we have both red and blue light mixed together we should get a violet light.
Quick quizModify your code to create the following colored light: Violet (red & blue), Turquoise (blue & green) and yellow (green & red)
Now you are ready for the "final project" of this very long lesson. You are going to make a color changing light! 
Modify the sketch so that the emitted light goes in order: red, yellow, green, turquioise, blue violet and back to red. It should pause about half a second between each color change.
Hint: One way to make the color mixing work better is to diffuse the light, in a light box. You can make a light box out of plain paper, scissors and some tape. Just make a paper box and cut a hole in it. Fill the box with tissue paper. The tissue acts as a diffuser, helping the light mix nicely