Instructables

Arduino Tutorial Bundle .:Arduino Experimentation Kit:. (ARDX)

Featured

Step 4: .:8 LED Fun:. (Multiple LEDs) - CIRC02

Picture of .:8 LED Fun:. (Multiple LEDs) - CIRC02
CIRC02-assembled.jpg
CIRC02-pieces.jpg
CIRC02-3d.png
CIRC02-3dexploded.png
CIRC02-scem.png
CIRC02-sheet.png

What We're Doing:
We have caused one LED to blink, now its time to up the stakes. Lets connect eight. We'll also have an opportunity to stretch the Arduino a bit by creating various lighting sequences. This circuit is also a nice setup to experiment with writing your own programs and getting a feel for how the Arduino works.

Along with controlling the LEDs we start looking into a few simple programming methods to keep your programs small.

for() loops - used when you want to run a piece of code several times.
arrays[] - used to make managing variables easier (its a group of variables)

(you can also download the breadboard layout sheet from the bottom of this step)



The Parts:
  • CIRC-02 Breadboard sheet
  • 2 Pin Header (x4)
  • 5mm Green LED (x8)
  • 560 ohm Resistor (green-blue-brown) (x8)


The Circuit and Plugging Everything In:
A Small Video of Everything Being Plugged in


The Code:

/*     --------------------------------------------------------- *     |  Arduino Experimentation Kit Example Code             | *     |  CIRC-02 .: 8 LED Fun :. (Multiple LEDs)   | *     --------------------------------------------------------- *   *  A few Simple LED animations * * For more information on this circuit http://tinyurl.com/d2hrud * *///LED Pin Variablesint ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the pin each LED is connected to                                   //i.e. LED #0 is connected to pin 2, LED #1, 3 and so on                                   //to address an array use ledPins[0] this would equal 2                                   //and ledPins[7] would equal 9/* * setup() - this function runs once when you turn your Arduino on * We the three control pins to outputs */void setup(){    //Set each pin connected to an LED to output mode (pulling high (on) or low (off)  for(int i = 0; i < 8; i++){         //this is a loop and will repeat eight times      pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output  }                                   //the code this replaces is below   /* (commented code will not run)   * these are the lines replaced by the for loop above they do exactly the   * same thing the one above just uses less typing  pinMode(ledPins[0],OUTPUT);  pinMode(ledPins[1],OUTPUT);  pinMode(ledPins[2],OUTPUT);  pinMode(ledPins[3],OUTPUT);  pinMode(ledPins[4],OUTPUT);  pinMode(ledPins[5],OUTPUT);  pinMode(ledPins[6],OUTPUT);  pinMode(ledPins[7],OUTPUT);  (end of commented code)*/}/* * loop() - this function will start after setup finishes and then repeat * we call a function called oneAfterAnother(). if you would like a different behaviour * uncomment (delete the two slashes) one of the other lines */void loop()                     // run over and over again{  oneAfterAnotherNoLoop();   //this will turn on each LED one by one then turn each off  //oneAfterAnotherLoop();   //does the same as oneAfterAnotherNoLoop but with much less typing  //oneOnAtATime();          //this will turn one LED on then turn the next one on turning the                              //former off (one LED will look like it is scrolling along the line  //inAndOut();              //lights the two middle LEDs then moves them out then back in again}/* * oneAfterAnotherNoLoop() - Will light one LED then delay for delayTime then light * the next LED until all LEDs are on it will then turn them off one after another * * this does it without using a loop which makes for a lot of typing.  * oneOnAtATimeLoop() does exactly the same thing with less typing */void oneAfterAnotherNoLoop(){  int delayTime = 100; //the time (in milliseconds) to pause between LEDs                       //make smaller for quicker switching and larger for slower  digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (connected to pin 2)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (connected to pin 3)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (connected to pin 4)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (connected to pin 5)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (connected to pin 6)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[5], HIGH);  //Turns on LED #5 (connected to pin 7)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[6], HIGH);  //Turns on LED #6 (connected to pin 8)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[7], HIGH);  //Turns on LED #7 (connected to pin 9)  delay(delayTime);                //waits delayTime milliseconds  //Turns Each LED Off  digitalWrite(ledPins[7], LOW);  //Turns on LED #0 (connected to pin 2)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[6], LOW);  //Turns on LED #1 (connected to pin 3)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[5], LOW);  //Turns on LED #2 (connected to pin 4)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[4], LOW);  //Turns on LED #3 (connected to pin 5)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[3], LOW);  //Turns on LED #4 (connected to pin 6)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[2], LOW);  //Turns on LED #5 (connected to pin 7)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[1], LOW);  //Turns on LED #6 (connected to pin 8)  delay(delayTime);                //waits delayTime milliseconds  digitalWrite(ledPins[0], LOW);  //Turns on LED #7 (connected to pin 9)  delay(delayTime);                //waits delayTime milliseconds  }/* * oneAfterAnotherLoop() - Will light one LED then delay for delayTime then light * the next LED until all LEDs are on it will then turn them off one after another * * this does it using a loop which makes for a lot less typing.  * than oneOnAtATimeNoLoop() does exactly the same thing with less typing */void oneAfterAnotherLoop(){  int delayTime = 100; //the time (in milliseconds) to pause between LEDs                       //make smaller for quicker switching and larger for slower//Turn Each LED on one after another  for(int i = 0; i <= 7; i++){    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i    delay(delayTime);                //gets one added to it so this will repeat   }                                  //8 times the first time i will = 0 the final                                     //time i will equal 7;//Turn Each LED off one after another  for(int i = 7; i >= 0; i--){  //same as above but rather than starting at 0 and counting up                                //we start at seven and count down    digitalWrite(ledPins[i], LOW);  //Turns off LED #i each time this runs i    delay(delayTime);                //gets one subtracted from it so this will repeat   }                                  //8 times the first time i will = 7 the final                                     //time i will equal 0;                                                                          }/* * oneOnAtATime() - Will light one LED then the next turning off all the others */void oneOnAtATime(){  int delayTime = 100; //the time (in milliseconds) to pause between LEDs                       //make smaller for quicker switching and larger for slower    for(int i = 0; i <= 7; i++){    int offLED = i - 1;  //Calculate which LED was turned on last time through    if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will      offLED = 7;        //turn on LED 2 and off LED 1)    }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)                         //instead we turn off LED 7, (looping around)    digitalWrite(ledPins[i], HIGH);     //turn on LED #i    digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time    delay(delayTime);  }}/* * inAndOut() - This will turn on the two middle LEDs then the next two out * making an in and out look */void inAndOut(){  int delayTime = 100; //the time (in milliseconds) to pause between LEDs                       //make smaller for quicker switching and larger for slower    //runs the LEDs out from the middle  for(int i = 0; i <= 3; i++){    int offLED = i - 1;  //Calculate which LED was turned on last time through    if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will      offLED = 3;        //turn on LED 2 and off LED 1)    }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)                         //instead we turn off LED 7, (looping around)    int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED #0 when i = 3     int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED #7 when i = 3     int offLED1 = 3 - offLED; //turns off the LED we turned on last time    int offLED2 = 4 + offLED; //turns off the LED we turned on last time        digitalWrite(ledPins[onLED1], HIGH);    digitalWrite(ledPins[onLED2], HIGH);        digitalWrite(ledPins[offLED1], LOW);        digitalWrite(ledPins[offLED2], LOW);            delay(delayTime);  }  //runs the LEDs into the middle  for(int i = 3; i >= 0; i--){    int offLED = i + 1;  //Calculate which LED was turned on last time through    if(i == 3) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will      offLED = 0;        //turn on LED 2 and off LED 1)    }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)                         //instead we turn off LED 7, (looping around)    int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED #0 when i = 3     int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED #7 when i = 3     int offLED1 = 3 - offLED; //turns off the LED we turned on last time    int offLED2 = 4 + offLED; //turns off the LED we turned on last time        digitalWrite(ledPins[onLED1], HIGH);    digitalWrite(ledPins[onLED2], HIGH);        digitalWrite(ledPins[offLED1], LOW);        digitalWrite(ledPins[offLED2], LOW);            delay(delayTime);  }}  

Not Working?
  • Some LEDs Fail to Light - It is easy to insert an LED backwards. Check the LEDs that aren't working and ensure they the right way around.
  • Operating out of sequence - With eight wires it's easy to cross a couple. Double check that the first LED is plugged into pin 2 and each pin there after.
  • Starting Afresh - Its easy to accidentally misplace a wire without noticing. Pulling everything out and starting with a fresh slate is often easier than trying to track down the problem.


