Interfacing of Arduino with 74HC595 & 74HC165

Hello friends, I hope you all are doing great. In today's tutorial, I am going to show you How to Interface Arduino with 74HC595 & 74HC165. I have already interfaced these shift registers separately with Arduino. In the first tutorial we have seen Arduino 74HC595 Interfacing in which I have discussed How to increase the output pins of Arduino using 74HC595. After that in second tutorial we have seen Arduino 74HC165 Interfacing where we have increased the input pins of Arduino. So, now we are gonna interface both of these shift registers with Arduino UNO and will increase both input and output pins of Arduino. I have also given the Proteus simulations for download at the end of this tutorial along with Arduino code. So, lets get started with Interfacing of Arduino with 74HC595 & 74HC165:

Interfacing of Arduino with 74HC595 & 74HC165

  • As you can see in above figure, I have used 74HC165 & 74HC595 and interfaced its pins with Arduino UNO.
  • I could use same clock for these shift registers but it would have made the code quite complex.
  • That's why I have used separate clock pins and I have used the below code to reflect the input on output.
#define NUMBER_OF_SHIFT_CHIPS   1
#define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8
#define TotalIC 2
#define TotalICPins TotalIC * 8

int LoadPin    = 8;
int EnablePin  = 9;
int DataPin    = 11;
int ClockPin   = 12;

int RCLK = 5;
int SER = 6;
int SRCLK = 7;

unsigned long pinValues;
unsigned long oldPinValues;
boolean Data[TotalICPins];

void setup()
{
    Serial.begin(9600);

    pinMode(LoadPin, OUTPUT);
    pinMode(EnablePin, OUTPUT);
    pinMode(ClockPin, OUTPUT);
    pinMode(DataPin, INPUT);

    digitalWrite(ClockPin, LOW);
    digitalWrite(LoadPin, HIGH);
    Serial.println("Visit us at www.TheEngineeringProjects.com");
    Serial.println();
    pinMode(SER, OUTPUT);
    pinMode(RCLK, OUTPUT);
    pinMode(SRCLK, OUTPUT);

    ClearBuffer();
    
    pinValues = read_shift_regs();
    print_byte();
    oldPinValues = pinValues;
}

void loop()
{
    pinValues = read_shift_regs();

    if(pinValues != oldPinValues)
    {
        print_byte();
        oldPinValues = pinValues;
    }

}

unsigned long read_shift_regs()
{
    long bitVal;
    unsigned long bytesVal = 0;

    digitalWrite(EnablePin, HIGH);
    digitalWrite(LoadPin, LOW);
    delayMicroseconds(5);
    digitalWrite(LoadPin, HIGH);
    digitalWrite(EnablePin, LOW);

    for(int i = 0; i < DATA_WIDTH; i++)
    {
        bitVal = digitalRead(DataPin);
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

        digitalWrite(ClockPin, HIGH);
        delayMicroseconds(5);
        digitalWrite(ClockPin, LOW);
    }

    return(bytesVal);
}

void print_byte() { 
  byte i; 

  Serial.println("*Shift Register Values:*\r\n");

  for(byte i=0; i<=DATA_WIDTH-1; i++) 
  { 
    Serial.print("P");
    Serial.print(i+1);
    Serial.print(" "); 
  }
  Serial.println();
  for(byte i=0; i<=DATA_WIDTH-1; i++) 
  { 
    
    Serial.print(pinValues >> i & 1, BIN); 
    Data[i] = pinValues >> i & 1, BIN;
    //if(BinaryValue == 1){Data[i] = HIGH;}
    //if(BinaryValue == 0){Data[i] = LOW;}
    UpdateData();
    if(i>8){Serial.print(" ");}
    Serial.print("  "); 
    
  } 
  
  Serial.print("\n"); 
  Serial.println();Serial.println();

}

void ClearBuffer()
{
    for(int i = TotalICPins - 1; i >=  0; i--)
    {
       Data[i] = LOW;
    }
    UpdateData();
} 

