2008-04-01

HOWTO: Shift registers for driving lots of LEDs

Upplagd av Joakim Bergman

It's easy to have as many LED's as you have outputs on your microcontroller but what happens when you need more of them?

In our project we used the Sun SPOT which has five I/O ports of which one had to be used for input leaving us with only four ports. This would have been fine if we had only four LEDs but we had in total 18 (three colours multiplied by six sides). Well, actually we had 3x9x6 LEDs which makes it a total of 162 LEDs. They were connected in parallell of nine so only 3x6 outputs were needed.

This is where shift registers come in handy. They allow you to store multiple digital values (1/0) for later reading using only one output (well, three actually, but the other two are not data as such, only control for the shift register).

  • STR makes the shifted bits appear on the outputs.
  • CP is used for telling the shift register to "shift" all values one step further.
  • D is used for the data that you want to shift out. It is read by the shift register when CP is cycled.
Shifting out an array of bits goes like this:

Pseudo code
  1. Set STROBE to 0

  2. For each LED

  3.   Set CLOCKPULSE to 0

  4.   Set DATA to either 0 or 1 (LED on or off)

  5.   Set CLOCKPULSE to 1

  6. End for

  7. Set STROBE to 1

On the Sun SPOT, you'd do this:

byte[] data = { 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0 };
static IIOPin D = EDemoBoard.getInstance().getIOPins()[EDemoBoard.D0];
static IIOPin CP = EDemoBoard.getInstance().getIOPins()[EDemoBoard.D1];
static IIOPin STR = EDemoBoard.getInstance().getIOPins()[EDemoBoard.D2];

STR.setLow();
for (int i = data.length - 1; i >= 0; i--) {
  CP.setLow();
  D.setHigh(data[i] > 0);
  CP.setHigh();
}
STR.setHigh();

Note that the loop is actually going backwards because the first bit you send will actually be available on the last pin of the shift register, something we didn't realize until after the circuit was soldered.


Shift registers for RGB LEDs

The shift register we used is called HEF4894B and is a "12-bit store and shift register" which is a nicer way of saying it can hold 12 bits. Here's the datasheet (PDF).

We connected it like this:



Three of the ports on the Sun SPOT was connected to STR, CP and D respectively. These were used for controlling the shift register.

The LEDs were connected on the outputs of the shift register in triples of RGB for each side.

For getting all LEDs to work, we had to daisy-chain two shift registers making the total number of bits expand up to 24 bits. Connecting two shift registers was quite easy. Just connect the same STR and CP from from the first one and connect the first ones OS' (Output Serial on negative clockcycle, should work with regular OS but we tried OS' first and didn't dare tamper with it) output to the seconds D input.

The power feed for the LEDs was connected on the VDD input on the shift register. Ground was connected to VSS.



As you can clearly see in this demonstration video, the RGB values are shifted out in triplets for the effect of wandering colours. Normally you wouldn't send the STR before all bits are sent but if you want this effect, send it after each triplet.

All was good.

0 kommentarer: