How to use the 74HC595 Shift Register with Arduino.
Often times when using microcontrollers we may need more digital input and output pins compared to the ones available for example in projects involving the use of many sensors, displays, LEDs and switches. One of the common ways of solving this problem is using serial to parallel shift registers like the 74HC595.
What is a Shift Register?
A register is simply a device for storing data. Electronic registers are made of groups of flip flops connected in series which are used to store multiple bits of data. The bits stored in such registers can be made to move within the registers and in or out of the registers by applying clock pulses. An n-bit shift register can be formed by connecting n flip-flops where each flip flop stores a single bit of data.
A Shift Register is therefore a type of sequential logic circuit that can be used for the storage or the transfer of binary data. This sequential device loads the data present on its inputs and then moves or “shifts” it to its output once every clock cycle, hence the name Shift Register.
The 74hc595 Shift Register.
The 74hc595 is an 8-bit Serial-In Parallel-Out (SIPO) shift register. This means it is uses serial data(one bit after another through a single data line) as input which then controls 8 output pins in parallel.
- GND: is connected to the ground of the Arduino.
- VCC: is the power supply for 74HC595 shift register which we connect the 5V pin on the Arduino.
- A(Serial Data Input): for sending data into the shift register one bit at a time.
- QA – QH(Parallel Data Outputs): These are the non-inverted, 3−state, latch outputs that can be connected to digital pins of the Arduino to control devices like LEDs.
- SHCP (Shift Register Clock): This is for providing a clock signal to the shift register. This clock is rising-edge driven which means a LOW to HIGH transition on this input causes the data at the Serial Input pin to be moved into the 8−bit shift register.
- STCP (Latch Clock): This provides a clock signal to the latch register and it is also rising-edge driven. A LOW to HIGH transition on this output moves the contents of Shift Register into the Latch Register.
- OE (Output Enable): This active low pin therefore is normally kept LOW. When this pin is LOW the data from the latch register is presented at the parallel outputs. A HIGH signal on this input forces the outputs (QA-QH) into high impedance state which stops the flow of current. The serial output is however not affected by this control unit.
- MR (Reset): This is an active low asynchronous shift register reset input. When set to LOW, all the bits in the shift register are reset to 0. If no reset is needed then this pin should be HIGH. This pin has no effect on the 8-bit latch.
- SQ_H(A non-inverted Serial Data Output): Acts as bit-7 output of the shift register. This pin is very important for daisy chaining the shift registers. This is achieved by connecting the SQH pin of one 74HC595 shift register IC to the A pin of another and give both ICs the same clock signal. In this case the two ICs will behave like a single IC with 16 outputs.
How the 74hc595 Shift Register works.
From the logic diagram above, the 74HC595 Shift Register has two registers which are the Shift register and Latch register. These registers act as storage areas (memory locations) each with 8-bit data storage capacity. The bits are stored as 1 or 0.
The working of this shift register can be represented using a simple timing diagram shown below.
Moving serial data into the shift register is determined by the Data and Clock pins. To move the 8-bits into the register, the clock pin needs to receive eight pulses. Whenever a clock pulse is applied and the data pin is HIGH, then a 1 is pushed into the shift register and if the data pin is LOW, a 0 is pushed to the register.
When all eight clock pulses have been received, enabling the Latch pin will copy the contents of the shift register into the Latch register. Each of the eight bits of the latch register is connected to the parallel output pins QA–QH. Therefore the final output signals are determined by the value of the bits in the latch register.
Connecting 74HC595 Shift Register to Arduino.
For the normal working of the shift register first connect the pin 16(VCC) and pin 10 (RESET) to 5V pin of the Arduino. Then connect pin 8 (GND) and pin 13 (OE) to the GND pin of the Arduino.
The three pins needed to control the 74HC595 shift register are connected to digital pins of the Arduino. For example in the diagram below I have connected the control pins as: Pin 11 (SHCP), Pin 12 (STCP) and Pin 14 (DS) of the shift register to Pin 8, Pin 7 and Pin 4 of the Arduino respectively.
The above connection can be used to demonstrate a number of applications of this shift register with Arduino as given below;
LED Chaser.
This enables us to light the eight LEDs one after the other from one side and then back in the opposite direction.
Code for the LED chaser using 74HC595 Shift Register.
int tDelay = 250;
int dataPin = 4; // DS - data serial on 74HC595
int latchPin = 7; // ST_CP - storage register clock pin on 74HC595
int clockPin = 8; // SH_CP - shift register clock pin on 74HC595 - Why not D13 SCK pin?
bool DirectionState = 0;
void setup(){
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop(){
byte leds = 0B00000000;
updateShiftRegister(leds);
delay(tDelay);
for (int i = 0; i < 8; i++){
// set the bit i to 1, starting with bit 0, the least
significant bit (rightmost bit)
bitSet(leds, i);
updateShiftRegister(leds);
delay(tDelay);
}
DirectionState = !DirectionState;
}
void updateShiftRegister(byte leds){
digitalWrite(latchPin, LOW);
if (DirectionState == false) {
shiftOut(dataPin, clockPin, LSBFIRST, leds);
} else {
shiftOut(dataPin, clockPin, MSBFIRST, leds);
}
digitalWrite(latchPin, HIGH);
}
Description of code.
The updateShiftRegister()
function is used to control the lighting of the LEDs by changing the bit values in the shift register using the latch pin. First the latch pin is made LOW to prepare the register to receive data. To change the values in the register the latch pin has to then be made HIGH.
The shiftOut()
function is for moving data on a serial connection. It can take a byte value and output it in a serial format in sync with a clock pulse on another pin. You can choose to output the data in two directions.
The parameter MSB First stands for “Most Significant Bit first”. For example if used with the binary number 10110010, then one bit at a time will be output starting with “101”,that is, from left to right.
Similarly LSB First stands for “The Least Significant Bit first” and when used the bits will be output one at a time beginning from right to left.
When the above code is uploaded to the Arduino board the LEDs will light as shown below;
Binary Counter
In this example we shall let the eight LEDs light in patterns corresponding to the binary value of numbers beginning from 0 to 255.
Code for the binary counter.
int dataPin = 4; // to 74HC595 pin 4
int latchPin = 7; // to 74HC595 pin 7
int clockPin = 8; // to 74HC595 pin 8
byte led1 = 0b00000000;
void setup() {
Serial.begin(9600);
// initialize I/O pins
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
digitalWrite(latchPin, LOW); // prepare shift register for data
shiftOut(dataPin, clockPin, LSBFIRST, led1); // send data
digitalWrite(latchPin, HIGH); // update register
delay(1000);
Serial.print(led1,BIN);
Serial.print(' ');
Serial.println(led1,DEC);
led1 = led1+1;
}
When this code is uploaded to the Arduino board the LEDs will begin lighting as shown below;
Controlling 7-segment displays.
74HC595 shift registers very important in controlling both single digit and multiple digit 7-segment displays as I showed in another tutorial which you can check out from this link: