learn 74hc595 shift register tutorial

 74hc595 shift register tutorial 

Hello! In this blog, I will show you how to test a 74hc595 shift register on an arduino uno. Here you can find the introduction, working principle, applications, specificatoins, pinout & explanation, circuit diagram, test code, code explanation, Video Tutorial and data sheet of the 74hc595.

What is a shift registor IC?

Shift register IC made up of a group of flip-flops used to store binary data. A shift register is a device that shifts bits left or right according to its types. There are different types of shift registers, such as PIPO, SIPO, SISO, and PISO. If you want to learn more about the shift register, refer to this link.

74hc595 IC is an 8-bit shift register. It is a type of SIPO(Serial In Parallel Out). It is used to increase the output of a microcontroller (Arduino Uno etc).

Working of 74hc595 shift register?

A 74HC595 has two registers: a shift register and a storage register. A shift register has 16 pins, but we only need three pins to control it, which are DS(Data), SRCLK(Clock), and RCLK(latch).

All outputs of a shift register are low at the start because there are no bits stored in the shift register. A data pin is used to sets the bits of an output pin to HIGH or LOW. Whenever the data pin is high, the bit is 1, and Whenever the data pin is low, the bit is 0. 


A bit will be shifted into a shift register when the clock goes from high to low. Bits will push whenever it's on the rising edge of the clock, but the output will still be low because the bits haven't been stored yet in the storage register.


It is necessary to set the latch pin high to move bits into the storage register. The bits will update as its set in the shift register, for example, 10101101.


If you want to see the visual explanation of 74hc595 working, then watch this YouTube video.

74hc595 specification:

Features Values
Operating Volt 2v - 6v
Power Consumption 80uA
Output Volt Equal to operating Volt
Output Current 35uA
Clock Frequency 25 Mhz at 4.5v*
Shift out frequency 100 Mhz
Cascading Possible

Aplication of 74hc595 shift Register:

  • Expand the GPIO pin of a microcontroller.
  • LED Matrix/Cube Projects.
  • Interface LCD.
  • Cascading applications.
  • High logic level controller.

74HC595 Shift Registor Pinout:

74hc595 pinout


 1. VCC pin:  
This pin is used to power the IC.

 2. GND pin : 
Should be connected to the ground.

 3. Q0 - Q7 pin: 
These are 8 output pin of shift registers.

 4. Q7’ pin: 
It's used to add more shift registers by connecting Q7' pin to other ShiftRegister DS pin and send the same clock signal. With this technique, control as many LEDs as you like.

 5. DS pin (Serial Data Input):
This pin is used to send data into the shift register.

 6. SRCLK pin (Shift Register Clock):
When it's high, this means the data shifted to the shift register.

 7. RCLK pin (storage Register Clock / Latch):
When it's high, the data will be updated and it's will move to the output pin.

 8. SRCLR pin (Shift Register Clear):
It's used to reset the shift register.
          a. When the pin is LOW means reset.
    b. When the pin is high means no reset.

 9. OE pin (Output Enable):
This pin is used to turn on or off the output.
    a. When the pin is LOW means turn on the outputs.
    b. When the pin is high means turn off the outputs.

Note: OE pin is also use as a pwm controller of all output.

 Component Requirements: 

Let gathering the component to build the arduino 74hc595 test circuit.
1. Arduino Uno to the control shift register.
2. A shift registers 74hc595 for controlling the LEDs.
3. 8 LEDs as an output.
4. 8 resistors to protect the LEDs.
5. Breadboard to make prototype circuit.
6. Jumper wires for connection.

 74hc595 circuit diagram:

Arduino 74hc595 circuit

1. Firstly, make the connection between the Arduino and the breadboard.
  • The Arduino 5v pin should be connected to the 5v side of the breadboard.
  • The Arduino GND pin should be connected to the GND of the breadboard.
  • The power sides of breadboard should be connected together as well.
2. Make the connection between 74hc595 IC and breadboard.
  • A74hc595 VCC (pin 16) and SRCLR (pin 10) are connected the breadboard 5v side.
  • A 74hc595 GND (pin 8) and OE (pin 13) are connected the breadboard GND side.
3. Make a connection between Arduino uno and 74hc595 shift register.
  • Connect the 74hc595 SRCLK(pin 11) to pin 2 of Arduino.
  • Connect the 74hc595 RCLK (pin 12) to pin 3 of Arduino.
  • Connect the 74hc595 DS (pin 14) to pin 4 of Arduino.
4. Attach 8 LEDs on the breadboard.
  • LEDs GND pin is connected to the GND side of the breadboard.
  • Protect all LEDs from high current by adding a 220-ohm resistor to their positive terminals.
