Feedback!

Shift Registers

Views: 12800 Difficulty: 1 Status: Complete
Screen_shot_2013-02-03_at_3.17.19_pm

Shift Registers help you multiply your inputs and outputs.

Shift registers help you multiply the inputs and outputs of a microcontroller. For example, a parallel-in, serial-out shift register will give 8 input pins using only 3 pins on the microcontroller. One example of this type of chip is the CD4021, the Arduino site has a nice tutorial on it. There are also serial-in, parallel-out shift registers, like the 74HC595. The 74HC595 gives 8 outputs using only 3 microncontroller inputs. There is also a great tutorial on the arduino site for using the 74HC595. One great thing about shift registers is they can be daisy-chained. This means you can connect multiple shift registers to each other without requiring any extra pins on your micro controller. In this way 3 MCU pins can control 16, 24, 32, etc. I/O pins. We use 4 daisy-chained CD4021 chips in our keyboard project so that we can listen to 32 buttons while only taking 3 pins from the microcontroller. We use 3 daisy chained 74HC595 chips in our heart matrix and in our light conductor project. You can see all our projects tagged with shift registers here.

Prototype view of 7segment LED with 74hc595 Shift Register

Numberblock_proto1.jpg
Fritzing diagram showing how to wire up the 74HC595 shift register.

74HC595 Driving 7 Segment LED

An Arduino Leonardo driving our 74HC595 breakout board and the 7 segment LED Display. These 74HC595 based 7-Segment LED number modules are available at our store.

Step-by-Step Instructions

Step 0 Understand the Timing Diagram

We don't really like timing diagrams over here, we rather see the thing in action, but this one is a beauty. It really shows how the signal comes in serially over the pin labelled DS, we call this the data pin, or the serial input pin. The byte sent is 11000011. Then after the byte is sent the pin labelled ST_CP (we call this the latch pin) goes high. This switches the mode of shift register, and it now displays each bit in the byte that was sent on its 8 output pins labelled Q0-Q7. It is as if that DS line in the timing diagram is rotated 90 degrees so that it now is display in parallel on pins Q0-Q7.

Step 1 Get the Pin Names Straight

One of the most frustrating things about dealing with shift registers is everybody calls the same pins by a different names. The shift register protocol requires three control lines: data (in or out), latch and clock. The data pin is also referred to as DS, serial in/out (depending on the type of shift register), signal, or SER. The latch pin switches the mode of the shift register from serial (in/out) to parallel (in/out) or vice versa. The latch pin is also referred to as ST_CP, RCK, and RCLK. Lastly, the clock pin is like the heart beat of the shifter and it oscillates from 1 to 0 indicating when a new bit is coming on the serial line. The Clock pin is also referred to as SCK, CLK, SH_CP, and SRCLK. So the terminology is a pain, but really there are just three control lines to worry about to get started.

Step 2 Wire it up!

Now that you know what the pins do, hook it up! This schematic shows a typical arrangement of 74HC595 controlling 8 LEDs with those 3 control pins (data, latch, and clock) going out to an Arduino or other micro-controller.

Step 3 Check out the Arduino Function

The Arduino language supplies the shiftOut and shiftIn functions for reading in from a shift register or writing out to one. These are handy functions but not terribly complex. We will show you how to write your own shift functions in the code section below. One thing to be wary of is the order in which you shift your bits (say that 5 times). You must specify either LSBFIRST (for Least Significant Bit First) or MSBFIRST (for Most Significant Bit First). This comes down to whether we read from left-to-right or right-to-left. This is know as endianness and the term is derived from Gulliver's travels, where the lilliputians fought over which side of a hard-boiled egg to eat first.

Step 4 Get Daisy Chaining

So now you know that for only 3 pins a shift register gives 8 input or outputs. Sweet deal right? But it gets better. Using just those three pins on the Arduino (or other MCU) you can keep adding shift registers and getting more inputs and outputs! This process is called daisy chaining and we have a whole tutorial dedicated to it. For starters if you want to daisy chain you will need to become familiar with one more pin on the 74HC595. This pin is called Q7' (notice the apostrophe). This is the serial output pin and you can connect it to serial input (DS) pin of the next shift register. You can repeat the process many times adding tons of outputs.

Arduino Code For Shifting Out LED Chaser

This code is for a basic LED chaser using a 74HC595 Shift Register. We use our shiftOut function for people curious about how it works, but you can use the one included with the Arduino library if you prefer. So the shiftOut function goes like this:
// 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);
}
The heart of this function is the for loop which iterates over all the bits in the byte. Each bit is checked to see if it is a 1 or 0 in the line:
if ( myDataOut & (1  <<  i) ) 
Here, we are using the bit-wise AND (&) operator, as well as the left shift operator( << ) to single out the ith bit. Now, the code to display the LED chaser call this shiftOut function inside a for loop, each time outputting another power of 2 (e.g. 1,2,4,8,16). These numbers in binary have a single one and all the other bits are 0. So from the shift register's perspective one LED is on and all the others are off. Here is the code:
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(500);
  }
}
Notice that again, we use the left shift bit-wise operator ( << ) , here we use it to get consecutive powers of two (this is more efficient than multiplying). Lastly, make sure to set the pin modes on all the shift register pins to output.
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
Alright now grab the file and get your knight rider on!
/* LucidTronix Shift Register
 * LED chaser w/ 74HC595 
 * Tutorial at:
 * http://www.lucidtronix.com/tutorials/2
 */
 
int latchPin = 1; //Pin connected to ST_CP of 74HC595
int clockPin = 2; //Pin connected to SH_CP of 74HC595
int dataPin = 0; //Pin connected to DS 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(500);
  }
}

// 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
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
Shift Register IC 8-BIT SHIFT REGISTER 16-DIP Serial to Parallel Tri-State 2 V ~ 6 V Value: 74HC 1 $0.63 Link Screen_shot_2012-12-28_at_7.40.34_pm
Shift Register CD4021 16-DIP Parallel-in Serial-Out CD4021 Shift Register from Texas Instruments. Through-hole DIP package. Value: Parallel to Serial 1 $0.52 Link Dip16_sot38-1_pkg
Shift Register IC STATIC SHFT REG 8STG 16-TSSOP Parallel or Serial to Serial 3 V ~ 18 V Value: Parallel to Serial 1 $0.45 Link Screen_shot_2012-12-28_at_7.33.47_pm

    Comments:

    20120519_163335
    sam
    DIP kits available!
    Thu, Jul 11 2013 2:36PM
    20120519_163335
    sam
    We will offer DIP Kits soon!
    Tue, Jan 01 2013 8:59PM
    Birdbirds
    ayi
    lalal
    Sat, Dec 29 2012 9:18PM
    Birdbirds
    ayi
    Nice tutorial
    Sat, Dec 29 2012 9:18PM
Permalink: http://lucidtronix.com/tutorials/2
Two LED matrices give 70 LEDs to light up a necklace with messages....
Standalone programmable Heart-Shaped LED display....
4 Daisy chained CD4021 shift registers gives us 32 inputs....
Build your own clock with DS1307 chip and 4 7-Segment LED modules....
Connect shift registers together to get more output and input pins from your microcontroller....
Make an interactive light show using a proximity sensor, and a bunsh of shift registers and LEDs....
Keyboard kit using CD4021 shift registers and big through hole buttons....
The 74HC595 shift register controls a 10 LED Bar Graph....