Learn how to interface the shift register 74HC595 with Arduino. Get hands-on with two simple code examples and see your electronics projects come to life.
A shift register 74HC595 with Arduino
Arduino uno board has limited I/O pins to wire up different types of electronic components to perform multiple tasks at a time. But, sometimes if you want to wire up some more components, and you are not left with any input pin with Arduino in that scenario you need a shift register (74HC595) to add more I/O pins to Arduino.
What is Shift register (74HC595)?
A shift register is a special type of digital circuit that can store and move data one bit at a time. A shift register can be used with an Arduino to expand the number of output pins available and simplify wiring in projects requiring multiple device controls. This is particularly useful when working with projects that require a lot of LEDs or other similar devices.
What is the need for a Shift register?
The shift register takes in data serially (one bit at a time) and then outputs it in parallel (all at once). This means you can control multiple devices using just a few pins on your Arduino, which can be very useful if you are running out of pins or want to simplify your wiring. The 74HC595 shift register can be controlled by 3 pins of an Arduino Uno and in return provides results for 8 output pins.
In this tutorial, you will learn how to interface shift register with Arduino Uno. I will explain two basic examples to explain all the basics. In the first example, you will learn how to light up all the LEDs at the same time using the shift register. Meanwhile, In the second example, you will learn how bitwise operation can be performed using the shift register to light up random LEDs.
What are the types of Shift registers?
There are two main types of shift registers, a serial in parallel out (SIPO) or a parallel in serial out (PISO).
Serial in parallel out (SIPO): The SIPO type of shift register has a single input and multiple outputs. The data is shifted in serially one bit at a time, but the output shifted parallel, all at the same time. The 74HC595 is a standard serial in parallel out shift register.
Parallel in serial out (PISO): The PISO type of shift register has multiple inputs and a single output. The data is shifted in parallelly all at the same time, but the output is updated serially, one bit at a time. The 74HC165 is a standard parallel in serial out shift register.
How does the 74HC595 shift register work?
Internally, the 74HC595 shift register consists of a series of two 8-bit flip-flops (also known as latches) connected in a chain, with each flip-flop storing a single bit of data. These flip-flops are connected in such a way that the data is shifted from one flip-flop to the next each time the clock signal is toggled.
The 74HC595 also has an additional storage register, which holds the data that has been shifted into the shift register. The data in the storage register is updated each time the storage register clock signal is toggled.
When the storage register clock signal is toggled, the data in the storage register is transferred to the output pins (Q0 to Q7) of the 74HC595. This means that the data is output in parallel, all at once, rather than serially.
Pin description of 74HC595 shift register
- GND (Ground) = It is a ground pin.
- VCC (Supply voltage) = It is a power supply pin of 74HC595 shift register that must be connected to 5 volts.
- SER (Serial data input) = This pin is used to input data serially into the shift register. The data is shifted into the register one bit at a time, with the most significant bit (MSB) being shifted in first.
- SRCLK (Shift register clock) = This pin is used to clock the data into the shift register. Each time the SRCLK pin transitions from LOW to HIGH, the data on the SER pin is shifted into the register.
- RCLK (Register clock/ Latch) = This pin is used to clock the data from the shift register to the storage register. When the RCLK pin transitions from LOW to HIGH, the data in the shift register is transferred to the storage register, and the output pins (Q0 to Q7) are updated with the new data.
- SRCLR (Shift Register Clear): This pin is used to clear the shift register. When the SRCLR pin is set to LOW, all the flip-flops in the shift register are reset to their initial state.
- OE (Output Enable) = It is an active low pin. This pin is used to enable or disable the outputs of the 74HC595. When the OE pin is set to HIGH, the outputs are enabled and can be used to control external devices. When the OE pin is set to LOW, the outputs are disabled and are in a high-impedance state.
- QA-QH (parallel Output pins) = These pins are the parallel output pins of the 74HC595. They are updated with new data from the storage register each time the RCLK pin transitions from LOW to HIGH. The output pins can be used to control external devices, such as LEDs or motors.
- QH’ = This pin is an additional output pin that can be used to cascade multiple 74HC595s together. When multiple 74HC595s are cascaded together, the QH’ pin of one IC is connected to the SER pin of the next IC in the chain. This allows you to control even more devices using just a single microcontroller.
This was all about the important features of the shift register, which you must know before using it with Arduino Uno. Now let’s learn how to interface the 74HC595 shift register with Arduino uno.
Required components
Circuit Diagram
According to the wiring diagram first, you need to place the shift register on the breadboard. Afterward, You need to ensure that the shift register ICs are placed on different sides of the breadboard. Now place the LEDs on the breadboard in series with a 470 ohm resistor. You must connect the cathode (short leg) to GND and the Anode(longer leg) of each LED to the shift register’s output pin (from pin 1 to 7) followed by a 470-ohm resistor in series with it. Now you must be wondering what the need is to connect 470-ohm resistors in series with each LED. These resistors are used to protect the LEDs from being overloaded.
After this connect pin 16 (VCC) and pin 10 (SRCLR) to the 5-volt power supply of the Arduino board. The GND pin of Arduino is connected to the pin 8 (GND) and 13 (OE). Next, connect pin 11 (SRCLK), pin 12 (RCLK), and pin 14 (SER) of the shift register to pin 3, pin 4, and pin 2, respectively. Pin 15 (QA) is connected to the first LED from the left-hand side to the Q7 (QH) pin of the shift register.
This was all about wiring everything up. This same circuit connection will be used for both examples.
Example 1
According to the first example, you will learn how to turn ON all 8 LEDs at the same time for a second after that turn off all together, and repeat this cycle until the power is off.
Code1
//This is a basic shift resistor example which will turn on all the 8 LEDs for a second and then turn them off after some delay
const int DataPin=2; // Data pin of 74HC595 shift resistor is connected to pin 2
const int ClockPin=3; // Clock pin of 74HC595 shift resistor is connected to pin 3
const int LatchPin=4; // Latch pin of 74HC595 shift resistor is connected to pin 4
void setup() {
pinMode(DataPin, OUTPUT); // Set pin 2 as output
pinMode(ClockPin, OUTPUT); // Set pin 3 as output
pinMode(LatchPin, OUTPUT); // Set pin 4 as output
}
void loop() {
digitalWrite(LatchPin, LOW); //Sets the pin 4 LOW
shiftOut(DataPin, ClockPin, LSBFIRST, 255); //shift out a byte of data one bit at a time start from Least significnat bit first
digitalWrite(LatchPin, HIGH); //Sets the pin 4 HIGH
delay(1000); //Wait for a sec
digitalWrite(LatchPin, LOW); //Sets the pin 4 LOW
shiftOut(DataPin, ClockPin, LSBFIRST, 0); //shift out a byte of data one bit at a time start from Least significnat bit first
digitalWrite(LatchPin, HIGH); //Sets the pin 4 HIGH
delay(1000); //Wait for a sec
}
Output
Check out the output, it will help you understand how the code is working and how LEDs are performing on the simulator.
Example 2
This example will let you know how to turn on alternate LEDs one by one and then turn them off in the same manner. The code of this example will define how to perform bit-wise operations using a shift register.
The same circuit connection will be used for this example too.
Code 2
//This example turn on alternate LEDs one by one and then turn off them in the same manner.
const int DataPin=2; // Data pin of 74HC595 shift resistor is connected to pin 2
const int ClockPin=3; // Clock pin of 74HC595 shift resistor is connected to pin 3
const int LatchPin=4; // Latch pin of 74HC595 shift resistor is connected to pin 4
byte Data=0; // Define a variable named Data to hold the current state (ON or OFF) of LEDs
void setup() // This function run once when Arduino turns ON
{
pinMode(DataPin, OUTPUT); // Set pin 2 as output
pinMode(ClockPin, OUTPUT);// Set pin 3 as output
pinMode(LatchPin, OUTPUT);// Set pin 4 as output
}
void loop() // This function runs over and over again
{
shiftWrite(1,1); // Turn on the 2nd LED from left hand side by making pin 1 ON
delay(1000); // Wait for 1 sec
shiftWrite(3,1); // Turn on the 4th LED from left hand side by making pin 3 ON
delay(1000); // Wait for 1 sec
shiftWrite(5,1); // Turn on the 6th LED from left hand side by making pin 5 ON
delay(1000); // Wait for 1 sec
shiftWrite(7,1); // Turn on the 8th LED from left hand side by making pin 7 ON
delay(1000); // Wait for 1 sec
shiftWrite(1,0); // Turn off the 2nd LED from left hand side by making pin 1 OFF
delay(1000); // Wait for 1 sec
shiftWrite(3,0); // Turn off the 4th LED from left hand side by making pin 3 OFF
delay(1000); // Wait for 1 sec
shiftWrite(5,0); // Turn off the 6th LED from left hand side by making pin 5 OFF
delay(1000); // Wait for 1 sec
shiftWrite(7,0); // Turn off the 8th LED from left hand side by making pin 7 OFF
delay(1000); // Wait for 1 sec
}
void shiftWrite(int Pin, boolean State) //Initalize the function shiftWrite which has variable Pin that defines pins (0 to 7 from left hand side) and boolean variable defines the state (0/1) of the LEDS
{
bitWrite(Data, Pin, State);
digitalWrite(LatchPin, LOW); //Sets the pin 4 LOW
shiftOut(DataPin, ClockPin, MSBFIRST, Data); //shift out a byte of data one bit at a time start from Most significnat bit first
digitalWrite(LatchPin, HIGH); //Sets the pin 4 HIGH
}
Output
Check out the output, it will help you to understand the Bitwise operation of the shift register easily.
This was all about the Arduino and shift register. Hope you learn something innovative from this tutorial. Try to perform both the codes and learn effectively. Make some changes and try to perform different operations using the shift register. If you have any questions, please let me know in the comments.