1. Blinking a LED

Introduction
LEDs are small, powerful lights that are used in many different applications. To start off, we will work on blinking an LED, the Hello World of microcontrollers. That’s right – it’s as simple as turning a light on and off. It might not seem like much, but establishing this important baseline will give you a solid foundation as we work toward more complex experiments.

Parts Needed
You will need the following parts:
1x Breadboard
1x Arduino
1x LED
1x 330Ω Resistor
3x Jumper Wires

Introducing the LED

alt text

Light-Emitting Diode (LED) will only let current through it in one direction. Think of an LED as a one-way street. When current flows through the LED, it lights up! When you are looking at the LED, you will notice that its legs are different lengths. The long leg, the “anode,” is where current enters the LED. This pin should always be connected to the current source. The shorter leg, the “cathode,” is the current’s exit. The short leg should always be connected to a pathway to ground. LEDs are finicky when it comes to how much current you apply to them. Too much current can lead to a burnt-out LED. To restrict the amount of current that passes through the LED, we use a resistor in line with the power source and the LED’s long leg; this is called a current-limiting resistor. With the Arduino, you should use a 330 Ohm resistor. We have included a baggy of them in the kit just for this reason!

Hardware Hookup
Ready to start hooking everything up? Check out the wiring diagram and hookup table below to see how everything is connected.

Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction. Polarized components are highlighted with a yellow warning triangle in the table below.

Please note: Pay close attention to the LED. The negative side of the LED is the short leg, marked with a flat edge.

LED drawing

Components like resistors need to have their legs bent into 90° angles in order to correctly fit the breadboard sockets. You can also cut the legs shorter to make them easier to work with on the breadboard.

Bent resistor

Wiring Diagram for the Experiment

1blink
Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

Open Your First Sketch
Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Copy and Paste the following code into the Arduino IDE. Hit upload and see what happens!

/*
GCD BA Unit 2 - Starters Kit
Orriginal code by Spark Fun Electronics
Example sketch 01

BLINKING AN LED

Turn an LED on for one second, off for one second,
and repeat forever.

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about Arduino.
*/



//The setup function runs once upon your Arduino being powered or once upload is          //complete.

void setup()
{
  //set pin 13 to OUTPUT 
  pinMode(13, OUTPUT);
}

//The loop function runs from the top down and repeats itself until you upload new        //code or power down your Arduino
void loop()
{
  //Turn pin 13 HIGH (ON). 
  digitalWrite(13, HIGH);   

  //wait 1000 milliseconds (1 second)
  delay(1000);              

  //Turn pin 13, LOW (OFF)
  digitalWrite(13, LOW);    

  //wait 1000 milliseconds
  delay(1000);              
}

Code to Note

pinMode(13, OUTPUT); Before you can use one of the Arduino’s pins, you need to tell the board whether it is an INPUT or OUTPUT. We use a built-in “function” called pinMode() to do this. digitalWrite(13, HIGH); When you’re using a pin as an OUTPUT, you can command it to be HIGH (output 3.3 volts), or LOW (output 0 volts).

Now, lets have a closer look at the delay(1000); function. Pin 13 is set to HIGH (on) followed by a delay of 1000 milliseconds. Meaning the LED light stays on for 1 second. After this delay Pin 13 is set to LOW (OFF), followed by another delay of 1000 milliseconds. At the end of this delay the code is completed and will continue in a loop from the top (set pin 13 to HIGH).
Start playing with the code. Change the delay value, copy and paste a few lines and explore how you can control the LED into different patterns. Hit upload to see the changes you made happen.

What You Should See
You should see your LED blink on and off. If it doesn’t, make sure you have assembled the circuit correctly and verified and uploaded the code to your board, or see the Troubleshooting section.

alt text

Troubleshooting
Program Not Uploading? This happens sometimes; the most likely cause is a confused serial port. You can change this in Tools > Serial Port > Also, if you get a Timeout error or the IDE could not find your arduino board, try pressing the Master Reset button on the arduino board, wait around 10 seconds and try re-uploading your sketch.

2. Reading a Potentiometer