Thursday, May 23, 2013

Back to basics: Using 1 shift register

At it's core the 74HC595 shift register is simply a way to turn on 8 signals at one time. To do this only 5 wires need to be run from the Arduino board. So you save 3 pins, "so what, I have plenty of pins" you ask? While this is true, the magic really happens when you want to use 16 signals, or, even 64 or more. Guess what? The same 5 wires will control all of them, even over 500 if necessary. Now that's some power.

A couple of entries ago, I dove into an overly aggressive project, attempting to wire 4 shift registers to control both the anode and cathode wires of my, now infamous, 8x8 RGB LED matrix. You can't imagine how many times I typed "8x8 RGB LED" into Google over the last few weeks. This matrix has 8 rows on the anode (plus) side connecting to all 24 LED connections in that row. That makes this panel a "Common Anode" device. This matrix, then, has 24 columns on the cathode (minus) side, 8 for each color and column. This gives this board 36 total connections. Thus, the 4 shift registers. One would be dedicated to that 8 anode connections, 8 for the red, 8 for the blue, and 8 for the green cathodes.

I full-heartedly dove right in wiring this together, borrowing some code that I found on-line, I compiled my masterpiece, watched as the transmit/receive LEDs lit up on my Arduino board as the code was transferred and watched, in horror, as absolutely nothing happened.

[insert expletive here]

I felt I understood the wiring but, admittedly, didn't understand the code at all.

That's why I find myself here, now.

I ripped my project apart and decided to start at the beginning with using shift registers. I intend to spend as much time understanding the code as I do with the wiring. Since I have to start somewhere I'm going to start with the wiring.

If you've read some of my other postings, you'll remember that I had come across some replacement LEDs from some Christmas light sets, 4 in total. While this is an incredibly simple for the use of a shift register, it will be enough to gain the understanding that I need.




This is a clip from the manufacturers datasheet which can be downloaded by clicking the above link. The pins that I am going to first concern myself with are as follows:


Pin Label Meaning
1QBData Output 2
2QCData Output 3
3QDData Output 4
4QEData Output 5
5QFData Output 6
6QGData Output 7
7QHData Output 8
8GNDGround
10SCLRShift Register Clear Input. If this pin is driven low, it will clear the register. Since I don't have a need for that right now, I will just hold it high. The easiest way to do this is to connect it to the 5 volt pin on my Arduino board.
11SCKShift Register Clock. This will be wired to the Arduino and is used to control the movement of the bits in the register.
12RCKStorage Register Clock Input. This will be connected to the Arduino board. When this is low, data is expected to be flowing into the shift register. When this pin is high, the data is copied from the shift register to the storage register. This, in effect, turns on or off the pins that you told it to. This is just like turning on or off 8 light switches at the same time, in parallel, as it is known.
13GOutput Enable Input. When this is high, it turns off all output. Yes, you could use the SCLR, but this remembers the data. When the pin is, once again, low, the same pins that were on before will be turned on again. By toggling this, it is possible to dim the LEDs using PWM. For now, I just want to keep this low. This easiest way to do this is to wire it to ground.
14SISerial Data Input. This is our workhorse. On this pin, we will send the on and off signals telling this register which outputs will be on or off.
15QAData Output 1
16VccPositive Supply Voltage. This is, just like it sounds, positive voltage. I'll simply wire this to the 5v on the Arduino board.

Whew! Maybe a picture will make this overwhelming amount of information make sense. Here is a drawing of the circuit:
Clear now? Let's break it down. First, all of the black wires are ground (minus) and all of the red wires are 5V (plus). If you have the same kind of breakout board as shown here, you'll see 2 rows of pins at the top and 2 more at the bottom. One has a red line and the other black. These rows are connected across the entire board and are there to have a simple way to grab a positive or negative lead. Think of it as a power strip like you would use in your home to plug you tv, dvd, vcr (what that? :), and other components into instead of running them all to the wall outlet. Likewise, why run all of these extra wires back to the Arduino? So the first thing I did was run a lead from the 5v pin (wall outlet) to the red row at the top (power strip). Then, just for convenience, we plug in another "power strip" on the bottom of the board. 

Now, I added my other components. First, the shift register chip. Notice how it straddles the gap in the middle of the board. On breakout boards all of the pins are connected in columns, but they are not connected across the gap. With the chip in place, I get 4 rows of connectors for each pin on the top row of the chip and the same is true on the bottom of the chip. If not for the gap, you would be connecting the pins on the top of the chip to the pins on the bottom of the chip which, obviously, is not desirable. Also, notice the notch in the chip on the left side. If you look above at the picture with the pin descriptions, you'll see that picture, too, has a notch. This tells us that pin 1 is on the lower left as I plugged it into the board.

Next my 4 LEDs, taking care that the anode is on the left of each one. One pin in each column. Below them, are 220k OHM current limiting resisters. These are simply to protect the LEDs. If too much current passes through the LED, it will not produce noticeably more light, light a light bulb would. Instead, it will start to generate heat. Heat will damage the components inside the LED and wear them out quicker. I bought 400 resisters on ebay for less than $5 which is far cheaper than LEDs.

Now all of our components are installed and we have power readily available. In the pin description above, I said that there were 3 wires, besides 5v and ground, that were going to be connected to the Arduino. These are SCK (11), RCK (12), and SI (14). I'm connecting these to pins 10, 9, and 8 on the Arduino board with the yellow, purple, and blue wires. This is where our code will come in to do it's magic.

Now we wire the LEDs. Pin 15 (QA) on the shift register will go to the anode on LED 1. Pin 1 (QB), pin 2 (QC), and pin 3 (QD) are connected to the anodes on LED 2, 3, and 4. In the picture, I have each of these connections on a different row. This is not necessary, since all of the pins in that column on that side of the gap are connected. This was just done this way so the picture looked better.

I, in the pin description above, mentioned that the SCLR (pin 10) needs to be held high, so run a red wire to the positive power row at the top of the board. Likewise, G (pin 13) needs to be held low, so run a black led wire to the negative power row at the top.

That's all the logic that needs connecting. Now let's provide power and ground to everything that needs it. Each of the resisters that are coming from the cathodes of our LEDs need to go to ground so run a black wire to the negative power row at the bottom. The shift register needs a ground on pin 8, so run a black wire to the negative power row. Lastly, our shift register needs power on pin 16, so a red wire from the positive power row to pin 16 takes care of that.

I'll leave you with a schematic drawing of the circuit. In the schematic drawing, instead of the Arduino PCB (printed circuit board) being illustrated, the actual connections on the ATmega chip are shown. In essence, this is what is happening. The PCB also has connections to USB, power supply, etc. but, essentially, the chip is performing its work on the connections indicated.You should be able to find the wires that go to each connection on the schematic. See one of my previous blog entries for a description of the schematic symbols.
Next time, I'm going to figure out the code that goes with this....

I hope!

No comments:

Post a Comment