- Thinkabit Lab

Fun With Multiple LEDs
Learn to connect and write code to control multiple LEDs.
If you thought playing with 1 LED was fun, just wait till you get to play with multiple LEDs! You may need
to partner up with a friend to borrow more LEDs. This lesson will walk you through how to wire 4 LEDs
and write code to make them behave independently.
Note: You should have completed the LED Blink activity before starting this activity. You may find the
“Breadboards” and “The Arduino” information sheets useful.
 Arduino
 Breadboard
 1 black wire
 4 LEDs
 4 red wires
 4 resistors (100 – 300 ohms)
All circuit images created with Fritzing
1. Start by connecting the black wire from GND 2. Next, start placing LEDs. Start with one LED and
to the negative rail of the breadboard. This
place the short leg in the negative rail and the
makes the entire column negative.
long leg in 5a. (Note: The long leg of the LED can
go in any inside row as long as the
corresponding resistor in step 6 is moved
accordingly.)
3. With the next LED, place the short leg in the 4. Next, place another LED with the short leg in the
negative rail and the long leg in 10a.
negative rail and the long in 15a.
All circuit images created with Fritzing
5. Last LED, place the short leg in the negative 6. Now place the resistors. Start by connecting one
rail and the long leg in 20a.
resistor from row 5 across the center aisle to the
other row 5. (The resistor must be in the same
row as the LED, so if you connected the LED to
a different row than row 5, put the resistor in
the same row as that LED).
7. Connect the next resistor in row 10 across 8. Connect the next resistor the same way in row
the center aisle to the other row 10.
15.
All circuit images created with Fritzing
9. Connect the last resistor the same way in 10. Now place the red wires. These will all connect
row 20.
to different pins on the Arduino board so that
we can control each LED individually. Start by
connecting a red wire from pin 8 on the Arduino
to the far side of row 5 where only the resistor
is (not where the LED and resistor are).
11. Connect a red wire from pin 9 to the far side 12. Connect a red wire from pin 10 to the far side of
of row 10.
row 15.
All circuit images created with Fritzing
13. Lastly, connect your final red wire from pin 11 to the far side of row 20.
All circuit images created with Fritzing
1. Start by connecting the Arduino to the computer via the USB cable. Open a new sketch and delete
all of the text. Also, make sure that you are connected to the right port.
2. You usually declare variables at the start of a program, but there are none to declare for this program,
so move straight into setup. There are 4 LEDs to set up as outputs and they are connected to pins 8,
9, 10, and 11, so the setup should look like this:
All circuit images created with Fritzing
3. Next, move onto your loop. The following code will cause all of the LEDs to turn on at the same time,
stay on for one second, and then turn off at the same time and stay off for one second.
4. Upload your code and your LEDs should all blink together.
All circuit images created with Fritzing
5. As cool as that may look, it gets even better! Now you get to make the LEDs blink in a row. This
means that the LED connected to pin 8 will turn on and then when it turns off, the LED connected to
pin 9 will turn on and so forth. The code shown below uses a delay of 500, or half a second, but you
could use a value that is larger or smaller if you prefer. Don’t forget to upload your code!
Note: The first line of the loop tells the LED in pin 11 to turn off. While this doesn’t make much sense
at first (since it wasn’t on to begin with) if you look at the end of the loop, you will notice that the
LED attached to pin 11 is HIGH, or on. If you didn’t include the first line of the loop that turns off pin
11, the LED in pin 11 would stay on after it ran through the loop once.
All circuit images created with Fritzing
 If you are having problems uploading, use the “Troubleshooting” document to help. Common errors
that cause upload errors include forgetting the semicolon at the end of each line or spelling
something wrong.
 Check that you are connected to the right port by going to Tools, and then Port. This was done in
Step 1 of writing the code, but if you saved your code and tried to upload another day it may not be
connected.
 If your code uploaded to your Arduino but your LEDs are not behaving, check the wiring. It may be
easiest to pull out everything attached to your breadboard and start over. LEDs can burn out so you
may need to test your LEDs.
See if you can write code to solve the following challenges. If you get stuck, you can look at the
Challenge Solutions on the following pages for help.
1. Try to make the LEDs blink in sequence like they did with the code from step 18 (the next LED turns
on when the current LED turns off) but this time blink down and then back. (8, 9, 10, 11, 10, 9, repeat)
2. Try to make the LEDs turn on, and stay on, as each additional LED is turned on and once all four LEDs
are on, all of the LEDs turn off and the pattern starts over. (1 LED on, 2 LEDs on, 3 LEDs on, 4 LEDs
on, 0 LEDs on, repeat)
3. Using 3 LEDs (one green, one yellow, one red), try to make a stop light such that the green LED stays
on for a long amount of time, then the yellow LED turns on for a very short amount of time, and then
the red LED turns on for a long amount of time. Try to make it realistic like a traffic light.
4. Try to program your LEDs to blink in time with a song. You may want to start with something simple
like “Twinkle, Twinkle Little Star.”
5. Try to make the LEDs fade on in order and then off in order. You will need to have completed the
“LED Fade” activity to complete this challenge. Remember that “LED Fade” uses variables that need
to be declared before your code’s setup. Since you have four LEDs instead of one, you will need four
“brightness” variables like: brightness1, brightness2, brightness3, and brightness4. You will also
need to change the wire from pin 8 to pin 6 in both the physical wiring and the code so that the LED
can get an analog signal required for a fade.
All circuit images created with Fritzing
Note: The setup portion of the code is not included in the solutions if it is the same as step 15.
Challenge 1 Solution:
void loop() {
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
delay(500);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(500);
}
Challenge 2 Solution:
void loop() {
digitalWrite(8, HIGH);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(11, HIGH);
delay(500);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
delay(500);
}
Challenge 3 Solution:
void setup() {
pinMode(8, OUTPUT); //this will be green
pinMode(9, OUTPUT); //this will be yellow
pinMode(10, OUTPUT); //this will be red
}
All circuit images created with Fritzing
void loop() {
digitalWrite(10, LOW);
digitalWrite(8, HIGH);
delay(4000);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(5000);
}
Challenge 4 Solution:
This code will make 4 LEDs blink to the tune of “Twinkle, Twinkle Little Star.”
void loop() {
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
delay(500);
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
delay(500);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
delay(500);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(500);
}
All circuit images created with Fritzing
Challenge 5 Solution:
int brightness6 = 0;
int brightness9 = 0;
int brightness10 = 0;
int brightness11 = 0;
void setup() {
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
for(brightness6 = 0; brightness6 <= 255; brightness6 +=
analogWrite(6, brightness6);
delay(30);
}
for(brightness9 = 0; brightness9 <= 255; brightness9 +=
analogWrite(9, brightness9);
delay(30);
}
for(brightness10 = 0; brightness10 <= 255; brightness10
analogWrite(10, brightness10);
delay(30);
}
for(brightness11 = 0; brightness11 <= 255; brightness11
analogWrite(11, brightness11);
delay(30);
}
for(brightness11 = 255; brightness11 >= 0; brightness11
analogWrite(11, brightness11);
delay(30);
}
for(brightness10 = 255; brightness10 >= 0; brightness10
analogWrite(10, brightness10);
delay(30);
}
for(brightness9 = 255; brightness9 >= 0; brightness9 -=
analogWrite(9, brightness9);
delay(30);
}
for(brightness6 = 255; brightness6 >= 0; brightness6 -=
analogWrite(6, brightness6);
delay(30);
}
}
5){
5){
+= 5){
+= 5){
-= 5){
-= 5){
5){
5){
All circuit images created with Fritzing
Challenge 5 Explanation:
The code makes use of multiple “for” statements. A “for” statement is like a mini loop, in that once
a “for” statement is entered, the code within that “for” statement will loop until the “for” conditions
are satisfied. For example: the first “for” statement in the code above says that the LED attached to
pin 6 will change its brightness by a value of 5 every 30 milliseconds so long as its brightness is less
than or equal to 255. By the end, the entire code will make the first LED will fade on and stay on,
then the second LED will fade on and stay on, then the third LED will fade on and stay on, then the
fourth LED will fade on and then fade off, then the third LED will fade off, then the second will fade
off, then the first will fade off and then the whole thing will repeat.
All circuit images created with Fritzing