Shift Register 74HC595 Arduino Library
The Shift Register 74HC595 Arduino Library simplifies shift registers usage. It allows you to set single pins of your shift register either high or low, just like normal Arduino pins. It thereby removes the overhead of shifting out bytes that were created with complicated bit-wise operations. Setting for instance the second pin of your shift register would simply look like sr.set(2, HIGH);
Getting Started
Download the library and install it.
Connect the shift register to your Microcontroller as shown in Figure 1.
Open up a new project in the Arduino IDE and import the library with the following code:
#include <ShiftRegister74HC595.h>
Create an instance of the ShiftRegister74HC595
class with:
int numberOfShiftRegisters = 1; // number of shift registers attached in series
int serialDataPin = 0; // DS
int clockPin = 1; // SHCP
int latchPin = 2; // STCP
ShiftRegister74HC595<numberOfShiftRegisters> sr(serialDataPin, clockPin, latchPin);
Note: When using the pins `` and 1
, they cannot be used for the serial connection anymore. If you want to use statements like Serial.println
, you should connect the shift register to other pins, e.g. 2
, 3
, and 4
.
If you want to use multiple shift registers in series, connect them as shown in Figure 2 and increase the numberOfShiftRegisters
variable accordingly. To operate multiple shift registers in parallel, keep numberOfShiftRegisters = 1
and instantiate multiple ShiftRegister74HC595
objects with different pins.
Insert the normal setup
and loop
function.
Congratulations! Now you have additional output pins (Fig. 3 – series resistors are left out for clarify reasons). You can access them with a bunch of methods, some of which are explained below:
Set a specific pin of your shift register on high:
int pin = 0;
sr.set(pin, HIGH);
Set all pins:
// either on high ...
sr.setAllHigh();
// ... or low
sr.setAllLow();
Sets all pins at once (pin 0 low, pin 1 high, …):
uint8_t pinValues[] = { B10101010 };
sr.setAll(pinValues);
Set multiple pins at once when using two shift registers in series:
uint8_t pinValues[] = { B00011000, B10101010 };
sr.setAll(pinValues);
You can also read the current state of a pin:
int pin = 0;
uint8_t state = sr.get(pin); // 0 = LOW, 1 = HIGH
An example sketch can be found here.
Contribute
Since the initial release of the library in November 2014, several people have contributed to the library with pull requests on GitHub. If you miss features or have ideas for improvements please consider contributing them by forking the repository and opening a pull request. Be sure to test the changes thoroughly beforehand.
Example Project
Below is a project which is using the shift register library.