Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
:
:
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine.

74HC595 Shift Register

This 74HC595 shift register tutorial shows you how to increase outputs for any microcontroller. Learn how to add 8, 16, 32 or more outputs with multiple 595's.

The 74HC595 is a serial-to-parallel shift register or SIPO (Serial In Parallel Out) device for increasing the number of outputs from your microcontroller. Its simply a memory device that sequentially stores each bit of data passed to it.

You send it data by presenting a data bit at the data input and supplying a clock signal to the clock input. At each clock signal the data is passed along a chain of d-types - the output of each d-type feeds into the input of the next.

When you have clocked in 8 bits of data you can then activate the parallel 8 bit output from the d-type stores.


SPI Interface

The 595 uses the standard SPI interface. The three serial control signals from the microcontroller are:

  • Serial Data In.
  • Serial Clock.
  • Latch Clock.

With only thses 3 serial controls you can add as many outputs as you need - the number of serial control wires always remains the same.

On the chip there are also two other control signals:

  • Master reset (MR)
  • Output Enable (OE)

The output enable is not really useful for modern designs just connect it to ground (see here for what it should really used for).

Inside the 74hc595.

You can wire the Master Reset (MR) control inactive unless it is vital that the outputs are reset to zero on power up. Leaving it active means a software reset is required at the start to send zeros to the shift register and then strobe them to the output using the latch clock.

Note: On my bench, on first power up, all the outputs came up fully On! If this is a problem then use a global reset signal (that you make from a few components to hold the reset line low for a while after power up). You can also use this signal to reset any other components that have an active low reset - they usually do - such as the processor.

The 595 allows you to transmit data in a serial stream using a few wires with the outputs at the destination delivered in parallel. In addition multiple 595's can be daisy chained so you can have as many outputs as you need without any increase in serial wiring.

Microcontroller systems come in 8, 16, 32 bit parallel bus designs because it is faster to send data internally inside a chip using a wide bus (a set of parallel connections). However when you need to interconnect devices outside the processor, connecting and routing 32 wires all over the place becomes a problem (even 8 is a pain). The solution is to use a serial interface and translate the parallel 8, 16 or 32 bit data into a timed stream of data. The 595 will translate that back into an 8 bit output (see later for increasing the parallel output width).

Structure of the 74HC595

The 595 has two main registers:

  • The shift register.
  • The output register (or data latch or sometimes called a register latch).

The 74HC595 is arranged this way, so, when you update the shift register data using the serial clock (SRCLK) and serial data (SER) inputs, the outputs from the chip do not change .i.e. you can feed data into the shift register and it will ripple through without affecting the output pin state. Only when the output data clock is strobed (latch clock or register clock - RCLK) will the data be transferred from the shift register to the output pins.

The diagram below shows the internal structure of the 595:

Internal structure of the 74hc595.

74HC595 pinout

The diagram below shows the pinout of the 74HC595. It follows the standard TTL chip layout with power at top right and ground at bottom left. All outputs except one are on the left side of the chip and control signals are on the right. The serial output QH' is at bottom right and this is for daisychaining the devices.

74hc595 pins - pinout of the chip

Pin name Functions
QA-QH Outputs Q0 (QA) to Q7 (QH).
QH' Serial data output.
SER Serial data input.
SRCLK Serial data input clock.
RCLK Output register (Latch) clock.
SRCLR Active low, Asynchronous Shift register Clear.
OE Active low, Output Enable.
VCC Positive Power Supply.
GND Negative Power supply (Ground - 0V).

How to make a 16 bit shift register with the 74HC595

Making a 16 bit shift register is very simple it is just using a 74HC595 daisy chain. All you do, is to use two 595's and connect the data output from the first shift register to the data input of the next one i.e. connect the QH' of the first 595 to the SER (serial data input) of the next.

The diagram below shows the simple connections required (note how the serial clock signals are fed into both chips while the data signal is shifted out from the 1st chip and into the 2nd chip):

16 bit shift register with the 74hc595

Note: The only change that is required to control the outputs is that instead of 8 x SRCLK clock pulses you will need 16 x SRCLK clock pulses, with associated serial data (SER) i.e. a software change is required.

Daisy chaining more than 2 x 74HC595s

How to serial connect 4 74HC595s in a chain; This is very simple to do - it is just an extension of the above process i.e. connect the serial out of the previous 74HC595 to the serial input of the next. The only difference is the total number of bits to be transmitted.

