Introduction
Arduino is a popular open-source electronic platform that allows users to create a variety of projects with ease. However, sometimes the limited number of digital output pins can limit the functionality of these projects. The 74HC595 shift register is a simple and cost-effective solution to this problem, allowing for the expansion of the number of digital output pins available to Arduino.
In this article, we will explore how to interface the “74HC595 shift register” with Arduino and how it can be used to expand the number of digital output pins.
What is the 74HC595 Shift Register?
The 74HC595 is a popular serial-in, parallel-out shift register that allows users to expand the number of digital output pins on a microcontroller or other digital device. It has eight output pins that can be individually controlled using only three pins from the microcontroller: data, clock, and latch. By serially shifting data to the shift register, the user can control up to eight outputs using only three pins from the microcontroller, freeing up the remaining pins for other purposes.
Hardware Components
To interface 74hc595 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 | 74HC595 | 1 |
LED | – | 8 |
Jumper Wires | – | 1 |
74HC595 Shift Register Pinout
Pin label | Pin Description |
---|---|
Q0 | Parallel output data bit 0 |
Q1 | Parallel output data bit 1 |
Q2 | Parallel output data bit 2 |
Q3 | Parallel output data bit 3 |
Q4 | Parallel output data bit 4 |
Q5 | Parallel output data bit 5 |
Q6 | Parallel output data bit 6 |
Q7 | Parallel output data bit 7 |
GND | Ground connection |
Q7S | Serial clock output pin. This pin is needed when you cascade two or more shift registers to increase the number of outputs further. |
MR# | Master Reset Input. This is an active low-input pin. When you drive a low on this pin, the data bits reset to zero (this pin is independent of the clock) |
SHCP | Shift Register Clock Input |
STCP | Storage Register Clock Output |
OE# | Output Enable Input |
DS | Serial Data Input |
VCC | Supply Voltage |
74HC595 Shift Register Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | 74HC595 |
---|---|
5V | VCC |
GND | GND |
D2 | DS |
D4 | ST-CP |
D3 | SH-CP |
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.
int datapin = 2;
int clockpin = 3;
int latchpin = 4;
// We'll also declare a global variable for the data we're
// sending to the shift register:
byte data = 0;
void setup()
{
// Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop()
{
// To try the different functions below, uncomment the one
// you want to run, and comment out the remaining ones to
// disable them from running.
oneAfterAnother(); // All on, all off
// oneOnAtATime(); // Scroll down the line
// pingPong(); // Like above, but back and forth
// randomLED(); // Blink random LEDs
// marquee();
// binaryCount(); // Bit patterns from 0 to 255
}
void shiftWrite(int desiredPin, boolean desiredState){
// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().
bitWrite(data,desiredPin,desiredState); //Change desired bit to 0 or 1 in "data"
// Now we'll actually send that data to the shift register.
// The shiftOut() function does all the hard work of
// manipulating the data and clock pins to move the data
// into the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data); //Send "data" to the shift register
//Toggle the latchPin to make "data" appear at the outputs
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void oneAfterAnother()
{
// This function will turn on all the LEDs, one-by-one,
// and then turn them off all off, one-by-one.
int index;
int delayTime = 100; // Time (milliseconds) to pause between LEDs
// Make this smaller for faster switching
// Turn all the LEDs on
for(index = 0; index <= 7; index++)
{
shiftWrite(index, HIGH);
delay(delayTime);
}
// Turn all the LEDs off
for(index = 7; index >= 0; index--)
{
shiftWrite(index, LOW);
delay(delayTime);
}
}
void oneOnAtATime()
{
// This function will turn the LEDs on and off, one-by-one.
int index;
int delayTime = 100; // Time (milliseconds) to pause between LEDs
// Make this smaller for faster switching
// step through the LEDs, from 0 to 7
for(index = 0; index <= 7; index++)
{
shiftWrite(index, HIGH); // turn LED on
delay(delayTime); // pause to slow down the sequence
shiftWrite(index, LOW); // turn LED off
}
}
void pingPong()
{
// This function turns on the LEDs, one at a time, in both directions.
int index;
int delayTime = 100; // time (milliseconds) to pause between LEDs
// make this smaller for faster switching
// step through the LEDs, from 0 to 7
for(index = 0; index <= 7; index++)
{
shiftWrite(index, HIGH); // turn LED on
delay(delayTime); // pause to slow down the sequence
shiftWrite(index, LOW); // turn LED off
}
// step through the LEDs, from 7 to 0
for(index = 7; index >= 0; index--)
{
shiftWrite(index, HIGH); // turn LED on
delay(delayTime); // pause to slow down the sequence
shiftWrite(index, LOW); // turn LED off
}
}
void randomLED()
{
// This function will randomly turn on and off LEDs.
int index;
int delayTime = 100; // time (milliseconds) to pause between LEDs
// make this smaller for faster switching
index = random(8); // pick a random number between 0 and 7
shiftWrite(index, HIGH); // turn LED on
delay(delayTime); // pause to slow down the sequence
shiftWrite(index, LOW); // turn LED off
}
void marquee()
{
// This function will mimic "chase lights" like those around signs.
int index;
int delayTime = 200; // Time (milliseconds) to pause between LEDs
// Make this smaller for faster switching
// Step through the first four LEDs
// (We'll light up one in the lower 4 and one in the upper 4)
for(index = 0; index <= 3; index++)
{
shiftWrite(index, HIGH); // Turn a LED on
shiftWrite(index+4, HIGH); // Skip four, and turn that LED on
delay(delayTime); // Pause to slow down the sequence
shiftWrite(index, LOW); // Turn both LEDs off
shiftWrite(index+4, LOW);
}
}
void binaryCount()
{
// This function creates a visual representation of the on/off pattern
// of bits in a byte.
int delayTime = 1000; // time (milliseconds) to pause between LEDs
// make this smaller for faster switching
// Send the data byte to the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data);
// Toggle the latch pin to make the data appear at the outputs:
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
// Add one to data, and repeat!
// (Because a byte type can only store numbers from 0 to 255,
// if we add more than that, it will "roll around" back to 0
// and start over).
data++;
// Delay so you can see what's going on:
delay(delayTime);
}
Code Explanation
This code is used to control an 8-bit shift register (74HC595) with Arduino to control 8 LEDs using three digital pins. The shift register is used to convert serial input data to parallel output data. This means that it can take data one bit at a time and output all eight bits at once. The datapin
is the pin where the data is being input, the clockpin
is the pin where the shift register listens for a clock pulse to indicate that the next bit of data is available, and latchpin
is the pin that, when pulsed, latches the eight bits of data on the shift register to their respective output pins.
The setup()
function sets the three pins defined earlier as OUTPUT
.
The loop()
function contains different functions, but only one function is active at a time. Depending on which function is currently active, the LED lights will behave differently. The active function is indicated by commenting out the other functions in the loop()
function.
The shiftWrite()
function allows you to set a particular LED by turning the shift register outputs HIGH
or LOW
. The data is stored in a global variable called data
, and the shiftOut()
function is used to send the data to the shift register. After the data is sent, the latchpin
is toggled to display the data on the output pins.
The functions in the code control the LEDs in different ways:
oneAfterAnother()
turns all the LEDs on, then off, one by one.oneOnAtATime()
turns on and off the LEDs, one by one.pingPong()
turns on the LEDs one by one, in both directions.randomLED()
turns a random LED on and off.marquee()
mimics chase lights like those around signs.binaryCount()
creates a visual representation of the on/off pattern of bits in a byte.
Applications
Here are some applications of the 74HC595 shift register:
- Driving multiple LEDs or other simple digital outputs with fewer I/O pins than required by directly connecting each device to the microcontroller.
- Building large, multiplexed displays such as seven-segment displays, dot-matrix displays, or scrolling LED signs.
- Controlling multiple servo motors or other PWM outputs using a single microcontroller timer.
- Controlling large arrays of relays, solenoids, or other high-power outputs with fewer I/O pins.
- Multiplexing buttons or other inputs to reduce the number of I/O pins required for reading all of the inputs.
Conclusion
The 74HC595 shift register with Arduino is an excellent way to expand the number of digital output pins available for your projects. By following the steps outlined in this article, you can easily connect the shift register to your Arduino and start controlling multiple outputs with a few simple commands.