void UpdateData()
{
   digitalWrite(RCLK, LOW);
   for(int i = TotalICPins - 1; i >=  0; i--)
   {
        digitalWrite(SRCLK, LOW);   
        digitalWrite(SER, Data[i]);
        digitalWrite(SRCLK, HIGH);

  }
  digitalWrite(RCLK, HIGH);
}
  • In the above code, I have used Number_of_Shift_Chips 1 and it means I am using 1 chip each, so in total 2 chips.
  • Now get hex file from Arduino software and upload it in your Proteus software.
  • Run your simulation and if everything goes fine then you will get something as shown in below figure:
  • You can see in above figure that all those LED outputs are ON which has HIGH inputs.
  • I have also attached a Virtual Terminal with Arduino to have a look at the input bits.
  • Now let's add 2 chips of 74HC165 and 74HC959, so design a simple simulation as shown in below figure:
  • Now in your above code change the Number of Shift chips from 1 to 2, as now we are using 2 chips each.
  • Upload your hex file and if everything goes fine then you will get similar results:
  • So, that's how you can easily increase input and output pins of Arduino UNO.
  • I have just designed a simple code but you can work on it and can control these inputs separately as well.
  • You can interface different digital sensors on these input pins and can control motors, relays, solenoids etc. at output pins.
  • You can download both of these Proteus Simulations along with Arduino code by clicking the below button, but I would suggest you to dwsign it on yoru own so that you could learn from mistakes.

[dt_default_button link="https://theengineeringprojects.com/ArduinoProjects/Interfacing%20of%20Arduino%20with%2074HC595%20&%2074HC165.zip" button_alignment="default" animation="fadeIn" size="medium" default_btn_bg_color="" bg_hover_color="" text_color="" text_hover_color="" icon="fa fa-chevron-circle-right" icon_align="left"]Download Proteus Simulation & Arduino Code [/dt_default_button]

So, that was all about Interfacing of Arduino with 74HC595 & 74HC165. I hope you can now easily simulate it. If you have any questions then ask in comments and I will try my best to resolve them. Thanks for reading. Take care !!! :)

Arduino 74HC165 Interfacing: Increase Input Pins

Hello friends, I hope you all are doing great. In today's tutorial, I am going to do an Arduino 74HC165 Interfacing and we will have a look at How to increase Input Pins of Arduino. 74HC165 is a shift register and works on the principal of Parallel In Serial Out. In my previous tutorial Arduino 74HC595 Interfacing: Increase Output Pins, we have seen How to increase the output pins of Arduino and today we are gonna do exact the opposite and we will increase the input pins. 74HC165 will take 8 parallel inputs from different sensors or buttons etc and will send them to serial OUT Pin, which will be connected to Arduino. So, if you are working on a project where you want to get data of 15 or 20 digital sensors then you can use this shift register and just using a single pin of Arduino you can read data of all those sensors. We can only get digital inputs, we can't get analog input through this shift register. So, let's get started with Arduino 74HC165 Interfacing:

Arduino 74HC165 Interfacing

  • I will design a Proteus Simulation of Arduino 74HC165 Interfacing, I have given the files for download at the end of this tutorial, but I would recommend you to design it so that you could learn.
  • I will connect simple Logic buttons with this shift register and will read their status on the Serial Port.
  • So, first of all design a simple Proteus Simulation as shown in below figure.
  • I have used Arduino UNO and have connected Virtual Terminal so that we could have a look at Serial data.
  • As you can see in the above figure that I have connected four pins between Arduino and 74HC165, which are:
    • Pin # 8 of Arduino  ==> Shift (SH) of shift register.
    • Pin # 9 of Arduino  ==> Clock Enable (CE) of shift register.
    • Pin # 11 of Arduino ==> Serial OUT (SO) of shift register.
    • Pin # 12 of Arduino ==> Clock (CLK) of shift register.
  • Now open you Arduino software and copy paste the below code in it:
