Using a shift register with Raspberry Pi

This instructable will attempt to explain, in simple terms, what a shift register is, and how you can use it with the Raspberry Pi to expand the GPIO port by running eight outputs with just four GPIO ports.
To start with, what is a shift register?Ā  Wikipedia has a technical definition of shift registers, but in a nut shell, shift registers let you take serial input (one bit after the other), and output it in parallel.
Letā€™s say, for example, you wanted to output six digital bits from your Raspberry Pi to drive a display based on the HD44780, like this one, but your project had to give up almost all the GPIO to other things, and all you have left is four pins. You can send those six bits one after the other to the shift register using those four pins, which will then appear to the display as six parallel outputs.
For this project, which is a first step towards the above situation, Iā€™m going to run LEDs off each of the eight outputs, just as a proof of concept.Ā  The attached video shows the end result of this process.Ā  Just a bunch of flashing lights for now, but stay tuned.Ā  This is really the second in a series of instructables leading up to my Voiceberry PiĀ  (see the first one here).

Using a Shift Register With Raspberry Pi

Step 1: Wiring up the chip Part 1: Data pins 1-4

Wiring Up the Chip Part 1: Data Pins 1-4

I picked up a few shift registers for $0.39 each at Jameco.com.Ā  The ones I eneded up getting were SN74HC164Nā€™s from Texas Instruments.Ā  Not that it really means anything one way or the other, but thatā€™s what I ended up getting.Ā  The datasheet is available right there at Jameco, and details how to wire up the chip are in there.Ā  But hereā€™s the pinout:
1: Input A
2: Input B
3: Data 1
4: Data 2
5: Data 3
6: Data 4
7: Ground
8: Clock
9: NOT Clear
10: Data 5
11: Data 6
12: Data 7
13: Data 8
14: Vcc
Unless you get the same chip, your pinout will be different, and you should follow your datasheet.
To start with, the chip goes in the bread board, and then four LEDs are linked up from data pins 1-4 to a common lead (not ground, yet).

Step 2: Wiring up the chip Part 2: Data pins 5-8

Wiring Up the Chip Part 2: Data Pins 5-8

To wire up the next four LEDs, they go into the breadboard right next to the first four, but then wires need to run from the data pins 5-8 to the anode of the LEDs.
Additionally, a limiting resistor has to be inserted between the LEDsā€™ cathodes and what will become ground.

Step 3: Wiring up the chip Part 3: The other pins

Wiring Up the Chip Part 3Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Wiring Up the Chip Part 3 the Other Pins

 

The next wires too hook up are, in order of the pictures:

Vcc: Chip pin 14 to GPIO pin 2
Ground: Chip pin 7 to GPIO pin 6
Clock: Chip pin 8 to GPIO 7
NOT Clear: Chip pin 9 to GPIO 11
Input A: Chip pin 1 to GPIO pin 13
Input B: Chip pin 2 to GPIO pin 15
Note that GPIO 2 and 6 are hard wired as +5V and Ground repsectively, but all other GPIO pin choices were quite arbitrary.Ā  I could have used any valid GPIO pin, and chose the ones I chose to keep them close to the ground and 5v pins.

Step 4: Now what? Or I still donā€™t get it!

Now What or I Still Don't Get It

So youā€™ve got it all wired up, but what does it all mean?Ā  How does it work, and how to do I make it work?Ā  Well, this varies a little from chip to chip, but for the most part itā€™s the same.
NOT Clear is set to low first, to clear the shift register, and then the clock is turned HIGH and LOW.
The input wire can be high or low, depending on what you want to set, and the clock is turned HIGH and LOW.
Each bit to be sent in is set either high or low, and the clock is turned HIGH, then LOW.
Each time you want to send another bit to the register, you set that bit on the input, and then ā€œtickā€ the clock, by setting it HIGH, then LOW. There is a minimum time the clock must be HIGH, but the timing of the RPi is such that turning it HIGH, then immediately LOW will be more than the minimum time for my chip.
My video shows the LEDs switching back and forth between and ON OFF pattern and and OFF ON pattern.Ā  As an example, letā€™s setup the OFF ON pattern.
1) Set NOT Clear LOW, and tick the clock (turn clock HIGH, then LOW).Ā Ā  Set NOT Clear HIGH. This clears all outputs.2) Set the INPUT to HIGH, and tick the clock.Ā  This sets the first output HIGH
3) Set the INPUT to LOW, and tick the clock.Ā  This SHIFTS the outputs one to the right (1 goes to 2, 2 goes to 3, and so on), and
sets the first output to LOW.Ā  Our pattern is now LOW HIGH
4) Set the INPUT to HIGH, and tick the clock.Ā  This SHIFTS the outputs again, and sets the first output to HIGH.Ā  Our pattern is now HIGH LOW HIGH
5) Set the INPUT to LOW, and tick the clock. This SHIFTS the outputs, and sets the first output to LOW, giveing LOW HIGH LOW HIGH
6) Set the INPUT to HIGH, and tick the clock. This SHIFTS the outputs again, and sets the first output to HIGH.Ā  Our pattern is now HIGH LOW HIGH LOW HIGH
7) Set the INPUT to LOW, and tick the clock. This SHIFTS the outputs, and sets the first output to LOW, giveing LOW HIGH LOW HIGH LOW HIGH
8) Set the INPUT to HIGH, and tick the clock. This SHIFTS the outputs again, and sets the first output to HIGH.Ā  Our pattern is now HIGH LOW HIGH LOW HIGH LOW HIGH
9) Set the INPUT to LOW, and tick the clock. This SHIFTS the outputs, and sets the first output to LOW, giveing LOW HIGH LOW HIGH LOW HIGH LOW HIGH

Step 5: The Code

The Code
Attached to this step is my code to run the video youā€™ve seen twice now.

An important part of the code Iā€™d like to point out is the sleep statement.Ā  It seems so unimportant, but itā€™s really not.

After you go through the process in the last step of setting your output, if you go right into clearing and setting your next output, youā€™ll never get to ā€œseeā€ it on your LEDs.Ā  It will just flash too quick to see it.Ā  So you HAVE to pause the execution of the code for a second to get the LEDs to stay lit long enough for you to see them.

while running==True:
try:
shifter.clear()
shifter.setValue(80)
sleep(1)
shifter.clear()
shifter.setValue(170)
sleep(1)
except KeyboardInterrupt:
running=False

Also, as an aside, notice what I did with the try/except statements.Ā  This allows you to exit the code using Control-C, without generating the normal error.Ā  A little thing, but I like how it works out.

Also note that this code is object oriented, and can be imported into other projects at a later date.Ā  This will allow you to set output easily and quickly with this circuitry.

Step 6: Next Steps and Insights

Next Steps and Insights
Whatā€™s next?Ā  Well, finding a purpose for this, like running a display. Which is what my next step will be.

An insight: This chip really has two inputs.Ā  Not all chips do, and Iā€™ve passed over what the second input does.Ā  This chip takes an AND on both inputs, and only considers the input HIGH when BOTH Input A and Input B are HIGH.Ā  So I just set Input A HIGH the whole time, and kind of ignore itā€™s there.

Another fun task, and Iā€™ll only give you hints here, would be to chain two (or more) shift registers together.Ā  To do this with these chips, join up their Vccs, grounds and Input As, then hookup Data 8 of the first to Input B of the second.Ā  OK, Iā€™ll just tell you how itā€™s done, but not detail it step by step.

Stay tuned for the next two instructablesā€“using a display with a shift register, and then, the long awaited, much anticipated, Vocieberry Pi.

 

Source: Using a shift register with Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top