Feedback!

Daisy Chain Shift Registers

Views: 33686 Difficulty: 2 Status: Development
Daisy_led_bar_graph

Connect shift registers together to get more output and input pins from your microcontroller.

We demonstrate how to add input and output pins using shift registers. The process of connecting several copies of the same chip together is called daisy-chaining. The term is derived from those pretty flower chains. We will show how to get more output pins from an Arduino or other MCU by daisy chaining the 74HC595 serial-in, parallel-out shift register. We will also discuss how to get more input pins into an Arduino by daisy-chaining the CD4021 parallel-in, serial-out chip.
Daisy chaining is a super-useful, common technique and we use it all the time in our designs. For example, our heart matrix LED display uses three daisy-chained 74HC595 chips to control the LED matrices. Our shift keyboard project, uses four daisy-chained CD4021 chips, to offer 32 buttons (a button for every letter in the English alphabet, and 6 to spare!), while only requiring 3 general purpose I/O pins from the micro-controller. Daisy-chaining is not limited to shift registers, in fact many chips afford this functionality. For example, the TLC5940 PWM controller can be daisy-chained in a similar manner.

A LED daisy made from daisy-chained shift registers!

Shift_register_daisy_bar_graph
This PCB daisy has 6 of our LED bar graph shift register boards wired to a hexagon shaped arduino clone.

Video of 7 Segment LEDs Daisy-Chained

Check out the daisy chained 7 Segment LEDs. You can get your own 7 Segment LED module at the LucidTronix store.

Video of Daisy-Shaped LED Bar Graph Daisy-Chain of 74HC595 Shift Registers.

See the daisy-shaped daisy chain of LED bar graphs in action!

Step-by-Step Instructions

Step 0 Daisy Chain your Shift Registers

The way to daisy-chain two 74HC595 shift registers together is to connect the Q7' pin on the first shift register to the Serial In pin on the second register. All shift registers in the daisy chain share the latch and clock lines. The schematic shows a typical set up of two daisy chained 74HC595 chips. You can continue to add more chips by connecting the Q7' pin on the last shift register to the Serial In pin of the shift register you are adding.

Step 1 Daisy Chain CD4021 Shift Registers

For a parallel-in serial-out shift register like the CD4021, the set up is very similar to the one shown above. Again, all the shift registers share the same latch and clock pins from the micro-controller. As always these pins go by many names (latch is also referred to as PL, and parallel/serial control or P/S CTRL, and the clock pin is labelled CP in the diagram. Now, the serial in pin on the shift register closest to the micro-controller is connected to the data pin of the second shift register. The data pin is labelled Q7 in the diagram. Don't be confused, "Serial In" on the CD4021 chip has a very different meaning than serial in on the 74HC595. Because the CD4021 is a parallel-in serial-out shift register "serial in" to the CD4021 means where data from a daisy-chained shift register comes in to be sent to the micro-controller. On the 74HC595, Serial In means where data FROM the micro-controller goes to be displayed by the shift register (or sent down the daisy chain to be displayed by another register).

Arduino Code for Two Daisy Chained 74HC595 Shift Registers

Once you've got your shift registers daisy chained together writing out to the chain is super easy. All you have to do is call the shiftOut function once for every shift register you have connected. So in this code, which drives two 7 Segment LED displays, we have these two lines inside the nested for loops:
      shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[onesColumn]);
      shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[tensColumn]);
This outputs the numbers 00-99 on the shift registers. Not that the first call to shift out sends its byte to the shift register at the far end of the chain (i.e. the register that is furthest from the Arduino.
/* LucidTronix
 * Daisy Chained Shift Registers
 * 74HC595 connected to 7-Segment LED display
 * Tutorial at:
 * http://www.lucidtronix.com/tutorials/40
 * April 2013
 */

int dataPin = 2;
int latchPin = 3;
int clockPin = 4;

byte dec_digits[] = {0b11000000,0b11111001,0b10100100,0b10110000,0b10011001,0b10010010,0b10000011,0b11111000,0b10000000,0b10011000 };

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 tensColumn = 0; tensColumn < 10; tensColumn++) {
    for (int onesColumn = 0; onesColumn < 10; onesColumn++) {
      // take the latchPin low so 
      // the LEDs don't change while you're sending in bits:
      digitalWrite(latchPin, LOW);
      // shift out the bits:
      shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[onesColumn]);
      shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[tensColumn]); 
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
      // pause before next value:
      delay(300);
    }
  }
}