Note: The only change is a software change, and that is to send more serial data and serial clocks to the 1st 595. The data then ripples through to the last 595, as long as you send enough clocks!

ic 74hc595 shift register connecting 4 together

View larger image here.

A Project using the 74HC595 arduino for 7 segment Display.

This project shows how to wire up and control 16 LEDs and two seven segment displays using 4 74HC595 chips. There are three sets of code going through 8,16 and 32 outputs to allow easy wiring of the devices.

74HC595 x 4 daisy chained showing wiring connections

View larger image here.

Note: all resistors for led drives including the seven segement displays are : 470 Ohms. See discussion here for why this should be the lowest value i.e. not 330 Ohms

As you can see only three connections are required from the Arduino and you can add more 595 chips as you require more outputs, without using any more Arduino pins.

The Arduino connections are:

  • P3 - RCLK - register or latch (output) clock.
  • P6 - SRCLK - Serial Register Clock.
  • P4 - SER - Serial Data input.

Example Arduino sketch 1 : 8 bit output

This example shows how to drive a single 74HC595 using the outputs for driving LEDs. The walking one direction is controlled by the variable dir while the actual value is set into variable d. If a high bit is detected at either end of the byte then the direction is changed. in this way the walking one goes left and right through the byte. After each shift the value is output to the 595 using the shiftOut function, MSB first. Note: shiftOut operates on bytes only.

Click in the code below to copy it to the clipboard.

// Demonstration code for 74HC595 8 bit output
int RCLKPin = 3;   // pin 12 on the 74hc595 latch - nSS 
int SRCLKPin = 6;  // pin 11 on the 74hc595 shift register clock - SCK
int SERPin = 4;    // pin 14 on the 74hc595 data - MOSI
unsigned int d;    // Data to be sent to the shift reg.
int dir =0;        // Direction of walking 1.
char buf[12];      // General purpose buffer.

void setup() {
  Serial.begin(9600);          // start serial port (debug).
 
  pinMode(RCLKPin, OUTPUT);    // Set 595 control PIN sto output.
  pinMode(SRCLKPin, OUTPUT);
  pinMode(SERPin, OUTPUT);

  Serial.println("74HC595 Demo.");

  d=1;
}

void loop() {
      
    delay(100);
    digitalWrite(RCLKPin, LOW);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, 0x00ff & d);    
    digitalWrite(RCLKPin, HIGH);     
    Serial.println(itoa(d,buf,16));

    if (!dir) d<<=1; else d>>=1; // Shift
    
    if (d&0x80) dir=1;           // Set direction.
    if (d&0x01) dir=0;
}


Example Arduino sketch 2 : 16 bit output

This example extends the previous example to to drive two 595s creating a 16 bit shift register with the 74HC595 and using the outputs for driving LEDs. Again a walking one is created but this time the code uses an unsigned integer to store the value allowing 16 bits of output. Note how the shiftOut function must be used twice to output the 16 bit value (that also has to be split into 2 pieces using bit shifting since shiftOut can only handle 8 bits of data at a time ).

Click in the code below to copy it to the clipboard.

// Demonstration code for 74HC595 16 bit output
int RCLKPin = 3;   // pin 12 on the 74hc595 latch - nSS 
int SRCLKPin = 6;  // pin 11 on the 74hc595 shift register clock - SCK
int SERPin = 4;    // pin 14 on the 74hc595 data - MOSI
unsigned int d;    // Data to be sent to the shift reg.
int dir =0;        // Direction of walking 1.
char buf[12];      // General purpose buffer.

void setup() {
  Serial.begin(9600);          // start serial port (debug).
 
  pinMode(RCLKPin, OUTPUT);    // Set 595 control PIN sto output.
  pinMode(SRCLKPin, OUTPUT);
  pinMode(SERPin, OUTPUT);

  Serial.println("74HC595 Demo 2xchips for 16 bit register.");

  d=1;
}

void loop() {
      
    delay(100);
    digitalWrite(RCLKPin, LOW);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, (0xff00 & d)>>8);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, 0x00ff & d);    
    digitalWrite(RCLKPin, HIGH);     
    Serial.println(itoa(d,buf,16));

    if (!dir) d<<=1; else d>>=1; // Shift
    
    if (d&0x8000) dir=1;           // Set direction.
    if (d&0x0001) dir=0;
}


