Skip to Content

Interfacing Parallel In Serial Out Shift Register 74HC165 With Arduino

Interfacing Parallel In Serial Out Shift Register 74HC165 With Arduino

In this article, I will present all the necessary details to understand the operation of the Parallel In Serial Out (PISO) shift register.

Using the PISO shift register, you can read inputs from multiple switches with fewer Arduino pins. 

At the end of this article, you will have all the details needed to build your parallel to serial systems.

The applications where you use PISO 74HC165 are Counting, shifting, multiple switches, and keypad matrix. 

The following section reviews the shift register’s working, inputs, and pin descriptions.

Later, we will see some working Arduino examples where you can quickly learn to code Arduino to interact with the shift registers. 

In the last sections, I will bring the frequently asked questions about the 74HC165 Parallel In Serial Out register. 

Let’s get started!

Components Needed To Build Project With A Shift Register

Hardware Components 

Software

Quick Tip: Buy the shift register ICs which come in DIP. It is easier to connect these to the breadboard.

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

What Is A Parallel In Serial Out Shift Register – 74HC165?

The 74HC165 is an 8-bit Parallel In Serial Out(PISO) shift register. You can use PISO to read the status of 8 pins using only three pins on the Arduino UNO.

The shift register 74HC165 is used to increase the number of input pins for Arduino. The IC can be cascaded to scale the number of inputs without a further increase in the control pins used by the Arduino.

The 74HC165 IC has eight input parallel pins. The IC will sample all the input pins simultaneously and provides the sampled data serially to the Arduino.

Let us quickly glance at the key components and functional modes of an 8-bit shift register from TI.

The IC 74HC165 has two modes:

Sample mode: The eight pins sample the input data in this mode. This is also called Load mode. You will have a dedicated input pin for the IC, which you have to control. 

The voltage level on the pin defines whether the IC operates in Shifting Mode or Sampling mode. In most ICs, a logic Low is required on the pin to put the shift register into sampling mode. 

Shift mode: In this mode, the latched 8-bit data will be sent out for every clock pulse on the clock input pin of the shift register. 

The MSB bit will come first. DATA7 first comes on the DOUT pin. For the second pulse, the data on DATA6 comes on the DOUT pin.

Finally, you will get the information on the DATA0 pin for the eight-clock pulse. 

In most ICs, a logic High is required on the pin to put the shift register into shifting mode.

One crucial point is that when you are in shifting mode, the changes happening on the eight input pins will not be loaded into the IC.

The changing data on the input pins have no effect when the IC is in shifting mode.

Pin Details Of An 8-bit PISO Shift Register

The shift registers will have different pin labels depending on the manufacturers. Let us go through one such datasheet from TI.

Pin labelPin Description
AParallel input data bit 0
BParallel input data bit 1
CParallel input data bit 2
DParallel input data bit 3
EParallel input data bit 4
FParallel input data bit 5
GParallel input data bit 6
HParallel input data bit 7
ClockClock pin – Every pulse on this pin shifts the latched data on the data pin
Clock inhibitClock inhibit pin – when this pin is active, the clock pin is disabled. Any pulses on the clock pin will be ignored. 
QSerial data output pin
Q#Serial data output pin (inverted)
SERSerial input
SH/LD#Shift or load input – When you drive this pin high, the IC will be in shifting mode. When you drive this low, the data will be loaded onto the parallel input pins.
VCCPower Pin (5 V)
GNDGround connections

What Pins Of 74HC165 Are Needed To Connect To Arduino?

At a minimum, you need only three pins on the Arduino to connect to the shift register. The three pins are the Serial data pin, the clock pin, and the latch pin.

Here is a snippet from a project where you read the status of 8 switches using only three Arduino UNO pins.

8 push buttons using only three Arduino UNO pins

Feel free to remap the pins based on your needs. The Arduino sketch allows you to easily change the pin mapping.

const int dataPin = 2;   /* Q7 */
const int clockPin = 3;  /* CP */
const int latchPin = 4;  /* PL */

In the below table, I have summarised the pin mapping.

Arduino PinShift Register PinDescription
2QSerial data Input pin. This is the output pin for the Shift register and input pin for the Arduino
3CLKShift Register Clock Input. This is the output pin for the Arduino and input for the shift register
4SH/LD#Shift or load control pin – Output pin of the Arduino

Step-By-Step Instructions To Connect PISO Shift Register To An Arduino

This section will provide step-by-step connections to complete a working 8-input Arduino project using the 74HC165 shift register. Let’s begin

Project 1: 8-bit Shift Register And Arduino To Read 8 Switches

The final connection diagram needed to complete the project is shown below.

The final connection diagram

Step 1: Start with the Arduino UNO

Start with the Arduino. We will need one Arduino UNO, one  PISO Shift register, 8 slide switches, and wires.

Start with the Arduino UNO

Step 2: Connect GND pins

The PIN 1 indicator is shown in the below image. Start from PIN 1 and count in the anticlockwise direction to find pin 8.

The GND pin is the 8th pin on the shift register.

There are several GND pins available on the Arduino UNO. Choose the first one.

Connect GND pins
GND pins available on the Arduino UNO

Step 3: Connect the Shifter / Load Pin

Connect the SH/LD# pin (sometimes called latch pin) of the shift register to PIN 4 of the Arduino.

Connect the Shifter

Step 4: Connect the Serial Clock line

Connect the CLK pin of the shift register to PIN 3 of the Arduino.

Connect the Serial Clock line

Step 5: Connect the Serial Data Output line

Connect the Q pin (data output) of the shift register to the PIN 2 of the Arduino.

Connect the Serial Data Output line

Step 6: Connect the first slide switch

Connect the slider switch to the shift register. One of the pins of the slide switch goes to VCC, and the other one goes to the GND pin.

Hence, depending on the slider’s position, the digital signal will be either logic one or 0.

Connect the Serial Data Output line

Step 7: Complete the remaining slider connections

By end of this step, you have connected all 8 input pins of the shift register to the 8 sliders.

Complete the remaining slider connections

Step 8: The Arduino Code

Here is the link for the Arduino sketch. The code below is all you need to read the 8 inputs from the sliders.

const int dataPin = 2;   /* Q7 */
const int clockPin = 3;  /* CP */
const int latchPin = 4;  /* PL */
 
const int numBits = 8;   /* Set to 8 * number of shift registers */
 
void setup() {
  Serial.begin(115200);
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
}
 
void loop() {
  // Step 1: Sample
  digitalWrite(latchPin, LOW);
  digitalWrite(latchPin, HIGH);
 
  // Step 2: Shift
  Serial.print("Bits: ");
  for (int i = 0; i < numBits; i++) {
    int bit = digitalRead(dataPin);
    if (bit == HIGH) {
      Serial.print("1");
    } else {
      Serial.print("0");
    }
    digitalWrite(clockPin, HIGH); // Shift out the next bit
    digitalWrite(clockPin, LOW);
  }
 
  Serial.println();
  delay(1000);
}

Project 2: Read 32 Switches Using 3 Arduino Pins

In this project, you will read the status of the 32 slide switches using only three Arduino pins. This is a bit of an exaggeration but practically a working project. This example shows two things.

  1. How to cascade two or more shift registers
  2. How to read multiple slide inputs using shift registers
Read 32 Switches Using 3 Arduino Pins

Step 1: The Connection Diagram

You can follow similar steps as in project 1 to complete the connection. You can refer to the link here to get access to the wiring diagram in a better resolution. 

Step 2: The Arduino Code to read 32 slide switch inputs

const byte latchPin = 9;        // to latch the inputs into the registers
const byte clockPin = 13;       // I choose the SCK pin
const byte dataPin = 12;        // I choose the MISO pin
uint32_t oldOptionSwitch = 0;   // previous state of all the inputs

const int pulseWidth = 10;      // pulse width in microseconds

void setup ()
{
  Serial.begin( 115200);
  Serial.println( "Turn on and off the switches");
  Serial.println( "Top row is switch 0 (right) to switch 7 (left)");
  Serial.println( "Second row is 8 to 15, and so on");

  pinMode( clockPin, OUTPUT);   // clock signal, idle LOW
  pinMode( latchPin, OUTPUT);   // latch (copy input into registers), idle HIGH
  digitalWrite( latchPin, HIGH);
}