Arduino Code for Four Daisy Chained 74HC595 Shift Registers

It is easy to extend the code above so that it can control for shift registers instead of two. We just add two more calls to the shiftOut function. So now there are four nested for loops and they go like this:
    for (int thousandsColumn = 0; thousandsColumn < 10; thousandsColumn++) {
      for (int hundredsColumn = 0; hundredsColumn < 10; hundredsColumn++) {
        for (int tensColumn = 0; tensColumn < 10; tensColumn++) {
          for (int onesColumn = 0; onesColumn < 10; onesColumn++) {
            // take the latchPin low so 
            // the LEDs don't change while you're sending in bits:
            digitalWrite(latchPin, LOW);
            // shift out the bits:
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[onesColumn]);
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[tensColumn]);
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[hundredsColumn]); 
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[thousandsColumn]);
            //take the latch pin high so the LEDs will light up:
            digitalWrite(latchPin, HIGH);
            // pause before next value:
            delay(50);
          }
        }
      }
    }
This is the code that is running during the video. You can download the .ino file below.
/* LucidTronix
 * Daisy Chained Shift Registers
 * 74HC595 connected to 7-Segment LED display
 * Tutorial at:
 * http://www.lucidtronix.com/tutorials/40
 * April 2013
 */
 
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;

byte dec_digits[] = {0b11000000,0b11111001,0b10100100,0b10110000,0b10011001,0b10010010,0b10000011,0b11111000,0b10000000,0b10011000 };

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 thousandsColumn = 0; thousandsColumn < 10; thousandsColumn++) {
      for (int hundredsColumn = 0; hundredsColumn < 10; hundredsColumn++) {
        for (int tensColumn = 0; tensColumn < 10; tensColumn++) {
          for (int onesColumn = 0; onesColumn < 10; onesColumn++) {
            // take the latchPin low so 
            // the LEDs don't change while you're sending in bits:
            digitalWrite(latchPin, LOW);
            // shift out the bits:
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[onesColumn]);
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[tensColumn]);
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[hundredsColumn]); 
            shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[thousandsColumn]);
            //take the latch pin high so the LEDs will light up:
            digitalWrite(latchPin, HIGH);
            // pause before next value:
            delay(50);
          }
        }
      }
    }
}

    Comments:

    Missing_profile
    Feather
    Nice project. For better design, you may need a free, easy-to-use EDA tool aiming to bring electronics hobbyists an Easier EDA Experience and integrates powerful schematic capture, mixed-mode circuit simulation and PCB layout.Try and learn more about EasyEDA by https://easyeda.com/
    Thu, Jun 16 2016 11:17PM
    Missing_profile
    JimMorton
    So if the delay in the sketch running the four daisy-chained 74HC595 shift registers were increased to 1000, would the display be counting upwards in seconds?
    Tue, Jan 05 2016 3:04PM
Permalink: http://lucidtronix.com/tutorials/40
A QWERTY keyboard you build yourself....
Keyboard kit using CD4021 shift registers and big through hole buttons....
Shift Registers help you multiply your inputs and outputs....
4 Daisy chained CD4021 shift registers gives us 32 inputs....
Build your own clock with the MCP79410 chip and 4 7-Segment LED modules....
32 buttons for a fully-functioning hand-held USB keyboard....
Display 4 digits using only 3 MCU wires with the 4 x 7 Segment LED and shift register breakout board....
Two LED matrices give 70 LEDs to light up a necklace with messages....