Monday, October 24, 2011

8 More LEDs (CIRC-05)

PURPOSE:

The purpose of this Arduino circuit experiment is to make 8 LEDS blink in series using the MM74HC595 high speed Shift Register.

EQUIPMENT:

·         1 x Shift Resister 74HC595
·         8 x Red LED
·         8 x 330 Ohm Resistor(Orange-orange-Brown)
·         Wires (any color)
·         1 x CIRC-05 Breadboard Sheet
·         1 x Breadboard
·         1 x Arduino Uno
·         1 x Arduino Holder

PROGRAM DETAILS:

This circuit lab is similar to the 8 LED Fun (CIRC-02) since it involves working with 8 LEDs. However, in this lab we use the Shift Resister 74HC595 as the main component to make our 8 LEDs blink. The Shift Resister 74HC595 is also known as IC (Integrated Circuit) which packages any range of complicated electronics inside an easy to use package. The amazing things this unique device has are low-power consumption, loud noise immunity and ability to drive 15LS-TTL loads. ICs or Shift Resister has a very deceptive appearance as it looks very similar to the ATMega chip on the Arduino board. In reality, the ATMega chip and the Shift Resisters are very different in function ways and price ways.

The Shift Resister is a serial to parallel converter which gives you an additional 8 outputs to control LEDs only by using 3 Arduino pins. If linked together properly, they can also give you many more outputs using the same four pins. To use it you “clock in” the data and then you lock it in (latch it). If you want to do this then you have to set the data to HIGH/LOW, pulse the clock, then set the data pin again and pulse the clock again and again until you have shifted out 8 bits of data. Lastly, you need to pulse the latch so that the 8 bits are transferred to the shift resisters pins.

The programming section of this circuit experiment introduced how to assigning values to HIGH/LOW (HIGH = 1; LOW = 0). The new method that got introduced in this lab as well is the ShiftOut(pin, pin, MSBFIRST, int). This method helps to send Arduino the 8 bits data which it needs to display. The creating the circuit part was fairly easy and simple, all we did was attaching the parts listed with the help of the CIRC-05 Breadboard Sheet.

TIME TO COMPLETE:

It took 25 minutes to build the circuit and 10 minutes to program it.

RESULTS:

At first, the Arduino’s power LED went out because we accidentally inserted the chip backwards but then we fixed it quickly and the circuit blinked as it was supposed to.

PHOTOS OF PROJECT:


8 LEDs Blinking Using the 74HC595 Shift Register! ISN'T IT AMAZING???

TIPS:

The tips I would give is that make sure the metal leads of the resistors don’t come in contact with the metal leads of the LEDs and watch out for proper orientation (look for marks showing pin 1).

FURTHER WORK:

To take it to the next level, I would like to control the individual LEDs. This can be done by replacing the loop() code with the following lines of code:

int delayTime = 100;
for(int i = 0; i < 8; i++)
{
    changeLED(i, ON);
    delay(delayTime);
}
for(int i = 0; i < 8; i++)
{
    changeLED(i , OFF);
    delay(delayTime);
}

PROGRAM MODIFICATIONS:

The program I have been following is the very same program as the one from the Sparkfun Inventor’s Guide (Page. 16 and 17). However, when I was typing my code out, I took out the comments to save myself some time and, I also increased the delay time to delayTime = 500 so that it’s easier to observe the individual LED blink.

PROGRAM (with comments):

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(i);
   delay(delayTime);
  }
}
/*
* 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
  delay(1);
  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 http://en.wikipedia.org/wiki/Bitwise_operation
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
}

REFERENCE:





No comments:

Post a Comment