Making it Better:
Switching to Loops:
in the loop() function there are 4 lines. The last three all start with a '//' this means the line is treated as a comment (not run). To switch the program to use loops change the void loop() code to:
  //oneAfterAnotherNoLoop();     oneAfterAnotherLoop();     //oneOnAtATime();            //inAndOut();
Upload the program, and notice that nothing has changed. You can take a look at the two functions, each does the same thing, but use different approaches (hint the second one uses a for loop)

Extra Animations:
Tired of this animation? Then try the other two sample animations. Uncomment their lines and upload the program to your board and enjoy the new light animations. (delete the slashes in front of row 3 and then 4)

Testing out your own Animations:
Jump into the included code and start changing things. The main point is to turn an LED on use digitalWrite(pinNumber, HIGH); then to turn it off use digitalWrite(pinNumber, LOW); . Type away, regardless of what you change you won't break anything.


CIRC02-sheet.pdf(630x810) 46 KB
 
Remove these adsRemove these ads by Signing Up
JohnH19 made it!10 days ago

Hey - fun project! I had no problems getting it to work. then I hacked it a bit to count to 1024 (2 extra LEDs). I'm interested in using this to count passers-by. I have a second IR beam-breaking detector that provides a ground when the beam is interrupted. I would like to feed this ground signal into D13 so that when grounded, the binary count would increment by one. Can anyone recommend how to tweak the code to look for a ground on D13 thereby incrementing the count by one?

Thanks!! Mø

Mo_BinaryLED.MOV(516x290) 10 KB
Evyllity3 months ago

I don't understand why for(int i = 0; i < 8 ; i++) even works. if i is set to 0, then i is less than 8, right? for(int i = 0; i =7 ; i++) does nothing, and for(int i = 0; i == 7 ; i++) works correctly except the LEDs are lit at ~10%. What's happening here?

I don't understand the purpose of the wire running to 5v. It connects the positive rail on the breadboard to 5v but I don't see anything on the breadboard connected to the positive rail. Here's my new-to-Arduino question: obviously all of the LEDs ground through the resistors=>negative rail=> GRND on Arduino. Do they get all the power they need (+) from the pins 2~9 being controlled by the code? Right now I'm running it completely without the wire to 5V and it runs but LEDs are dim.

You are correct, you technically do not need the 5v wire from the ardunio to the positive side of the breadboard, the positive voltage comes from the pins (2-9) all the code actually does is turns the voltage on or off through the pins, thus the circuit completes through the negative rail on the breadboard, and lights the LED.

rasmino10 months ago

can i copy and paste the code or no

JDalman3 years ago
ON the in and out code I found an easier way. I reloaded the array so the order is the same order the LEDs are to be lit, then I used loops to kight them from the middle out and unlight them from the outside in. I then changed it around a little to sequence out and then in.

Here's the array:
int ledPins[] = {5, 6, 4, 7, 3, 8, 2, 9};

Here's the loop:
//LEDs light inside to outside
for(int i = 0; i <= 7; i++){
int j = i + 1;
digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs
digitalWrite(ledPins[j], HIGH); //Turns on LED #j each time this runs
delay(delayTime); //gets one added to it so this will repeat
digitalWrite(ledPins[i], LOW); //Turns off LED #i each time this runs
digitalWrite(ledPins[j], LOW); //Turns off LED #j each time this runs
} //8 times the first time i will = 0 the final
//time i will equal 7;

//LEDs light outside to inside
for(int i = 4; i >= 0; i--){ //same as above but rather than starting at 0 and counting up
int j = i + 1;
//we start at seven and count down
digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs
digitalWrite(ledPins[j], HIGH); //Turns on LED #i each time this runs
delay(delayTime); //gets one added to it so this will repeat
digitalWrite(ledPins[i], LOW);
digitalWrite(ledPins[j], LOW);
dadoftwins5 years ago
Where is the code for this project? Can't find it anywhere.
The code is just below the video and goes all the way to the right and i mean ALL the way
cefn4 years ago
The code for circuit 02 is at http://tinyurl.com/dkpxbn