Example Arduino sketch 3 : 32 bit output

This example extends the previous example keeping the original operation but now driving 2 seven segment displays as well. Seven segment displays are just arrangements of LEDs that are used to represent digits. In the datasheet each LED is assgined a letter in a standard position; These are letters are 'a' through 'g' and 'dp' (for the decimal point). To make coding easier it is best to assign 'a'-'g' to b0-b6 of the output and dp to b7. Note to make routing a pcb easier, you could assign outputs as needed but then the array holding the conversions value (display digit) would have to be changed.

In this example the "walking one is kept as before" but now to further 8 bit shiftOut calls are made. Note how everything is shifted right to get it to the lower 8 bits for the shiftOut function.

Note: Even for 32 outputs (or even more) you still only need 3 control wires!

Click in the code below to copy it to the clipboard.

// Demonstration code for 74HC595 16 bit output and 2x7 segment displays
int RCLKPin = 3;   // pin 12 on the 74hc595 latch - nSS 
int SRCLKPin = 6;  // pin 11 on the 74hc595 shift register clock - SCK
int SERPin = 4;    // pin 14 on the 74hc595 data - MOSI
unsigned long d;   // Data to be sent to the shift reg.
int dir,num;       // Direction of walking 1, output number.
char buf[12];      // General purpose buffer.

// Define an array to translate number to 7segment
char segment[10] = { // segment bit pos: dp g f e d c b a
  0x3f, // zero
  0x06, // one 
  0x5b, // two
  0x4f, // three
  0x66, // four
  0x6d, // five
  0x7c, // six
  0x07, // seven
  0x7f, // eight
  0x67  // nine
};

void setup() {
  Serial.begin(9600);          // start serial port (debug).
 
  pinMode(RCLKPin, OUTPUT);    // Set 595 control PINs to output.
  pinMode(SRCLKPin, OUTPUT);
  pinMode(SERPin, OUTPUT);

  Serial.println("74HC595 Demo 4xchips for 16 bit register and 2x7seg.");

  d=1;
  num = 0;
}  

void loop() {
unsigned char lnum,rnum; 
       
    if(num++>99) num=0;
    lnum = num / 10; 
    rnum = num % 10;
    
    digitalWrite(RCLKPin, LOW);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, segment[lnum]);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, segment[rnum]);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, (0xff00 & d)>>8);
    shiftOut(SERPin, SRCLKPin, MSBFIRST, 0x00ff & d);    
    digitalWrite(RCLKPin, HIGH);     
    
    Serial.println(ltoa(d,buf,16));

    if (!dir) d<<=1; else d>>=1; // Shift
    
    if (d&0x8000) dir=1;           // Set direction.
    if (d&0x1) dir=0;

    delay(200);
}



74HC595 arduino library

There are Arduino library code modules available but they use shiftOut as does the code above. So it may not really be worth using them as all you need to do is set RCLK low, send out the data using shiftOut, then set RCLK high. The library will just tidy the code a bit. I have also read that the 74HC595 library can not cater for more than 8 bits - not sure if that is true but if it is, then definitely use the method shown in the examples in this page.

74HC595 datasheet

Click the image or here to open the 74HC595 data sheet in a new window.

74hc595 datasheet

Advanced bits

Data bandwidth

As with all serial interfaces the ultimate outcome is the appearance of data at the output from end to end. You start off with an 8 bit quantity serialise it and then reconstruct it at the output. The time taken to do this process is the limiting factor.

If you are only setting indicators or turning slow speed relays on and off, then this is not an issue, however, if you are doing a more complex operation such as driving PWM outputs or other high speed operation then the bandwidth is important (this chip is not the best for PWM - use a dedicated PWM device e.g. TLC5940 for easy PWM expansion).

if you want to turn on and off the outputs at a specific rate then there are as an example say you want to go at 100kHz. When you serialise it the data has to be 8 times faster (plus a bit more for the strobe signal) i.e. ~800kHz. If you had 16 outputs then it would require an update rate of ~1600kHz or ~1.6MHz.

Getting a microcontroller to go at 1.6MHz strobe rate will be impossible (for a 20MHz device it would not be able to do anything else - obviously a very high speed device could achieve this) but the internal SPI module can typically operate at 20MHz.

