Basic Shift Register

An explanation of  and code to set up a basic shift register to light LEDs.

The code used here is simple and serves as a launching point for coding more complex animations.

There are connections for 8 LEDs on the shift register.

They are coded as 1 for on and 0 for off.

So you code 00000000 for all to be off. 11111111 for all to be on.

The code 00000011 turns on the first two LEDs and the remainder will be off.

When you enter them in the sketch, the format would be, as an example,  B11000111.  The B prefix tells the computer to create a fxed value that is based on individual bits.

Think about the bits as black and white marbles. And think as the shift register as a trough with notches to hold the marbles.

MSB means the shiftout starts at the rightmost bit.  Or it moves the rightmost marble to the trough first.  Then it moves the second marble, which when it gets to the trough, shoves the first marble to the left. This continues until all the notches are filled.  The colors represent on and off, or 1 and 0.

If you daisy chain shift registers, the second shiftout will move this first group of marbles left.  And so on.

There are many examples online that show sketches for using shift registers.

We, however, consider the examples MUCH to difficult, unless you are well versed in shift register coding, as well as sometimes incomplete, to understand.

We take the position that the least code is the best code.  It it for that reason that we publish this information.

And we animate our layouts using modules -meaning that we use a nano for each animation rather than larger boards doing multitasking.

Let me add code here and add explanations for each step of the way.

The intent is to turn LEDs on and off depending on the position of a toggle switch.

There are three green leds and 3 red.  They will be used to provide turnout position indication on each leg of the turnout and on the control panel.

/* This first section declares your pin assignments.

//Pin connected to ST_CP of 74HC595
int latchPin = 10;
//Pin connected to DS of 74HC595
int dataPin = 11;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;

int toggle1 = 2;  //This will connect to the board positive through a toggle switch.  You must add a 10k  drop down resistor to keep the latch pin low  when the toggle is in the off position or you will get LED flickering.
int postoggle1 = 0;

void setup() {
//set pins 10, 11, 12 to output so you can control the shift register and the toggle switch to input.
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(toggle1, INPUT);
Serial.begin(9600);
}
void loop() {

/*we are now going to tell the switch register to light red leds if the toggle switch is connected to the board positive and if it is not, to light the green LEDS.*/
postoggle1 = digitalRead(toggle1);
if (postoggle1 == HIGH)
{
digitalWrite(latchPin, LOW);

/* shift out the bits: The red LEDS are connected to the first three locations on the shift register and the green LEDs are connected to the next three.*/
shiftOut(dataPin, clockPin, MSBFIRST, B00000111);

//takes the latch pin high so the Red LEDs will light up and the Green LEDs will be off.
digitalWrite(latchPin, HIGH);
}

else {
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, B00111000);
//takes the latch pin high so the Green LEDs will light up and the Red LEDs will be off.
digitalWrite(latchPin, HIGH);

}}

 

 

Tell others about this information by using one of these services. Thank you for the support.
Scroll to top