The Shift Register / Expansion Module uses a 74HC595 shift register to allow you to drive 8 output channels using 3 pins on your microcontroller.
Module Pinout
Serial OUT: Passes serial data back out to another module. Can be connected to the Serial IN of another module to daisy-chain them together and control even more outputs. Typically left unconnected.
Serial IN: Data sent from the microcontroller to set the state of the output pins. Connect to a digital I/O pin of your microcontroller.
Latch: Also sometimes referred to as the "storage clock" or "latch clock". On a LOW-to-HIGH transition this pin causes the values loaded into the shift register to be applied to the outputs. Connect to a digital I/O pin of your microcontroller.
Clock: On a LOW-to-HIGH transition this pin causes the value currently applied to the Serial IN pin to be read into the shift register, and all other values moved along one position.
Output Enable: Active LOW. When pulled HIGH, all outputs are disabled and placed in a high-impedance state. When pulled LOW, all outputs are enabled. For minimal pin usage this can be tied to GND to permanently enable the outputs, but this will have the effect that they may start in an unknown state at power-up. Connect to a digital I/O pin of your microcontroller if you want to explicitly enable outputs from software, otherwise connect to GND.
Reset: Active LOW. When pulled LOW, the shift register is cleared and all stored values are lost. Pull HIGH for normal operation. For minimal pin usage this can be permanently tied to 5V to disable reset, but this will require you to explicitly set all 8 bits in order to reset the outputs if they are not in a known state. Connect to a digital I/O pin of your microcontroller if you want to be able to reset the state with a single pulse, but generally it's simplest to connect to 5V.
GND: Connect to GND (0V) on your microcontroller.
VCC: Connect to 5V on your microcontroller.
Outputs A-H: Outputs to your other devices. Can be driven LOW, HIGH, or be set into a high-impedance state by setting Output Enable to LOW.
Basic Usage
In this example we have tied Reset to 5V (to permanently enable the module) and Output Enable to GND (to permanently enable the outputs) to minimise the number of pins required on the Arduino.
We've also connected:
Serial IN to Arduino digital I/O pin D2.
Latch to Arduino digital I/O pin D3.
Clock to Arduino digital I/O pin D4.
The Serial OUT connection is not required.
Example Sketch
The 8 outputs of the shift register represent the bit values of a single byte of data. Using the "shiftOut" command we can clock a byte of data (value 0 to 255) to the module, setting the state of each output appropriately. For example, sending a value of "0" decimal will set all the outputs LOW. Sending a value of "255" decimal will set all of the outputs HIGH. Sending a value of 1, 2, 4, 8, 16, 32, 64, or 128 decimal will set only output A, B, C, D, E, F, G, or H HIGH respectively.
The sketch below counts from 0 to 255 and sends each consecutive value to the shift register, waiting for half a second between each value. If you connect the positive (anode) lead of an LED to each output, and connect the negative (cathode) lead of each LED to GND through a resistor of about 680 Ohms, you'll see the binary values being counted up on the LEDs.
int dataPin = 2; int latchPin = 3; int clockPin = 4; void setup() { pinMode(dataPin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); } void loop() { for (int currentValue = 0; currentValue < 256; currentValue++) { // Disable the latch while we clock in data digitalWrite(latchPin, LOW); // Send the value as a binary sequence to the module shiftOut(dataPin, clockPin, MSBFIRST, currentValue); // Enable the latch again to set the output states digitalWrite(latchPin, HIGH); delay(500); } }
If you want to be able to manipulate individual outputs from within your sketch without affecting the state of other outputs, the most convenient method is probably to store the current value of the shift register in a "int" variable (a single byte) and apply bitwise operators to it. The Arduino website has a good introduction to bitwise operators at http://www.arduino.cc/playground/Code/BitMath.