#define NUMBER_OF_SHIFT_CHIPS   1
#define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8

int LoadPin    = 8;
int EnablePin  = 9;
int DataPin    = 11;
int ClockPin   = 12;

unsigned long pinValues;
unsigned long oldPinValues;

void setup()
{
    Serial.begin(9600);

    pinMode(LoadPin, OUTPUT);
    pinMode(EnablePin, OUTPUT);
    pinMode(ClockPin, OUTPUT);
    pinMode(DataPin, INPUT);

    digitalWrite(ClockPin, LOW);
    digitalWrite(LoadPin, HIGH);

    pinValues = read_shift_regs();
    print_byte();
    oldPinValues = pinValues;
}

void loop()
{
    pinValues = read_shift_regs();

    if(pinValues != oldPinValues)
    {
        print_byte();
        oldPinValues = pinValues;
    }

}

unsigned long read_shift_regs()
{
    long bitVal;
    unsigned long bytesVal = 0;

    digitalWrite(EnablePin, HIGH);
    digitalWrite(LoadPin, LOW);
    delayMicroseconds(5);
    digitalWrite(LoadPin, HIGH);
    digitalWrite(EnablePin, LOW);

    for(int i = 0; i < DATA_WIDTH; i++)
    {
        bitVal = digitalRead(DataPin);
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

        digitalWrite(ClockPin, HIGH);
        delayMicroseconds(5);
        digitalWrite(ClockPin, LOW);
    }

    return(bytesVal);
}

void print_byte() { 
  byte i; 

  Serial.println("*Shift Register Values:*\r\n");

  for(byte i=0; i<=DATA_WIDTH-1; i++) 
  { 
    Serial.print("P");
    Serial.print(i+1);
    Serial.print(" "); 
  }
  Serial.println();
  for(byte i=0; i<=DATA_WIDTH-1; i++) 
  { 
    Serial.print(pinValues >> i & 1, BIN); 
    
    if(i>8){Serial.print(" ");}
    Serial.print("  "); 
    
  } 
  
  Serial.print("\n"); 
  Serial.println();Serial.println();

}
  • The code is quite simple but let me give you a quick explanation of  it.
  • First of all, I have assigned names to all 4 pins of 74HC165 connected with Arduino.
  • Function read_shift_regs() is used to read the eight input pins of 74HC165 and print_byte() function is used to display that data on Serial Monitor.
  • So get your hex file from Arduino software and upload it in Proteus software.
  • Run your Proteus simulation and if everything goes fine then you will get results as shown in below figure:
  • If you change any input of your shift register then you will get the new value on your Virtual Terminal.
  • Now let's add another 74HC165 and increase our input pins by 16.
  • So, design a simple circuit as shown in below figure:
  • Now, in the above code, simply change the first line and make #define NUMBER_OF_SHIFT_CHIPS 2.
  • Simply changes 1 to 2, as we are using 2 shift registers now.
  • Now get your hex file and run the Proteus simulation.
  • Here's the output of our 16 increased inputs:
  • That's how you can easily interface multiple 74HC165 chips with your Arduino board and can increase the input options.
  • You can download these Proteus simulations and code for Arduino 74HC165 Interfacing by clicking the below button:

[dt_default_button link="https://www.theengineeringprojects.com/ArduinoProjects/Arduino 74HC165 Interfacing.rar" button_alignment="default" animation="fadeIn" size="medium" default_btn_bg_color="" bg_hover_color="" text_color="" text_hover_color="" icon="fa fa-chevron-circle-right" icon_align="left"]Download Proteus Simulation & Code[/dt_default_button]

  • You should also have a look at this video in which I have shown How to run these simulations:
So, that was all for today. In my coming tutorial, I will interface both 74HC165 and 74HC595 with Arduino UNO and will show you How to increase both input and output pins at the same time. Thanks for reading. Take care!!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir