2012-05-15

Arduino Tutorial: Lesson 3 - Breadboard and LEDs


Arduino Tutorial: Lesson 3 - Breadboard and LEDs

This tutorial is by Limor Fried and from http://www.ladyada.net/learn/arduino 


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


USB Cable. Standard A-B cable is required. Any length is OK.

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

Three 1KΩ Resistors (brown black red gold)
Any values from 300 Ω to 2KΩ are probably OK.

Arduino Prototyping Shield with tiny breadboard 

Standard solderless breadboard - If you don't have a protoshield
this is a substitute

Hookup Wire - Get 22 gauge solid-core wire in red, black and some other colour. Make sure its not stranded wire!





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


View a reference chart 
Remember: Just because the stripes are in a certain order doesn't mean the resistor is polarised! Resistors are the same forward and backwards, it doesn't matter which way they are used.


Quick quiz!


What is the colour 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 the picture 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 Wattage ...
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.


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 doesn't matter which way it goes in.


If you're using a standard breadboard, you'll need to use wires to reach the Arduino. Run one wire (red) to the 5V socket 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! It's 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!


Exercises!


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






3434343434




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.
Exercises!


Change the code so that the LEDs alternate their blinks:


3535353535


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 the 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






3636363636




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 quiz
Modify 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




3737373737


3838383838




3939393939











Arduino Tutorial: Lesson 2: Blink blink bLiNk BLINK


Arduino Tutorial: Lesson 2: Blink blink bLiNk BLINK

This tutorial is by Limor Fried and from http://www.ladyada.net/learn/arduino


Introduction
OK you've gotten your Arduino set up and also figured out how to use the software to send sketches to the board. Next step is to start writing your own sketches. We'll start off easy by just modifying something that already works.


To start we will venture deep into the Blink sketch, looking at each line and trying to understand what its doing.


Then we will start hacking the sketch!


Blinkie
Start up the Arduino software and open the Blink example sketch, as you did in Lesson 1.


The sketch itself is in the text input area of the Arduino software. Sketches are written in text, just like a document. When you select Compile/Verify from the menu, the Arduino software looks over the document and translates it to Arduino-machine-language - which is not human-readable but is easy for the Arduino to understand.


Sketches themselves are written in C, which is a programming language that is very popular and powerful. It takes a bit of getting used to but we will go through these examples slowly.

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */


int ledPin = 13;                // LED connected to digital pin 13

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


void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}



Comments
Lets examine this sketch in detail starting with the first section:

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */
This is a comment, it is text that is not used by the Arduino, its only there to help humans like us understand whats going on. You can tell if something is a comment because there is a /* at the beginning and a */ at the end. Anything between the /* and */ is ignored by the Arduino. In this example the person who wrote the comment decided to make it look pretty and add *'s down the side but this isn't necessary.

Comments are very useful and I strongly encourage every sketch you make have a comment in the beginning with information like who wrote it, when you wrote it and what its supposed to do.


Variables
Lets look at the next line:
int ledPin = 13;                // LED connected to digital pin 13
This is the first line of actual instruction code. It is also extremely unlike English (or any other human language). The first part we can easily understand is the part to the right, which is also a comment. Turns out if you want to make a small comment, you can use // as well as /* */. // is often used for short, one line comments.


The rest of the line, the stuff before the //, is what is called a statement, which is basically like a computerized sentence. Much like human sentances end with a . (period), all computer sentences end with a ; (semicolon)


OK all we have left is the statement itself, which turns out to be a sentence telling the computer that we would like it to create a box named ledPin and to put the number 13 in that box. If you remember your math, you may recall that the box is also known as variable.




box-type
box-name
=
stuff-to-put-in-box
int
ledPin
=
13


The first part of this sentence is int, which is short for integer which is a fancy way of saying whole number. 

The second part of this sentence is ledPin which is the name of the box.


The third part is an =, which basically says that the variable (box) named ledPin should start out equaling whatever is after the =.


The fourth part is 13, a whole number (integer) which is assigned to ledPin.


Procedures
Lets move on to the next section 
void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
OK we've got two comments, each starting with //. We understand comments already so lets skip that.


We also see in the middle there is a statement, we know it's a statement because it ends with a ; (semicolon) however there's a whole bunch more stuff before and after it.


This bunch of code is an example of a procedure, a procedure is a collection of statements, its used to group statements together so that we can refer to them all with one name. Its just like a procedure that we use to perform a task step by step.




returned value
procedure name
(input values)
{ statements }
void
setup
()
pinMode(ledPin, OUTPUT); }


To better understand procedures, lets use an analogy to the kinds of procedures we're used to.
clean cat wash the cat(dirty cat)                    // a procedure for washing the cat
{
 turn on the shower.
 find the cat.
 grab the cat.
 put cat under shower.
 wait 3 minutes.                                     // wait for cat to get clean.
 release cat.
}
This is a procedure for washing the cat. The name of the procedure is wash the cat, it uses a dirty cat as the input and returns a clean cat upon success. There are two brackets, an open bracket { and a closed bracket }, that's are used to indicate the beginning and end of the procedure. Inside the procedure are a bunch of statements, indicating the correct procedure for washing a cat. If you perform all of the statements then you should be able to turn a dirty cat into a clean cat.


Looking again at the procedure, we see that it is named setup and it has no input values and it returns void. Now you're probably asking yourself "what is void?" Well thats a computer-scientist way of saying nothing. That is, this procedure doesnt return anything. (That doesnt mean it doesn't do anything, just that it doesn't have a tangible number or whatever, to show when its complete).
void setup()                    // run once, when the sketch starts 
There is one statement in this procedure,
pinMode(ledPin, OUTPUT);      // sets the digital pin as output 
We'll return to this statement in detail later, suffice to say it is a way of telling the Arduino what we would like to do with one of the physical pins on the main processor chip.


Procedure calls
We're onto the next bunch of text..
void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
Using our now well-honed technique we recognize that the text to the right is all comments. We also recognize another procedure, this one called loop which also has no inputs or output. This procedure has multiple statements, one after the other.


We're going to skip the first statement for now and go straight to statement #2.


The second and fourth statements are the same, and have something to do with a delay. This statement is very similar to the "wait 3 minutes." command in our cat-washing procedure. This statement says "Dear Arduino. Stop what you're doing for a short amount of time. Thanks!" 


To do this, the statement performs a procedure call. (We will use the phrasing calls a procedure). Basically, we want the Arduino to take a break but don't quite know how to do it, lucky for us, someone else wrote a procedure called delay which we can call upon to do the work for us. Kind of like if we need to do our taxes and we dont know how, we call upon an accountant to do it for us, giving them the paperwork input and getting tax return as the result. 


procedure name
(input values)
;
delay
(1000)
;


This means that somewhere out there, there's a procedure something like this: 
void delay(number of milliseconds)  
{
  "Dear Arduino. Stop what you're doing for (number of milliseconds) amount of time. Thanks!" 
}
(Of course, this example is not proper code)


Turns out this delay procedure works pretty well, and all we have to do is tell it how many milliseconds (1/1000th of a second) to wait and it will do the job for us.


Returning to the first statment, we see that it is also a procedure call. This time for some procedure called digitalWrite. We'll also skip this one in detail for a bit, except to explain that its turning a pin on the Arduino chip on and off, and that pin is powering the LED so in essence its turning the LED on and off.


Special Procedures - Setup() and Loop()
I do want to mention quickly here that the two procedures we've mentioned so far are extra-special in that when the Arduino first wakes up after being reset, it always does whats in the setup procedure first. Then it does whatever is in the loop procedure over and over and over...forever! Or at least until you turn it off.


Modifying the example
Now that we've analyzed the entire program it's time to make some changes. In your Arduino software, change the number in the delay procedure calls to 500 (from 1000) as shown
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
}
If you try to save the sketch, you'll get the warning that it's read-only.




Not a big deal, you can save it under a new name, such as MyBlink.




Compile/Verify the sketch and Upload it, using your Lesson 1 techniques.


   


Once the Arduino has been updated with the new sketch you should see a faster-blinking light than before


If the LED is not blinking faster, check:
  • Did you make the changes to the delay procedure call to make it 500?
  • Did the compile/verify complete successfully? (should look like the screenshot above)
  • Did the upload complete successfully? (should look like the screenshot above)

Exercises
Now it time for you to make modifications to the sketch and experiment with different delay values


Exercise 1. 
Modify the code so that the light is on for 100 msec and off for 900 msec


Exercise 2. 
Modify the code so that the light is on for 50 msec and off for 50 msec. What happens?  (Highlight the text below to see the answer)
Intense strobe action!


Exercise 3. 
Modify the code so that the light is on for 10 ms and off for 10 ms. What happens? (Highlight the text below to see the answer)
The light is no longer blinking


Now pick up the Arduino and gently wave it back and forth, in a dark room. What happens? (Highlight the text below to see the answer)
The LED creates a dashed trail of light in the air. z


What do you think is happening here? (Highlight the text below to see the answer)
The LED is blinking, but its blinking so fast that our eyes can't pick it up, so it looks like a blur. When the Arduino is waved in the air, we see streaks of light from the blinks.


Wrap up
Congrats, you have finished the second lesson!


In this lesson you examined the Blink sketch in detail, and learned a bit of the language used to talk to the Arduino. You also learned how to modify a sketch, and practiced uploading sketches to the Arduino some more.