Saturday, December 21, 2013

How to use the 74HC595 Shift Register

I bought a bunch of 74HC595's a while ago and didn't really know what they were. They were super cheap though. I'm on Christmas break now so I finally have time to actually do stuff. The 74HC595 chip basically lets you use three pins on your arduino, and extends those into 8 more output pins. It's known as a shift register or serial to parallel converter. So the mini project I'm doing for this post is blinking Christmas colored leds. It's easy, but explains very well how the shift register works.

To start off, the way you control the output's on the 595 is by sending it a byte. A byte has 8 bits, and each bit corresponds to a pin on the 595. For example, if you send the byte 10101010, every other output would be HIGH, and the zeros would be LOW. In the Arduino IDE, there's a reserved word called MSBFIRST, or LSBFIRST, which stands for most significant bit first, or least significant bit first. Meaning that if you send 00001111 with MSBFIRST, the first four outputs will be LOW because it would read from right to left. But if you send it with LSBFIRST, the first four will be HIGH, because it reads if from left to right. Now on to the wiring.

Here's a small list of things we're going to need:
Three IO pins on the Arduino
Power and Ground from the Arduino
8 leds
8 220 ohm resistors (Red, Red, Brown)
74HC595 IC

On the 595 chip, we're going to use pins 11, 12, and 14. Which are the Clock pin, Latch pin, and data input pin respectively. I'll explain what those pins do later.

On your breadboard, connect the anode(+) of the led to the resistor, and cathode(-) to the common ground rail. Then the outputs for the 595 chip are on pins 1-7, and pin 15. Connect wires from those pins to each resistor. Pin 1 starts when the cutout is in the IC. It reads in a continuous loop, so pin 8 is on the very bottom left, then pin 9 starts directly across from it on the bottom. and continues up.

On my Arduino, I'm using pins 4, 5, 6 to connect to the three control pins on the shift register.

Here it is made in Fritzing, which is really fun to use by the way!

Now that it's all wired up we can write the code.

We have three pins being used on the Arduino. Pins 4, 5, 6. Arduino pin 4 is going to pin 11 on the shift register. Arduino pin 5 goes to pin 13 on the shift register. Arduino pin 6 goes to pin 14 on the shift register. Wire everything up exactly as you see it in the breadboard view above.

You can find some examples I made for the shift register on my github.

Understand how the function shiftOut works is key to using this shift register. The syntax for it is as follows: shiftOut(dataPin, clockPin, MSBFIRST, B11111111); based on that, the two control pins are the dataPin and clockPin. We want the register to read the Most Significant Bit First, and then we give it the byte pattern. Whenever you want to change the outputs, you need to set the latchPin LOW, make your changes, then set the latchPin HIGH again for the outputs to go into effect.

For example here's a function I made that set's all the outputs LOW:

void flashAllLow() {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
  digitalWrite(latchPin, HIGH);

Notice how latchPin goes LOW, then we make the changes we want, then set it HIGH again.

If you want to make an LED chaser you can use the << operand. What that does is shift the bits to the left a certain amount of times. Example: B00000001<<2 would become B00000100, the 1 got shifted to the left twice. To make an LED chaser we would want to continuously move that bit over in a for loop. An example of doing that is here:
  for (int k= 0; k < 8; k++) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, B00000001<<k);
    digitalWrite(latchPin, HIGH);
Again, all of this code can be found on my github page.

The way I figured out how to use these shift registers was by reading a crap load of tutorials online, and just kept trying new things with them.
Here's some really good links that helped me understand how these are all working.

Explains the << operand
"Can you move over?"
Shift-Registers + Arduino, by Kevin Darrah
shiftOut tutorial

Here's a few pictures of my build.

 a .gif of the leds flashing in random patters
 Breadboard picture of my wiring

and another picture

Feel free to ask questions! Typically I made these posts at 4AM so my way of thinking is probably heavily distorted...

No comments:

Post a Comment