Using the 74HC595 shift register: You can instantly increase your outputs by 8. For more, just daisy chain extra chips, on the same SPI interface.

74HC595: A serial input, parallel output, HC series chip.

  • Need more cheap microcontroller output pins?

  • Driven using an SPI interface; it's very high speed.

  • Find out here if you can use it to drive LEDs.

  • Examine practical code examples here.

The 74HC595 is useful if you find yourself needing more outputs than you have available on your microcontroller; Its time to think about using a serial shift register such as this chip.

Using a few of your existing microcontroller outputs you can add multiple 595's to extend outputs in multiples of 8; 8 outputs per 595. When you add more 595s you don't use up any more of your existing microcontroller output pins.

The way it works is that you use one microcontroller pin as a data output and another as the clock. Simply toggle the clock pin low to high to clock in the value at the data input of the 595. Since the 595 has 8 outputs you do this 8 times to fill the 595 memory. To transfer the new stored data to the outputs toggle another pin and you're done - 8 new outputs working.


Tip: The 74HC595 series chips are useful but only provide outputs. If you need both Inputs and Outputs (and interrupts on input) then have a look at this device : The MCP23017.

You can also string together multiple 595's for even more outputs! There are several examples here that show you how to do it.

This 74HC595 tutorial shows you examples of driving 7 segments, LEDs, and using multiple 595's for 8, 16 or 32 outputs all using just a 3 wire interface. You can also find out the correct way of using these devices to drive LEDs and seven segment displays and 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 these 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 daisy chaining 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.

Note: You can wire each chip up in turn as you go through the three examples below - you don't need to do them all at once.

74HC595 x 4 daisy chained showing wiring connections

View larger image here.

Note: all resistors for led drives including the seven segment 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 1 : 8 LED Walking 1

This example Arduino sketch 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.

// 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 2 : 16 LED Walking 1

This example Arduino sketch 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 ).

// 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 3 : 32 bit walking 1 and 2 x 7 segments

This example Arduino sketch 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 assigned 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!

// 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

It is not really worth using an Arduino library for the 74HC595 as shiftout does all the work required to get the data into the chip. The only extra code you need is to control the latch output clock (RCLK) before and after using shiftOut.

Arduino library code modules are 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 the link to open the 74HC595 data sheet in a new window.

74hc595 datasheet

74HC595 as an LED Driver (output current)

Driving LEDs with the 74HC595

You can drive LEDs with the 595 but the output current must be kept low (70mA) for the whole chip. The following discussion details how to do this and recommends a reliable current limit resistor value. Note that the LED output current also depends on the LEDs being driven (see discussion below).

Note: The datasheet 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 for the whole chip!).

Dangers of 'Web' Designs

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:

"this chip ... 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.

The control pin OE disables the output (see here 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.

Driving Different LEDs

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.

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.

More Output Current Drive

Boost current output

The even safer route for this chip, actually the way an electronics designer would choose if the design could not afford to use a different device, is to boost the output with a transistor or FET. A logic mosfet will do the job or use a transistor array (8 to a pack).

High current 595's

Another solution is to use a high current version of the 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!

Advanced Topics

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 you can work out the required operational speed. As an example say you want to go at 100kHz (turn on an d off an output 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 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).

For a more advanced I/O chip Check out the MCP23017.


Comments

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

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.




Privacy Policy | Contact | About Me

Site Map | Terms of Use