LED Lab details

Objective:  make 4 LEDs blink on and off in a programmed sequence

Parts List

LED (any color) 4
Resistor: 330ohm 4
Wire (red) 1
Wire (black) 1
Wire (Any color) 4
Arduino Uno* 1
Breadboard* 1
USB cable* 1

* you will need these parts for all labs in this website

Parts Set Up

Instructional Parts Set-Up

Part Connection 1 Connection 2
LED 1 C2 (longer wire) C3
Resistor 1 A3 -ve column
Wire 1 E2 Pin 2
LED 2 C6 (longer wire) C7
Resistor 2 A7 -ve column
Wire 2 E6 Arduino Digital Pin 3
LED 3 C10 (longer wire) C11
Resistor 3 A11 -ve column
Wire 3 E10 Arduino Digital Pin 4
LED 4 C14 (longer wire) C15
Resistor 4 A15 -ve column
Wire 4 E14 Arduino Digital Pin 5
Wire (black) Any -ve column Arduino GND
Wire (red) Any +ve column Arduino 5V

 Visual Parts Set-Up

Lab 2 Code 

In this lesson you will be shown a long way and then a short way of creating this code.  This is so that the short way's method is more obvious.  If you understand the difference, skip down to the short way.

Please TYPE this code in.  Copying and pasting this code is prevented because too many students would copy and paste and not learn how to type any code.

If you type code, there's a chance you might make a mistake.  By fixing that mistake, you will become a better programmer and you will need to look up the code less.

Without Comments

With Comments

Line Code         Comments
1 int ledPin1=2;  Creating a variable named ledPin1 to represent the number 2
2 int ledPin2=3; Creating a variable named ledPin2 to represent the number 3
3 int ledPin3=4; Creating a variable named ledPin3 to represent the number 4
4 int ledPin4=5; Creating a variable named ledPin4 to represent the number 5
    The numbers match the pin numbers we want to use.
5 void setup() This creates our setup() function which defines what the pins will do
6  { Many Arduino programs have this attached to the line above but it is separate here for emphasis
pinMode(ledPin1,OUTPUT); This declares the value of ledPin1 to be an OUTPUT.   (it makes Pin 2 an OUTPUT) 
8 pinMode(ledPin2,OUTPUT);  Same as line 7 but making Pin 3 an OUTPUT
pinMode(ledPin3,OUTPUT);  Same as line 7 but making Pin 4 an OUTPUT 
10  pinMode(ledPin4,OUTPUT);  Same as line 7 but making Pin 5 an OUTPUT 
11   } Closing out setup() function.  Every { must have a } to match!
12   void loop() This is where we put the program we want to loop forever 
13   { This indicates the start of void loop() 
14   int delayTime = 100;  This creates a variable called delayTime and assigns it a value of 100.This line could be put at the top of the program with the other variables. 
15   digitalWrite(ledPin1, HIGH); ledPin1 is given maximum power (HIGH = 255)
16   delay (delayTime); The program waits 100 milliseconds (the value of delayTime)
17   digitalWrite(ledPin2, HIGH); ledPin2 is given full power
18   delay (delayTime); 100 millisecond (ms) wait 
19   digitalWrite(ledPin3, HIGH); ledPin3 is given full power 
20   delay (delayTime); 100ms wait 
21  digitalWrite(ledPin4, HIGH); ledPin4 is given full power.  Note that all 4 LEDs are on now. 
22  delay (delayTime); 100ms wait 
23   This is just a space to allow you to see the on and off sections of programming more clearly 
24 delay (delayTime); 100ms wait.  This is here to allow a bit longer of a wait before LEDs start going off
25 digitalWrite(ledPin4, LOW); ledPin4 goes off (LOW = 0) 
26  delay (delayTime); 100ms wait 
27 digitalWrite(ledPin3,LOW); ledPin3 goes off 
28 delay (delayTime); 100ms wait 
29 digitalWrite(ledPin2,LOW); ledPin2 goes off 
30 delay (delayTime); 100ms wait 
31 digitalWrite(ledPin1,LOW); ledPin1 goes off 
32 delay (delayTime); 100ms wait 
33 } This bracket closes the void loop() 

 Visual Explanation

This program uses:

pinMode(pin #, OUTPUT); //this is to tell the Arduino what the pin is going to be used for (input or output: output in this case means to turn power on).

digitalWrite (pin #, HIGH or LOW); //this is to turn on or off a specific pin/LED

delay (time in 1/1000sec); ///this is to make the program wait a specific amount of time before moving to the next line


Why Use Variables 

More Efficient Program

More Efficient Version Explanation