That sounds like I have contradicted myself but I haven't. The internal module can operate very fast so a burst of serial data can be output very fast. That does not mean that the slower processor can keep up. This is what determines the overall data rate i.e. there will be a highs speed burst and then a waiting period while the processor decides to react to the interrupt generated from the SPI module. It is the processor speed that determines throughput.

SPI operation

You could use the internal SPI module to output data faster, so that the processor operation is not consumed by the 595 update. However the limiting factor now becomes how fast can you update the SPI module and that depends on interrupt timing and processor speed i.e. what other stuff it is doing. The other limiting factor is the speed of the HC595 (25MHz) as some microcontrollers can operate faster than this! However 74AHC can go at 100MHz!

These are just items to think about when designing this kind of system and are not investigated further here.

74HC595 as an LED Driver (output current)

Note: the datasheet (see below) does suggests use of the device as a low current LED driver i.e. limit the max output current for the total "all-on-state" to 70mA (this is the 74HC595 max current).

There are lots of designs on the web using the 74HC595 as a high current LED driver. The key question is : Should it be used for this at all? Some of these "designers"? have completely mis-read the data sheet and operate the outputs at high current. As with all chips you can go beyond the tested limits, and these limits may be conservative for the device but for a reliable design you do not want to go against the datasheet because at some point (likely when 100s of units have been made) units will start to fail - and that can be very expensive to correct.

Side Note: For instance this instructable [opens in a new window] states:

"However, this chip only outputs 0 or 5V and it can output as much as 70mA per pin";

It does not. The datasheet has been mis-read; The actual value (70mA) is for the total current through the chip. While the maximum for any pin is 35mA the total current through the whole chip should not exceed 70mA.

Also note that in that instructable there is a statement that holding the latch pin low disables the output - rubbish, it does not - all it does is "not update the output" - there is no disabling. If anything the control pin OE disables the output (see later for information on OE). The actual operation is that a rising edge on the Latch pin clocks the data to the output.

Warning: Do not believe everything (anything?) in that instructable.

Getting something right by design is more important than testing just a couple of chips and saying it works! Datasheets show what has been tested to guarantee operation and long term reliability. Yes, the conditions may be a bit conservative and less than could be achieved, and yes, the chip may work for a while under extreme overdrive but it is not worth the risk for a "real" design.

So should you use the 74HC595 as a high current LED driver?

The answer is therefore No (for high current output.; Any electronics designer reading the datasheet will see that the maximum output current for the whole chip is 70mA meaning for 8 LED the maximum that can be output for each LED is 70e-3/8=8.75. In fact the Motorola datasheet states ±6mA per output pin (other datasheets don't state this) - this is probably to allow current for internal FET operation. The solution is to use boost the output using FETs or Transistors or a transistor array.

Warning: The maximum current into/out-of the 74HC595 is 70mA maximum with a 500mW max dissipation - any higher than that then you are operating the device outside its specification and risk heat damage to the chip i.e. it may work for a while but then again, it may fail if driven beyond these limits for a long time.

You can probably get away driving LEDs if either:

  1. You don't drive all the LEDs at the same time.
  2. Drive different LEDs at different power levels (I limit resistors) observing max dissipation and max current.
  3. Drive LEDs at low current.

When you see a design that uses 330Ω (Rd) resistor in series with and LED driven by the output of a 595 then the current that should be driven is:

I=(5-1.7)/330 = 10mA, (Vtot - Vdiode) / Rd

Assuming a RED led Vf= 1.7V

Note: Different LEDs have different forward voltage drops (see below).

The following table shows current for a 330Ω current limit resistor with different LED colours (with a 5V power supply).

LED type Vf (V) Current (mA)
( Rd = 330Ω )
Current (mA)
( Rd = 470Ω )
Red LED (standard) forward voltage 1.7 10 7.0
Red LED (Bright) forward voltage 1.9 9.3 6.5
Orange / Yellow LED forward voltage 2.0 9 6.4
Green LED forward voltage 2.1 8.7 6.1
Bright White ~ Blue LED forward voltage 3.2 ~ 3.6 5.4 ~ 4.2 3.8 ~ 2.9

You can see that those designs are not driving the LEDs at maximum output (usually 20mA - check each LED datasheet for max. allowed). However on the bench, with standard LEDs, they look bright enough to me! Also the 7 segment display looks good at about 7mA (Rd=470Ω) for each red LED segment..

For the 330Ω current limit resistor:

However for 10mA at each output there will be 80mA used which is beyond the design limit for the 595. The only one that will be in-spec is the Bright White or Blue LED (5.4 x 8 = ~48mA).

For the 470Ω current limit resistor:

By changing to a 470Ω current limit resistor the corresponding current draw is reduced and all outputs are within specification of the device 8 x 7mA) = 56mA (max design output must be below 70mA - see datasheet).

There is a little bit of room to decrease the resistor but the LEDs will be underpowered. However the question is do you really need maximum brightness?

Conclusion

Probably the best way to go (for long term reliability) is to choose a high brightness type and adjust the resistor accordingly. However the safe route is to use 470Ω for all current limit resistors. The even safer route for this chip, actually the way an electronics designer would choose, is to boost the output with a transistor or FET.

Note: Another solution is to use a high current 74HC595. A suitable version is the TIPC6595 (or TIPC6C595) -which is(are) capable of 250mA output per pin! - this is a high power shift register. But you are going to find that it is more expensive than a 595!
Note: This is also why the example circuit shown in the datasheet (above) uses current limit resistors of value 560Ω for driving LEDs - that value seems a bit conservative which is why I used 470Ω which also allows the total output current to be below 70mA.

Alternatives to the high current 595

Use transistors or MOSFETs at each output (a logic mosfet will do the job). Or use a transistor array (8 to a pack).

74HC595 OE

What is the (OE) Output Enable for?

In the old, old days of yore, when a high end design consisted of an 8 bit system (similar to an 8 bit microcontroller) the only design technique was to buy individual components such as:

  • A processor e.g. a Z80.
  • EPROM - Erasable Programmable Read Only Memory.
  • RAM (requiring refresh).
  • An ADC.
  • ...plus more devices as needed.

Each of the above chips is a separate, individual device and the question was:

  • How do I connect these things together to make a functioning system?

The answer is that each device has an 8 bit bus, so that bus had to be used to transfer data around the system.

The next question is:

  • If the EPROM is constantly outputting data, how do I get the ADC to output data to the processor instead?

The answer is that you have to be able to turn off the output at a specific time (a set period in the processor instruction cycle) e.g. during a processor read operation the relevant device needs to be activated. The processor will have a dedicated read signal output usually labeled active low RD. Note a similar signal is used for write nWR.

You do that by setting all output enables of all other devices high (output is inactive) so that all other devices are not driving the bus and then you pull the ADC output enable low, so that only the ADC drives the bus - in this way the processor can read the ADC output when it wants to.

Note: All the above components are now inside a microchip but the same design process is used to enable each individual unit to access the (internal) bus. It is just that now you do not need to think about it i.e. it is hidden.
Note: If you want to really understand processor operation try looking at an old chip design (there are people who are designing with them as a hobby) e.g. a Z80, 6502 or 68000 etc. will show you how it is done - look for a chip select device (unless the processor has chip select outputs) e.g. 74LS138 that enables each unit.

So OE is a legacy from that design process. The advantage was that you could keep on adding devices to the main bus (within the fan out limitation of the processor) as long as you could control the output enable of the device. That is why there is an OE control pin in the 74HC595.

TIP: One unusual idea I have seen on the web for making use of the OE control input is as a current output controller i.e. a dimmer (if using the outputs as LED drivers). All you do is feed in a PWM signal to the OE input and voila, instant dimmability (of course this affects all the outputs of the 74HC595 at once).

New! Comments

Have your say about what you just read! Leave me a comment in the box below.




Privacy Policy | Contact | About Me

Site Map | Terms of Use



Visit our Facebook Page:

   Click Here



Recent Articles

  1. Arduino Battery Charger: How to save the planet one battery at a time; Recycle and re-use Alkaline battries

    Arduino Battery Charger: A very useful project that lets you charge 'un-rechargeable' alkaline batteries!

    Read more

  2. [Arduino Tutorial] : How to use the 74HC595 shift register.

    The Essential Guide to the 74HC595; What it is and how you can easily use one in any of your projects.

    Read more

  3. How to use the MCP23017 I/O Expander on the Arduino

    How to use the MCP23017 to increase your I/O by 16 pins (or more) and use its interrupt system.

    Read more

Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:


And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:

Now



:
:
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:



But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates


Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.



Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's

SUPERB and FANTASTIC."

- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan

bursach<at>gmail.com<

"Awesome site,
very, very easy and nice
to navigate!"


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.
Regards."


- Anon

Back to Top