Arduino Shift Register

How to use shift registers to increase the number of ways you can use your Arduino board.

This page focuses on using them as tools to assist in animating scale models, dioramas or model train layouts.

The first thing to do is to go through the Arduino tutorials on shiftout.  Set up a board and go through the examples.

 

Arduino Tutorial on shiftout – This is really a necessary basic step. However, we feel that the examples shown in the tutorials are overly complex and their use to a modeler is limited. So we have other information below.

Sparkfun Shift Register Breakout Board –  A tutorial on using a plug and play breakout board plus good basic shift register information. The video in the page is particularly important.

 

Arduino Project #12 | 3 Pins, 32 LEDs, 4 Shift Registers [74HC595]  Video   https://www.youtube.com/watch?v=N7CAboD1jU0

 

Shift Register IDE library  Link – This is the library mentioned in the video above       https://github.com/j-bellavance/HC595

 

By connecting your lights through a shift register, and powering the shift register externally, you totally remove the current load on your board.  So, for example, you can control 50 LEDs and more from a Nano and not burn it out.

So here are some basics

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 fixed 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. 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.

LSB means the shiftout starts at the leffmost bit.

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

 

 

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);

}}

 

 

 

 


We hope that you found the information in this page useful. The internet is all about getting and giving. And giving back is the best. Please donate $1 or more to aid us in our continuing quest to add to and polish the information in our site.


Scroll to top