More Arduino outputs, using shift registers

No matter how many digital outputs you get with your Arduino, you’ll still want more. And as outputs that come directly from the CPU tend to work out quite expensive, it would be good to find alternative ways of implementing them.

And this brings us to the concept of the shift register. This is a component that lets us run many more outputs, requiring only two or three pins on your Aduino. Effectively, it’s a serial-to-parallel converter.

The 595 shift register (also known as the 74LS595 or the 74HC595, depending on any special electrical requirements you may have) is a very common example, and I’ve just bought twenty of them off eBay … so that’s what I’ll use for this article.

The 595 provides eight digital outputs. It works rather like a conveyor belt. You push a one or a zero in at one end, which becomes the new state of output pin 0. Each bit that is already stored is pushed along one place, to the next output. The bit at the far end ‘drops off’ the conveyor.

To push a bit in, you only need two pins. One is the ‘data’ pin (the one or the zero you wish to push in) and the other is the ‘clock’ pin – the 595 monitors this, and accepts the new bit when the clock pin is pulsed low.

So if you want to change the state of any (or all) of the outputs, you need to push in eight new bits of data. The old eight bits get pushed out to make room for the new eight. Easy!

There’s a side-effect with this mechanism, though, which (depending on your project) may or may not be a problem. To push the old eight values out to make room for the new eight, your bits have just ‘scrolled’ across the outputs! Now, the 595 will accept new bits very quickly – certainly quicker than the human eye can see. So if the outputs are being used to drive LEDs (like a seven-segment display, for example) then you probably won’t see this effect. But if the outputs of your 595 are connected to another device, then you might see some odd behaviour as some outputs briefly change their state.

To beat this, the 595 has the ‘latch’ pin (also known as the ‘storage’ pin). When used, the 595 will ‘freeze’ the outputs in their current state, but still accepts input in the usual way. When the latch pin is released, the outputs update to their correct new state, with no scrolling in between.

This requires an extra pin on your Arduino, so whether you choose to use it depends on your needs, and how blessed you are with spare outputs on your Arduino!

A quick look ’round the pins


  • VCC – Power supply. Connect 5v here.
  • GND – Ground. Connect 0v here.
  • Q0 to Q7 – The eight outputs.
  • DS – The data pin.
  • /OE – Output enable. Should be low to make the register work. Tie this to ground for now.
  • ST_CP – The storage/latch pin.
  • SH_CP – The clock pin.
  • /MR – The master reset. In my humble opinion: not worth the faff. Tie it to VCC.
  • Q7‘ – The ‘overflow’ pin. We’ll deal with that later.

First experiment

Here’s a first circuit to demonstrate how the 595 works. The LEDs are just your bog-standard 20mA affairs, and the resistors are just to protect them (so with a 5v supply, that’s about 220 ohms). Click to see it bigger:


The LEDs are present to show the state of each of the eight outputs. The master-reset pin has been tied high (don’t want it) and the output-enable pin is tied low (because we want it running constantly for this circuit). The data, clock and latch pins should be connected to any three digital pins on your Arduino.

And here is a brief Arduino sketch to demonstrate it running:

int latchPin = 13;  // Connected to ST_CP (pin 12) of 74HC595
int clockPin = 12;  // Connected to SH_CP (pin 11) of 74HC595
int dataPin  = 11;  // Connected to DS (pin 14) of 74HC595

void setup()
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  digitalWrite(latchPin, HIGH);
  digitalWrite(clockPin, HIGH);

void loop()
  // we're going to push a 1 to the 595. So set the data pin high ...
  digitalWrite(dataPin, HIGH);

  // pulse the clock pin low to make the 595 read it
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);

  // brief pause, so we can see the result

  // we're going to push a 0 to the 595. So set the data pin low ...
  digitalWrite(dataPin, LOW);

  // pulse clock pin again ...
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);

  // another delay ...

  // and we'll send another 0. Pulse the clock again ...
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);

  // last pause ...

Run this sketch, and you’ll see your LEDs appear to scroll in a slow sequence – one LED on, two off. Modify the code and see the results so that you’re confident in how it works.

Also note above that the latch pin is set high during setup, and then not touched again for the rest of the sketch. When the latch pin is high, the outputs are unfrozen – what you see is what is happening. If you experiment with setting the latch pin low at one point then setting it high later on, you’ll see the ‘freezing’ effect.

Slightly neater code

To send a byte to the 595 is not a complex piece of code – a simple loop and some bit-testing would do it. Fortunately, it’s even simpler than that, thanks to a built in shiftOut function provided with the Arduino libraries:

shiftOut(dataPin, clockPin, MSBFIRST, value);

You just pass the pin-numbers for the data and clock pins, the order that you’d like the bit-pattern sent (can be MSBFIRST or LSBFIRST) and the value you want to send. If you want to use latching, just set the latch pin low beforehand, then set it high afterwards. Easy.

Here’s a quick bit of code that puts a binary count on the 595 as fast as it can, and uses the latch:

for (int v = 0; v < 256; v++)
  digitalWrite(latchPin, LOW); // freeze
  shiftOut(dataPin, clockPin, MSBFIRST, v);  
  digitalWrite(latchPin, HIGH); // unfreeze

Extending the conveyor belt

So that gives you eight outputs from just two or three pins. Can we do any better?

And the answer is: yes, quite easily! We can chain more 595s together (making a longer conveyor belt) by using the overflow pin (pin 9).

The overflow pin is the state of the pin which is about to ‘drop off’ the end. So, we use that pin to feed the input of the next 595 in the chain. We connect the clock and the latch pins together on all the 595s in the chain (so that they all process data at the same time and freeze in the same manner) and we get lots more outputs – for no extra pins on the Arduino!

Here’s a circuit demonstrating this principle (click to see it better):

595_dualIn it you can see that the clock and latches are connected together on both registers. Pin 9 (the overflow pin) of the upper register is connected to pin 14 (the data input) of the lower register – so as each bit drops off the end of one register, it appears in the next.

To properly update both registers in your Arduino sketch, just remember that you want to send two bytes of data each time rather than one:

for (int v = 0; v < 256; v++)
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, v); // for 2nd register
  shiftOut(dataPin, clockPin, MSBFIRST, v); // for 1st register
  digitalWrite(latchPin, HIGH);

Here’s my circuit running on breadboard, driven by an Arduino Nano (which is just like the Uno, but in a more conveniently-shaped package). It is running the one-on-two-off sketch:

595You might notice that there are only twelve LEDs on my board – that’s just because I ran out of space! Twelve is still enough to demonstrate that as each bit ‘falls out’ of the first register, it enters the second.

There isn’t really a limit to the number of times you can extend your conveyor. Your limiting factors are likely to be cost, your circuit size, and remembering that to change the state of one bit you must refresh all of them. Obviously, that gets slower as you add more registers!

Disadvantages to using shift registers

  • They’re slower than accessing pins on your Arduino. Though when I say “slow” I mean in computing terms, rather than in a speed that any human could discern. It is usually more than adequate for what you need to control. Running a sequential count on a single 595, and then connecting the lowest bit to the frequency counter in my crappy portable oscilloscope revealed that it was changing at a rate of nearly 4kHz. That’s still about 500 times faster than the human eye could distinguish.
  • It increases the component count and power consumption of your project. Only you can decide whether either of these points are an issue for your project. They rarely are for mine.