Introduction
In the world of electronics, shift registers are essential components that facilitate the transfer of data between various digital devices. One such shift register is the 74HC165, which is widely used due to its versatility and ease of interfacing.
In this article, we will explore how to interface the “74HC165 shift register with an Arduino” microcontroller, enabling you to expand the number of input pins available for your projects.
What is the 74HC165 Shift Register?
The 74HC165 is a parallel-in, serial-out shift register that is widely used in digital electronics. It has eight parallel input pins (D0-D7) that can be used to read the state of eight digital signals simultaneously. The 74HC165 also has two control pins, one for clock input (CLK) and the other for serial data output (Q7′). By sending pulses to the CLK pin, the eight parallel inputs can be shifted in and read out serially through the Q7′ pin. The 74HC165 shift register can be daisy-chained with other shift registers to expand the number of inputs available for reading, making it a versatile and popular component for a wide range of digital projects.
Hardware Components
To interface the 74HC165 Shift register with Arduino, you’ll need the following hardware components to get started:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
Shift Register | 74HC165 | 1 |
Jumper Wires | – | 1 |
74HC165 Shift Register Pinout
Pin label | Pin Description |
---|---|
A | Parallel input data bit 0 |
B | Parallel input data bit 1 |
C | Parallel input data bit 2 |
D | Parallel input data bit 3 |
E | Parallel input data bit 4 |
F | Parallel input data bit 5 |
G | Parallel input data bit 6 |
H | Parallel input data bit 7 |
Clock | Clock pin – Every pulse on this pin shifts the latched data on the data pin |
Clock inhibit | Clock inhibit pin – when this pin is active, the clock pin is disabled. Any pulses on the clock pin will be ignored. |
Q | Serial data output pin |
Q# | Serial data output pin (inverted) |
SER | Serial 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. |
VCC | Power Pin (5 V) |
GND | Ground connections |
74HC165 Shift Register Circuit
Make connections according to the circuit diagram given below.
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
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);
}
Code Explanation
This Arduino code is designed to read data from a chain of 74HC165 shift registers. The code first sets up three pins – dataPin, clockPin, and latchPin – to be used for interfacing with the shift registers. The numBits variable is set to the total number of bits to be read from all the shift registers in the chain.
In the setup() function, the code sets the dataPin to be an input and the clockPin and latchPin to be outputs. The serial communication is initialized with a baud rate of 115200.
In the loop() function, the code executes two steps:
Step 1: Sample – The latchPin is first set to LOW to prepare for sampling the input data. Then, it is set to HIGH to capture the current state of the input pins.
Step 2: Shift – The input data is read from the shift registers by iterating over the numBits variable. The data is read one bit at a time from the dataPin, and the value of each bit is printed to the serial monitor. The clockPin is then pulsed HIGH and LOW to shift the next bit into the shift register. The process repeats until all bits have been read.
Finally, the loop() function adds a delay of 1000 milliseconds before starting the next iteration. The code essentially reads the data from the shift registers in a continuous loop and outputs the data to the serial monitor.
Applications
Here are some applications for the 74HC165 shift register:
- Keyboard matrix scanning
- LED matrix control
- Serial to parallel data conversion
- Button input expansion
- Multiplexed display control
- Data logging from multiple sensors
- Audio level metering
- Motor control
- Robotics and automation systems
- Remote control input processing
Conclusion
Interfacing the 74HC165 shift register with an Arduino microcontroller can greatly enhance the functionality of your digital projects. With the ability to expand the number of input pins available, you can create more complex and sophisticated circuits that were previously impossible.