How to Build a Shift Register Circuit with an Arduino


74HC595 Shift Register



In this project, we are going to build a shift register with an Arduino microcontroller.

A shift register is naturally meant to deal with a microcontroller.

Dealing with a shift register using manual controls is very difficult and impractical. Microcontrollers have much greater speed and execute instructions faster and more efficiently than we can. Therefore, shift registers naturally work with microcontrollers.

Shift registers serve very good purposes to microcontrollers. The 74HC595 shift register can turn on up to 8 outputs. This means it adds additional outputs to a microcontroller. Now we can control even more output devices. Secondly, shift registers work in an arthmetical way in which bits (which represent outputs) can be shifted bit by bit. It works really well for a lot of arithmetical processes. This works well for a lot of different LED devices, such as individual LEDs and also for 7-segment LEDs. You'll see how shift registers can shift bits in the example below.

Components

  • 74HC595 Shift Register
  • 8 LEDs
  • 8 220Ω resistors
  • Arduino


One of the most popular shift registers is the 74HC595 shift register. This type of shift register can control 8 outputs. This means it can control, for example, 8 LEDs.

The shift register, in total, has 16 pins.

The pinout for the shift register is shown below.

74HC595 shift register pinout

The table below summarizes these pin connections.



Pin Description Function
QA-QH Output pins Outputs of the shift register
VCC (Pin 16) Power Positive voltage for the shift register
GND (Pin 8) Power Ground for the shift register
QH' (Pin 9) Serial Out Serial out is used to shift data to another 74HC595 shift register
CLEAR (Pin 10) Master Reclear, active low This sets all the bits in the shift register to 0 or off if pulled LOW.
Shift Clock (Pin 11) Shift Register Clock Pin If pulled HIGH, this shifts all the values in the shift register forward one.
Latch Clock (Pin 12) Stroage Register Lock Pin When pulled HIGH, it outputs the new shift register values.
Output Enable (Pin 13) Output enable, active low This enables the output when grounded and disables it when high.
Serial Data Input (Pin 14) Input for New Serial Data This is the input pin for the new serial data.


The 74HC595 shift register has 8 outputs. This means you can connect up to 8 output devices, such as LEDs or buzzers, to the shift register. The output pins are QA-QH, 8 in total. QA, pin 15, is the first output pin. Next are QB-QH, which are pins 1-7. These output pins do not connect to the arduino microcontroller. Instead, the outputs, which in this case are LEDs, are connected to them.

To provide power to the 74HC595 shift register, we give +5V to VCC and we connect GND to ground on the arduino microcontroller.

When connecting shift registers to microcontrollers, the serial data input line (pin 14) gets connected to digital pin 11 on the arduino. Through pin 11, through our code, we will write the data that we want to be displayed at the output. For example, we may write 11111111 to the serial data line. If this done while the clock is HIGH, then the 1s will be shifted into the storage register. Now the storage register will read 11111111. Once we do the next step, latch it, which means we transfer the data from the storage register into the outputs, it will show up at the output. This means the LEDs will all turn on.

The clock pin (pin 11), is the clock for the shift register. The 74HC595 is clock-driven on the rising edge. This means that in order to shift bits into the storage register of the shift register, the clock must be high. And bits are transferred in on the rising edge of the clock. So the clock must be HIGH when new data is being driven into the storage register.

The latch pin (pin 12) is a very important pin. This pin takes the data that is stored in the storage register and when driven HIGH, transfers the data to the output. So when we initially transfer in data, it goes into the storage register but does not show up at the output. It only shows up at the output when the latch pin is HIGH. This is when we then we see the LEDs turn on. So the latch pin can be seen as like the final step in the process to seeing our results at the output, which in this case are LEDs.

The CLEAR pin is an active low pin. This means that when it is grounded, all data in the storage register is cleared to 0. In order for this not to happen, we keep it at HIGH, so we connect it to VCC.

The Output Enable pin allows us to turn on or off all the outputs. Usually, we want this enabled so that there can changes on the output side. If this is disabled, then no output can be turned on. This pin is an active low pin, which means when it is grounded, all output pins are disabled. So normally we keep this pin HIGH, so it's tied to VCC.

The QH' pin is the serial data output pin. This pin is used if you want to cascade an additional shift register. So instead of being able to switch on 8 outputs, you then will be able to switch on 16 outputs. Since we're not cascading shift registers in this circuit, we simply leave this pin unconnected.



Arduino Shift Register Circuit

The shift register circuit we will build with an Arduino is shown below.

Arduino shift register circuit

The breadboard schematic of the circuit above is shown below.

Arduino shift register circuit breadboard schematic

This shift register circuit has pretty simple connections.

First we connect power. So we connect the VCC of the shift register to the 5V arduino terminal. We connect the ground of the shift register to the GND arduino terminal. This establishes sufficient power to the shift register.

Next we connect the clock pin (pin 11) to digital pin 12 of the arduino.

We connect the latch pin (pin 12) to digital pin 8 on the arduino.

We connect the serial data input line to digital pin 11 on the arduino.

Since the Output Enable pin is active low, we connect it to be ground, because we normally want outputs to be enabled. If outputs are disabled, we can't write anything to outputs. Outputs are permanently disabled unless enabled. Since we do not want them disabled ever in this circuit, we simply connect to ground permanently.

Since the CLEAR pin is active low, we connect to VCC, because we don't want the storage register to be cleared normally.

Since we are not cascading an additional shift register (we are only using one), we don't use the pin QH'. This is the Serial Data Output pin. Since we are not sending the data out to another shift register, this pin is not used. Therefore, we just leave it unconnected.

Lastly, we connect the LEDs to the output pins, QA - QH. To each of the LEDs, we connect a current-limiting 220Ω resistor.

Code

This is the code necessary to run this shift register circuit.

//Pin connections to the arduino microcontroller
const int latchPin= 8;
const int clockPin=12;
const int dataPin= 11;

void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Serial.begin(9600);
Serial.println("Enter the number of LEDs you want to turn on from 0-7:"); }

void loop(){
if (Serial.available() > 0) {
//ASCII characters 0 to 9 are represented by the values 48 to 57
//so if a user types in a number from 0 to 9 in ASCII
//simply subtract 48 to get the actual value

int bitDesired= Serial.read() - 48;

//writes to the shift register and turns the bit high that is requested
registerWrite(bitDesired, HIGH);
}
}

void registerWrite (int PinToSetHigh, int PinState) {
//the bits you want to send
byte bitsToSend= 0;

//turn off the output so the pins don't light up while the bits are being shifted in
digitalWrite (latchPin, LOW);

//turn on the next highest bit in bitsToSend
bitWrite(bitsToSend, PinToSetHigh, PinState);

//shift the bits out
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

//turn on the output so the LEDs can light up
digitalWrite(latchPin, HIGH);
}

In the first block of code, we show the connections of the latchPin, clockPin, and dataPin to the arduino board. These all connect to digital pins.

In the second block of code, we set the latchPin, clockPin, and dataPin as outputs. This because the arduino writes to them. If they were inputs, the arduino would read them. We also create a Serial Monitor so that the user can enter from numbers 0-7 the amount of LEDs desired to be turned on.

The next block, our loop() function, reads the value that was entered and writes into the storage register. We then, as the final line, turn the latch Pin HIGH so that the data in the storage register then gets transferred to the output pins. And this is when we see the LEDs turn on. We initially see the numbers light up that we entered into the Serial Monitor. Each the time the loop() function is executed, the bits then shift to the right. The Most Significant Bit (MSB), gets shifted out first. It's a left shift till all the LEDs are shifted out. We then can reset the microcontroller and put in a new value. If you want to left shift, you can change the shiftOut function from MSBFIRST to LSBFIRST. This will shift out the Least Significant Bit (LSB) first.

So this is an example of a shift register circuit.




Related Resources

How to Cascade Shift Registers

How to Build a Shift Register Circuit with Manual Pushbutton Control

HTML Comment Box is loading comments...