Wednesday, October 19, 2011

CIRC-02: Multiple LEDs

PURPOSE:                                                                                                                             Multiple LED Tutorial Video
To make 8 LEDs blink using 330 Ohm Resistor.

·    CIRC-02 Breadboard Sheet
·    5mm Yellow LED x 8
·    330 Ohm Resistor (Orange-Orange-Brown) x 8
·    Wires (any colors)

Link 1
Link 2


In the first circuit, we caused one LED to blink but in this circuit, we played around with 8 LEDs and had an opportunity to extend our Arduino knowledge a bit by making a range of lighting sequences. This circuit also gave us an opportunity to experiment with coding our own programs and getting a feel for how Arduino works. In addition to controlling the LEDs, we started investigating a few simple programming methods to keep our programs small and efficient. For example, we used the for() loops to run a piece of code several times and we used arrays[] to hold and manage variables easily.

Me and my partner, Varshini started off by gathering the parts we need for our Arduino. We attached the LEDs, resistors and wires respectively on the breadboard. We made sure the longer side of the LED goes to the positive side (the (+) and (-) are shown in the breadboard. We were being very careful with the resistors due to their fragileness. We chose different colored wires so that we don’t confuse ourselves and so that it looks better.


Approximately 45mins


When creating the circuit was completed, we analyzed the codes given in the Sparkfun Inventor’s Guide and we took an attempt to write our own code. We switched the serial port to the correct one and uploaded the program to see all the 8 LEDs blink. However, an unknown problem occurred and only 5 LEDs were blinking ( 1st, 4th and 6th LEDs weren’t blinking). We double checked our code and tried the 2 things out of the 3things the Sparkfun Guide tells you to do in cases when things don’t work out. The 2 things were: Checking the LEDs that weren’t working to ensure that they are the right way around and, double checked that the first LED is plugged into pin 2 and each pin thereafter. The 3rd thing was pulling everything out and starting with a fresh slate which we didn’t do because we didn’t want to start over again. Although, we tried these different things to solve our problem, we were still unsuccessful.

1st, 4th and 6th LEDs are not blinking!

Then Mr. Kee came in, checked our circuit and realized that the 1st, 4th and 6th LEDs weren’t blinking because their resistors were touching the LED’s side (when metal touches another metal, it doesn’t work). Finally, our problem was solved and all the 8 LEDs blinked.

CIRC-02 Breadboard Sheet
and Schematic Diagram
Fritzing Diagram

One tip I can give everyone is that you should always gather the parts you need to build your circuit before building it. This will make you more organized and it will reduce the chances of losing any parts. Another tip I would give is that if things don’t work out, do NOT panic! Try to do the things from the “Not Working?” section in the Sparkfun Guide or ask your peers/Mr.Kee for help.


The animation for this circuit is not so interesting and so, I would like to try the other two sample animations. To do this, I will have to uncomment their lines and upload the program to my board and enjoy the new light animations. I would also like to jump into the included code and start changing things to test out my own animations by turning on/off LEDs in different sequences/patterns. I can do this easily by using digitalWrite(pinNumber, HIGH) and digitalWrite(pinNumber,LOW) since the main point is to turn an LED on and off.


The program I was following is the same as the one found in this link.

PROGRAM (with comments):

/*     ———————————————————
*     |  Arduino Experimentation Kit Example Code             |
*     |  CIRC-05 .: 8 More LEDs :. (74HC595 Shift Register)   |
*     ———————————————————
* We have already controlled 8 LEDs however this does it in a slightly
* different manner. Rather than using 8 pins we will use just three
* and an additional chip.

//Pin Definitions
//The 74HC595 using a protocol called SPI (for more details
//Which has three pins
int data = 2;
int clock = 3;
int latch = 4;
//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
* setup() – this function runs once when you turn your Arduino on
* We set the three control pins to outputs
void setup()
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
* loop() – this function will start after setup finishes and then repeat
* we set which LEDs we want on then call a routine which sends the states to the 74HC595
void loop()                     // run over and over again
  int delayTime = 100; //the number of milliseconds to delay between LED updates
  for(int i = 0; i < 256; i++){
* updateLEDs() - sends the LED states set in ledStates to the 74HC595
* sequence
void updateLEDs(int value){
  digitalWrite(latch, LOW);     //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
* updateLEDsLong() - sends the LED states set in ledStates to the 74HC595
* sequence. Same as updateLEDs except the shifting out is done in software
* so you can see what is happening.
void updateLEDsLong(int value){
  digitalWrite(latch, LOW);    //Pulls the chips latch low
  for(int i = 0; i < 8; i++){  //Will repeat 8 times (once for each bit)
  int bit = value & B10000000; //We use a "bitmask" to select only the eighth
                               //bit in our number (the one we are addressing this time through
  value = value << 1;          //we move our number up one bit value so next time bit 7 will be
                               //bit 8 and we will do our math on it
  if(bit == 128){digitalWrite(data, HIGH);} //if bit 8 is set then set our data pin high
  else{digitalWrite(data, LOW);}            //if bit 8 is unset then set the data pin low
  digitalWrite(clock, HIGH);                //the next three lines pulse the clock pin
  digitalWrite(clock, LOW);
  digitalWrite(latch, HIGH);  //pulls the latch high shifting our data into being displayed
//These are used in the bitwise math that we use to change individual LEDs
//For more details
int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
* changeLED(int led, int state) - changes an individual LED
* LEDs are 0 to 7 and state is either 0 - OFF or 1 - ON
void changeLED(int led, int state){
   ledState = ledState & masks[led];  //clears ledState of the bit we are addressing
   if(state == ON){ledState = ledState | bits[led];} //if the bit is on we will add it to ledState
   updateLEDs(ledState);              //send the new LED state to the shift register

No comments:

Post a Comment