74HC595 Shift Register

52K5915

Intro: 74HC595 Shift Register

This will be a quick start guide to the 74HC595 Shift register.  

The shift register operates in a fairly simple way, but can be modified to become very complicated but very useful.

The basic concept is you have 8 output pins from the 74HC595.  For this example lets just say these are sent through a resistor to an LED.  So we have pins Q0 through Q7 as the output pins.  The rest can be considered control pins, their exact function is outlines in the table above. 

We can control the shift of the register with clock pulses.  As we raise the signal going to the clock pin to high, the clock is moved forward one step.  and when we pull it low and high again it shifts another.  Each time we shift the clock we switch the input to a different one of the eight registers.  We are essentially controlling the output of each of the eight pins one at a time, and as we move one clock signal forward, we switch to the next output pin to control.  

So far this simply sounds like a switch board, which is really what it is, but here's the key.

We can use the Storage register clock pin to control the "Master On/Off" switch.  Essentially how this is used is we can pull it low before we send our register values.  We then send all eight register values, whether they be high or low, and when we are done we pull the Storage register clock pin high.  What will happen is the value you send will be stored on each output pin, but not activated yet.  When you pull the storage register clock pin high all the outputs will then become active, and which ever pins you assigned as high will illuminate the LED.

Another perk is the Master Reclear Pin.  We can use this pin to clear all of our outputs and set everything to low.

So this is just a simple example.  The shift register can be a great tool when you are short on output pins, taking 8 outputs from only about 3 actual data inputs.  It can be added to for some really complicated applications, and they can be daisy-chained together for even more output options.

Hopefully this will help get you started and give you some knowledge on their basic concept.

I have included a sample arduino sketch and the wiring diagram from the arduino website that will turn on each LED one at a time given Serial Monitor input (0-9)


-------------------------------------- CODE START --------------------------------------
/*
  Shift Register Example
for 74HC595 shift register

This sketch turns reads serial input and uses it to set the pins
of a 74HC595 shift register.

Hardware:
* 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
as detailed below.
* LEDs attached to each of the outputs of the shift register

Created 22 May 2009
Created 23 Mar 2010
by Tom Igoe

*/

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("reset");
}

void loop() {
  if (Serial.available() > 0) {
    // ASCII '0' through '9' characters are
    // represented by the values 48 through 57.
    // so if the user types a number from 0 through 9 in ASCII,
    // you can subtract 48 to get the actual value:
    int bitToSet = Serial.read() - 48;

  // write to the shift register with the correct bit set high:
    registerWrite(bitToSet, HIGH);
  }
}

// This method sends bits to the shift register:

void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte bitsToSend = 0;

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

}
--------------------------------------- CODE END ---------------------------------------

I will try to get back to this to add more explanation to this code.

15 Comments

hi, this is great project but when i connect all and use the code it seems that after i send any number the correct LED blinks and then Q0 LED stays on. Can you help with it? I'm using arduino nano and pins described in the code.

As a newbie in electronics I was wondering what shift registers did. Thanks for the great information.

Can I use 74HC595 to detect buttons states! I work on a buttons matrix!

Example code!

Thank you!

You can use a similar process with what's called parallel in shift out registers. Same concept but all inputs with one serial output. Take a look at the 74HC165 chip

BR

I don't think 74HC165 work on my project. I found this it's pretty complicate. What do you think?

it really depends on your application. could you describe what you're trying to do more? I use a similar technique for button grids in my other I strict able here

https://m.instructables.com/id/Simple-Button-Keypad-Microcontroller/

this one uses all micro pins, but you can definitely use a shift register to control them like in the video you posted.

BR
Why is it that everything with many outputs starts at 0? Like, why not Q1-Q8?

Counting from 0 has origins in binary and makes a lot of counting operations easier in programming. For instance, lets say you want to do a for loop eight times:

for (int i = 0, i < 8, i++)

Because we started at 0, we can put the actual number of times we want it to loop in the conditional statement (the part that says i < 8). A lot of other math works better when starting from zero.

I think its more of a company/personal standard. It does make some things easier when writing code for certain languages. For example, in C++ when using arrays the first array position is always 0, so you can set array[0] to array [6], etc. But really I think it's just a decision, it can be defined any way you'd like.
So why does the array start at 0? That's part of my question. I don't expect you to know, it's just a strange thing I noticed.
Yea, its a programming thing. Some languages start at 0 and some dont. C++ starts at 0, but MATLAB (which is essentially C) starts at 1. Not sure the reason behind it.
Just a shot in the dark here but I think it might have to do with the way binary works. For a 3 bit number you can have the values 0-7.
000 -- 0
001 -- 1
010 -- 2
...
110 -- 6
111 -- 7

Maybe the makers of C++ were just lazy and decided to use the actual bit offset for the array numbering instead of subtracting 1 from the user coded arrays. Or maybe it was a simple 1 off error...

PS. I just realized that this is an old post but I took the time to write it so I am going to post it regardless :)
Will the shift register work with a arduino nano can't really find any information on this
So, with shift registers, from my understanding, it's like putting a matrix in a matrix. Does this mean that with regards to the blank/clear, when the LED's turn off, it's because of the signal coming from both the input and the output of the register? like signal confusion? Or is it terminating the flow of electricity into the shift register altogether.
Well

1. Its not really a matrix in a matrix. Think of it more like an array where you shift the values and each value is the output to an led. With two registers they are just set one after another, so as a value shifts out of the last position of the first "array" it shifts into the first position of the second "array"

2. By turning on the blank/clear you are just terminating the flow. I'm not exactly sure how everything inside is making it happen but it just simply doesn't show the register values at the output pins.