void loop ()
{
  // Give a pulse to the parallel load latch of all 74HC165
  digitalWrite( latchPin, LOW);    
  delayMicroseconds( pulseWidth);
  digitalWrite( latchPin, HIGH);

  // Reading one 74HC165 at a time and combining them into a 32 bit variable
  // The last 74HC165 is at the bottom, but the switches start numbering
  // at the top. So the first byte has to be shifted into the highest place.
  uint32_t optionSwitch = 0;
  for( int i=24; i>=0; i-=8)
  {
    optionSwitch |= ((uint32_t) ReadOne165()) << i;
  }

  for( int i = 0; i<32; i++)
  {
    if( bitRead( optionSwitch, i) != bitRead( oldOptionSwitch,i))
    {
      Serial.print( "Switch ");
      if( i < 10)
        Serial.print( " ");
      Serial.print( i);
      Serial.print( " is now ");
      Serial.println( bitRead( optionSwitch, i) == 0 ? "down ↓" : "up   ↑");
    }
  }
 
  oldOptionSwitch = optionSwitch;
  delay( 25);      // slow down the sketch to avoid switch bounce
}

// The ReadOne165() function reads only 8 bits,
// because of the similar functions shiftIn() and SPI.transfer()
// which both use 8 bits.
//
// The shiftIn() can not be used here, because the clock is set idle low
// and the shiftIn() makes the clock high to read a bit.
// The 74HC165 require to read the bit first and then give a clock pulse.
//
byte ReadOne165()
{
  byte ret = 0x00;

  // The first one that is read is the highest bit (input D7 of the 74HC165).
  for( int i=7; i>=0; i--)
  {
    if( digitalRead( dataPin) == HIGH)
      bitSet( ret, i);

    digitalWrite( clockPin, HIGH);
    delayMicroseconds( pulseWidth);
    digitalWrite( clockPin, LOW);
  }

  return( ret);
}

FAQs About The Serial In Parallel Out Shift Register And Arduino

In this section, I have answered the most frequent questions about the PISO (Parallel In Serial Out) shift register and Arduino. If you have more questions, please post them in the comments.  

1) Is 74HC165 a microcontroller?

No. The 74HC165 is a Parallel In Serial Out Shift register used to read multiple inputs using very few Arduino/MCU pins.

2) What can I do with a 74HC165?

You can do a lot of things with the 74HC165 shift register IC. 

  • General purpose input
  • Parallel to serial data conversion
  • Capture data and hold the data (latching) for long intervals
  • Industrial control, automation, etc.

3) How do Multiple 74HC165 work in cascade?

A cascade connection is one where you use multiple shift registers with only one Arduino. You have to make the connections in the following manner.

Connect the MSB (D7/H) pin to the Serial input pin of the next shift register. Connect the D7/H data input pin of the last unit to the microcontroller.

The latch/hold pin and the clock pin are shared with all the shift registers. Hence, you only need three pins to control more than 8 inputs when you use shift registers.

Conclusion

I am hoping that the article was helpful for you in learning about the PISO shift registers.

I hope you will continue building more useful Arduino and shift register projects in the future. 

This article covered the basic working principle, pin definitions, and critical information on the IC’s clock, shifting, and sampling status.

We also saw different projects with varying complexity.

I will be excited to learn about projects based on the shift registers you build. Please let me know in the comments section.

Also, it would be awesome if you could suggest any improvements to the article, I will be glad to accept all constructive feedback. 

Which topic would be more interesting to read? Let me know in the comments.

Please share this article with fellow Arduino enthusiasts.

Andrew

Wednesday 14th of February 2024

Worked it out by using pull-down resistors. It would be worth mentioning this.

Stefan Maetschke

Wednesday 14th of February 2024

Good point. Will add that. Thanks!

Andrew

Wednesday 14th of February 2024

I tried this with momentary push-buttons instead of slider switches, but the results were inaccurate. Is there anything special required to change this for push-buttons ?

Ezra

Saturday 15th of July 2023

I'm trying to use a single shift register for reading keypad inputs is it possible?