Feedback!

LED Bar Graph + 74HC595 Shift Register

Views: 8179 Difficulty: 1 Status: Development
Ledbargraph_74hc595_main

The 74HC595 shift register controls a 10 LED Bar Graph.

Control a 10 LED Bar Graph with a 74HC595 shift register. The shift register needs just 3 wires to control 8 different outputs. Annoyingly, the LED bar has 10 different LEDs while the shift register has only 8 output pins. We solve this issue by doubling up the last 4 LEDs in the LED Bar graph. This way, the first 6 outputs of the shift register each control a single LED, while the last two outputs of the register control 2 LEDs each. Watch the video for a visual explanation. We give Arduino code below to light up the LED Bar graph in a bunch of different lighting patterns, like chasers, blinkers, counters, etc. This PCB is ready to daisy chain so for the same 3 wires on your micro-controller you can control tons of LED bars. We silkscreened a sun and moon on this board to remind you this little PCB makes light-- a small example of onomatopoetic electronics. Want to learn more about shift registers? Check out our shift register tutorial.

The LED Solar Silkscreen

Ledbargraph_74hc595_main
Check out the LED Bar graph powered by a 74HC595 shift register.

LED Bar Graph Video

See all the lighting modes in action! Chaser, counter, blinker and more! Check the code below to make your own.

Arduino Code for LED Bar Graph controlled by 74HC595 Shift Register

Below is code to cycle through several different lighting modes on an LED Bar graph controlled by a 74HC595 shift register. The code to do a chaser goes like this:
for (int i = 0; i < 8; i++) {
    // latchPin low so LEDs don't change 
    // while you are sending in bits:
    byte curb = 1 << i;
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  curb); 
    digitalWrite(latchPin, HIGH);
    delay(i*50);
  }
We write consecutive powers of two out to the LED Bar Graph. Every power of two in binary is a single one and all the rest 0s. So if we write 1,2,4,8,16 we will see a single LED illuminated and it will seem to walk up or down The LED's as if its chasing itself. We get the consecutive powers of two with this line:
byte curb = 1 << i;
the left shift operator (<< ) gives efficient access to exponentiation by 2. Shifting the number 1 i places to the left gives a binary number equal to 2 to the power i. Here is code to display what we call the filler lighting mode:
for (int i = 1; i <= 8; i++) {
    // latchPin low so LEDs don't change 
    // while you are sending in bits:
    byte curb = (1 << i)-1;
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  curb); 
    digitalWrite(latchPin, HIGH);
    delay(200);
  }
Just as any power of 2 represented in binary is a single 1 followed by 0s, any number exactly 1 less than a power of 2 is represented by all 1s up to the next order of magnitude bit. So we get those numbers: 1,3,7,15,31,63 in the line:
byte curb = (1 << i)-1;
If you just want all the LEDs to go on and off blinky style try this:
for (int i = 0; i <= 8; i++) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  255); 
    digitalWrite(latchPin, HIGH);
    delay(150);
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  0); 
    digitalWrite(latchPin, HIGH);
    delay(150);
  }
You can change the for loop condition from 8 to however times you the the bar to blink. In this code we use a home-cooked shiftOut function, but you can just as well use the Arduino library's shiftOut . Enjoy! Let us know in the comments if you build anything cool with the code below!
/* LucidTronix Shift Register
 * LED chaser w/ 74HC595 
 * Tutorial at:
 * http://www.lucidtronix.com/tutorials/43
 */

int dataPin = 2; //Pin connected to DS of 74HC595 
int latchPin = 3; //Pin connected to ST_CP of 74HC595
int clockPin = 4; //Pin connected to SH_CP of 74HC595

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < 8; i++) {
    // latchPin low so LEDs don't change 
    // while you are sending in bits:
    byte curb = 1 << i;
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  curb); 
    digitalWrite(latchPin, HIGH);
    delay(i*50);
  }
  for (int i = 1; i <= 8; i++) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  255); 
    digitalWrite(latchPin, HIGH);
    delay(150);
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  0); 
    digitalWrite(latchPin, HIGH);
    delay(150);
  }
  for (int i = 1; i <= 8; i++) {
    // latchPin low so LEDs don't change 
    // while you are sending in bits:
    byte curb = (1 << i)-1;
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  curb); 
    digitalWrite(latchPin, HIGH);
    delay(200);
  }
  for (int i = 0; i < 8; i++) {
    // latchPin low so LEDs don't change 
    // while you are sending in bits:
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin,  random(0,255)); 
    digitalWrite(latchPin, HIGH);
    delay(150);
  }
  for (int i = 0; i < 256; i++) {
    // latchPin low so LEDs don't change 
    // while you are sending in bits:
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, i); 
    digitalWrite(latchPin, HIGH);
    delay(30);
  }
}

// Shifts 8 bits out MSB first, on rising clock edge
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0); 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }
    digitalWrite(myDataPin, pinState); //write the bit
    digitalWrite(myClockPin, 1); //shift bits rising clock pin 
    digitalWrite(myDataPin, 0);//0 data to prevent bleed through
  }
  //stop shifting
  digitalWrite(myClockPin, 0);
}

Parts

Title Description # Cost Link Picture
74HC595 PCB This PCB maps the 74HC595 shift register chip to an LED bar. 1 $1.5 Link Screen_shot_2013-02-15_at_6.35.17_pm
LED Bar Graph 10 Red LEDs in a line. Value: Red 1 $1.26 Link Lta-1000hr
Shift Register IC SHIFT REGISTER 8BIT 16-SOP Tri-State Serial to Parallel Value: 74HC 1 $0.65 Link Screen_shot_2012-12-28_at_7.32.29_pm
Resistors RES 300 OHM 1/8W 5% 0805 SMD Value: 300 2 $0.1 Link Screen_shot_2012-12-28_at_7.28.51_pm
Resistor Array RES ARRAY 300 OHM 4 RES 1206 Value: 300 2 $0.02 Link Vishay_-_cra06s-8
Permalink: http://lucidtronix.com/tutorials/43
Make an interactive light show using a proximity sensor, and a bunsh of shift registers and LEDs....
Standalone programmable Heart-Shaped LED display....
Two LED matrices give 70 LEDs to light up a necklace with messages....
Build your own clock with the MCP79410 chip and 4 7-Segment LED modules....
Shift Registers help you multiply your inputs and outputs....
Display 4 digits using only 3 MCU wires with the 4 x 7 Segment LED and shift register breakout board....
Display numbers in big bright red LEDs use only 3 micro-controller pins and the 74HC595 shift registe...
Connect shift registers together to get more output and input pins from your microcontroller....