5. Make a connection between of 74hc595 output pin to the LEDs.
  • The 74hc595 Q0 (pin 15) should be connected to the end of the LED 1 resistor. 
  • The 74hc595 Q1 (pin 1) should be connected to the end of the LED 2 resistor.
  • The 74hc595 Q2 (pin 2) should be connected to the end of the LED 3 resistor.
  • The 74hc595 Q3 (pin 3) should be connected to the end of the LED 4 resistor.
  • The 74hc595 Q4 (pin 4) should be connected to the end of the LED 5 resistor.
  • The 74hc595 Q5 (pin 5) should be connected to the end of the LED 6 resistor.
  • The 74hc595 Q6 (pin 6) should be connected to the end of the LED 7 resistor.
  • The 74hc595 Q7 (pin 7) should be connected to the end of the LED 8 resistor.
Important: Recheck the connection with the circuit diagram and pinout.

The circuit has now been completed.
6. Let's get started with coding.
7. Launch the Arduino IDE and paste the code.

 74hc595 Arduino Code: 
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
//How many of the shift registers - change this
#define number_of_74hc595s 1

//do not touch
#define numOfRegisterPins number_of_74hc595s * 8

boolean registers[numOfRegisterPins];

void setup() {
  pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}
void loop() {
  for (int i = 0; i < 8; i++) {
    registersWrite(i, HIGH);
    delay(1000);
  }
  for (int i = 7; i >= 0; i--) {
    registersWrite(i, LOW);
    delay(1000);
  }
}

////////////function//////////////
void registersWrite(int index, int value) {
  digitalWrite(latchPin, LOW);
  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    digitalWrite(clockPin, LOW);
    int val = registers[i];
    digitalWrite(dataPin, val);
    digitalWrite(clockPin, HIGH);
  }
  digitalWrite(latchPin, HIGH);
  registers[index] = value;
  delay(5);
}
1. Plug your Arduino uno into your computer via usb cable.
2. In the arduino software goto tool --> then select your board and COM port.
3. Upload the code.
4. After uploading is successfully, you can see LEDs is turning on one by one and then off one by one.

Visit this link to see 18 LEDs effects.


74hc595 Code Explanation: 

Firstly, define the controller pins of the 74hc595 shift register ic.
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;

Define the number of shift register are we using in the project. For example, if you are using 2 shift register then change it to 2.
#define number_of_74hc595s 1

This line counts the number of output pins. Do not Change it.
#define numOfRegisterPins number_of_74hc595s * 8

It will create an arrary of boolean values with a size of numofRegisterPins.  
For example:  registers[8] = {0,0,0,0,0,0,0,0}
boolean registers[numOfRegisterPins];

In void setup(), The code runs once, and we need to define the shift register controller pins of  as OUTPUT.
  pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

First, Let's understand the function.
void registersWrite(int index, int value) {
  digitalWrite(latchPin, LOW);
  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    digitalWrite(clockPin, LOW);
    int val = registers[i];
    digitalWrite(dataPin, val);
    digitalWrite(clockPin, HIGH);
  }
  digitalWrite(latchPin, HIGH);
  registers[index] = value;
  delay(5);
}
The registerWrite() takes two arguments, index and value.
  • Index - index of a Led.
  • Value- Use to store state of led; high(1) or low(0). 
For example: registerWrite(3, 1);  to turned on the four led.

digitalWrite(latchPin, LOW);
The latchPin should be low because we need to receive the bits first. 

In the for loop we need to make the clockPin as a low because we need to received the bits first.
digitalWrite(clockPin, LOW);

The val variable store the current bit, which is zero by default.
int val = registers[i];

When the clockpin is high, the dataPin is used to pass the val to the shift register . 
digitalWrite(dataPin, val);
digitalWrite(clockPin, HIGH);

The for loop runs numofRegister-1 in which our case will be 8 times. Therfore shift register bits is 00000000.
By setting latchpin to High, we can then update the bits in the storage register. The leds are off at this point because the bits is 00000000.

Here, we can set the bits, such as index of leds that should be on or off.
registers[index] = value;


Lets take an example: 
registerWrite(5, 1) which means led 6 will turn on.
For loop run 8 times and the bits are 00000000 at the start, then updated by the latch.
Then we set the bits: registers[5] = 1, which means the array will be 00100000 and then it will update again and the 6 led will turn on.

Back to code:
In void loop(), The code runs again and again. Here we need to call the function. registerWrite(pin, State);
The first for loop use to turned on the Leds one by one from left side with the delay of 1 second.
for (int i = 0; i < 8; i++) {
    registersWrite(i, HIGH);
    delay(1000);
  }
The second for loop use to turn off the Leds one by one from right side with the delay of 1 second.
for (int i = 7; i >= 0; i--) {
    registersWrite(i, LOW);
    delay(1000);
  }

 74hc595 Project Tutorial: 

  
                       


Wrapping up:

I hope this information helps you to test the 74hc595 shift register ic by arduino uno. If you have any problem query free feel to comment it. Will reply it as soon as possible. 

Thank you for reading!


TAGS: 74hc595 8 LEDs tutorial, 74hc595 arduino, arduino 74hc595, arduino shift register, arduino shift register projects, 74hc595 pinout, 74hc595 projects. 


Post a Comment

Previous Post Next Post