Using the 74HC595 shift register: You can instantly increase your
outputs by 8. For more just daisy chain extra chips, on the same
three wire SPI interface.
74HC595: A serial input, parallel output, HC series chip.
Do you need more microcontroller outputs?
Easily get more outputs using Cost Effective integrated circuit (HC595).
How to drive LEDs using the 595.
Practical code examples included.
The 74HC595 is useful if you find yourself needing more outputs than
you have available on your microcontroller; Its time to think about using this serial shift
register.
Using a two of your existing
microcontroller outputs you can add multiple 595's to extend outputs in
multiples of 8; with eight outputs available for every 595. When you add more 595s you don't use
up any more of your existing microcontroller output pins ;you can just daisy chain them together.
You use three microcontroller pins:
One for the data signal,
One for the shift clock,
One for the latch clock.
Set the data bit by changing the "data signal", and load it into the
595 using the "shift clock". Finally load the outputs from the internal
shift register to the 595 chip outputs using the latch clock.
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. Alternatively for inputs only, use a 74HC165.
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,
Using multiple 595's for 8, 16 or 32 outputs,
How to drive LEDs using using a 595.
You can do all this using only a single a 3 wire interface from your microcontroller.
Introduction
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.
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:
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.
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):
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!
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.
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
intRCLKPin=3;// pin 12 on the 74hc595 latch - nSS
intSRCLKPin=6;// pin 11 on the 74hc595 shift register clock - SCK
intSERPin=4;// pin 14 on the 74hc595 data - MOSI
unsignedintd;// Data to be sent to the shift reg.
intdir=0;// Direction of walking 1.
charbuf[12];// General purpose buffer.
voidsetup(){
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;
}
voidloop(){
delay(100);
digitalWrite(RCLKPin,LOW);
shiftOut(SERPin,SRCLKPin,MSBFIRST,0x00ff&d);digitalWrite(RCLKPin,HIGH);Serial.println(itoa(d,buf,16));
if(!dir)d<<=1;elsed>>=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
intRCLKPin=3;// pin 12 on the 74hc595 latch - nSS
intSRCLKPin=6;// pin 11 on the 74hc595 shift register clock - SCK
intSERPin=4;// pin 14 on the 74hc595 data - MOSI
unsignedintd;// Data to be sent to the shift reg.
intdir=0;// Direction of walking 1.
charbuf[12];// General purpose buffer.
voidsetup(){
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;
}
voidloop(){
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;elsed>>=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
intRCLKPin=3;// pin 12 on the 74hc595 latch - nSS
intSRCLKPin=6;// pin 11 on the 74hc595 shift register clock - SCK
intSERPin=4;// pin 14 on the 74hc595 data - MOSI
unsignedlongd;// Data to be sent to the shift reg.
intdir,num;// Direction of walking 1, output number.
charbuf[12];// General purpose buffer.
// Define an array to translate number to 7segment
charsegment[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
};
voidsetup(){
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;
}voidloop(){
unsignedcharlnum,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;elsed>>=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 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. This bad for long term design reliability.
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. 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.
So should you use the 74HC595 as a high current LED driver?
The answer is therefore No.
Can you use it as an LED driver - yes as long as the current is below 70mA total - for 8 outputs that is 8.75mA per output.
Driving Different
Different coloured LEDs have different voltage drops but you can
assume that the drop is around 2V for standard LEDs not including Blue
and White.
I=(5-2)/330 = 9.1mA, (Vtot - Vdiode) / Rd
For a RED led Vf= 1.7V (the smallest forward voltage) it is 10mA.
For a Blue or white LED the forward voltage drop is 3.2V ~ 3.6V and these are high brightness so don't need full current drive.
Since, for a 330Ohm resistor (usually used in 5V designs) the current will be too high change it to 470Ohm to get a 7mA drive.
Note 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..
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!
Unusual OE use
The OEn or active low output enable (note the letter 'n'
indicates negate or active low as you can't draw a line above characters
in text as you can on the schematic). OEn is a legacy from chips
designed to be used with a processor bus. The processor would enable the
chip output onto the 8 bit bus to allow the data into the ALU when
needed.
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).
Conclusions
If you need more outputs the 74HC595 is an easy to use chip that can be
daisy chained into as many outputs as you need. You can also use it to
drive LEDs successfully by choosing the right current limit resistor.
For a more advanced I/O chip Check out the MCP23017. This one allows Input and output direction control for individual pins.
Unlock the secrets of Arduino scrolling displays! This beginner-friendly guide shows you how to create real-time, dynamic graphics using an SSD1306 OLED, perfect for tracking sensor data and building…
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.