Shift register question

I’m new to the world of Arduino and am slowly building up my knowledge. I have just started using shift registers to control several LEDs and understand that registers can be daisy chained to increase the number of LEDs being controlled.

So far I have been using pins 8, 11 and 12 for the latch, clock and data output from the Arduino (UNO in my case) but was wondering if these are the only pins that can be used for this purpose.

The reason I am asking is that I am working on a project that requires the use of 2 shift registers that for practical reasons can’t be daisy chained and I was wondering if I can use different pins for the latch, clock and data functions to control the second register.

Yes, you can use any pins. The clock and data pins can be shared by multiple shift registers, even if they are not daisy chained. Only the latch pins will need to be separate for each register.

Thanks for that. Will make my design a lot easier to put together knowing that.

If you use the SPI interface to drive the shift registers, you can achieve much higher speeds, but if that doesn't matter to you, you can use any pins.

Pieter

My knowledge hasn’t extended to take in SPI yet, so for the moment I will be sticking with what I know, but thank you for highlighting an alternative.

SPI really isn't that hard, if you want to, you can certainly do it, just replace

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, shiftVal);
    digitalWrite(latchPin, HIGH);

with

    digitalWrite(latchPin, LOW);
    SPI.transfer(shiftVal);
    digitalWrite(latchPin, HIGH);

Include the SPI library:

#include <SPI.h>

and add SPI.begin(); to your setup. If you want the maximum frequency, add SPI.setClockDivider(SPI_CLOCK_DIV2); after SPI.begin();

For example, a simple LED chaser animation:



<br>Code (ShiftOut):<br>




---



```
const uint8_t latchPin = 10; // Pin connected to ST_CP of 74HC595
const uint8_t clockPin = 13; // Pin 13 (SCK) connected to SH_CP of 74HC595
const uint8_t dataPin  = 11; // Pin 11 (MOSI) connected to DS of 74HC595

const unsigned long delayTime = 100;

void setupcolor=#000000[/color] {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin,  OUTPUT);
}

void loopcolor=#000000[/color] {
  static uint8_t shiftVal = 1;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, shiftVal);
  digitalWrite(latchPin, HIGH);
  delaycolor=#000000[/color];
  shiftVal = (shiftVal << 1) | (shiftVal == 0);
}
```

|

Code (SPI):[hr][/td][/tr][tr][td][size=9pt][tt][color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]SPI[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color]

[color=#00979c]const[/color] [color=#00979c]uint8_t[/color] [color=#000000]latchPin[/color] [color=#434f54]=[/color] [color=#000000]10[/color][color=#000000];[/color] [color=#434f54]// Pin connected to ST_CP of 74HC595[/color]
[color=#434f54]// Pin 13 (SCK) connected to SH_CP of 74HC595[/color]
[color=#434f54]// Pin 11 (MOSI) connected to DS of 74HC595[/color]

[color=#00979c]const[/color] [color=#00979c]unsigned[/color] [color=#00979c]long[/color] [color=#000000]delayTime[/color] [color=#434f54]=[/color] [color=#000000]100[/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]SPI[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]SPI[/color][/b][color=#434f54].[/color][color=#d35400]setClockDivider[/color][color=#000000]([/color][color=#00979c]SPI_CLOCK_DIV2[/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// 16 MHz / 2 = 8 MHz[/color]
  [color=#d35400]pinMode[/color][color=#000000]([/color][color=#000000]latchPin[/color][color=#434f54],[/color] [color=#00979c]OUTPUT[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#00979c]static[/color] [color=#00979c]uint8_t[/color] [color=#000000]shiftVal[/color] [color=#434f54]=[/color] [color=#000000]1[/color][color=#000000];[/color]
  [color=#d35400]digitalWrite[/color][color=#000000]([/color][color=#000000]latchPin[/color][color=#434f54],[/color] [color=#00979c]LOW[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]SPI[/color][/b][color=#434f54].[/color][color=#d35400]transfer[/color][color=#000000]([/color][color=#000000]shiftVal[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#d35400]digitalWrite[/color][color=#000000]([/color][color=#000000]latchPin[/color][color=#434f54],[/color] [color=#00979c]HIGH[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#d35400]delay[/color][color=#000000]([/color][color=#000000]delayTime[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]shiftVal[/color] [color=#434f54]=[/color] [color=#000000]([/color][color=#000000]shiftVal[/color] [color=#434f54]<<[/color] [color=#000000]1[/color][color=#000000])[/color] [color=#434f54]|[/color] [color=#000000]([/color][color=#000000]shiftVal[/color] [color=#434f54]==[/color] [color=#000000]0[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[/size]
For a simple 8-LED chaser, you don't have to use SPI, but it will be faster (8 MHz clock vs a couple of KHz), and it will use a lot less CPU (the actual transmitting is done in hardware, rather than in software).

Pieter

PieterP:
SPI certainly isn't that hard, if you want to, you can certainly do it, just replace

    digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, MSBFIRST, shiftVal);
   digitalWrite(latchPin, HIGH);



with


digitalWrite(latchPin, LOW);
   SPI.transfer(shiftVal);
   digitalWrite(latchPin, HIGH);




(snip!)

Pieter

Oh this looks really useful! Very relevant to something I just posted up, thanks

Amazing community - thanks for the help and suggestions.

When using SPI, you can only use certain pins for clock (SCK=13 on Uno) and data (MOSI=11 on Uno). But these can be shared between several shift registers, as I said before. You can use any pins you like for the latch signals. However, you can't use pin 12 on Uno (that's MISO for incoming SPI data). Also, it's important to set SS (pin 10 on Uno) as an output, so always use that as one of your latch pins.