We've already seen on the demultiplexers page the reasons for wanting to expand the number of outputs on the Raspberry Pi. Demultiplexers provide one solution, but they're not the only one. Here, we'll now look at another possible solution, called shift registers.
In principle, a single shift register behaves in a similar way to a demultiplexer - it takes 3 inputs (for example, three GPIO pins from the Raspberry Pi) and from those it switches 8 outputs. These outputs could be used to light simple LEDs, of course, or they can be used to control something more complicated.
An example of a shift register, lighting one or more of eight LEDs controlled by just three pins.
Unlike the demultiplexer, any number of these outputs can be on at the same time.
The way that the inputs of a shift register work is however completely different to that of a demultiplexer. For a demultiplexer, you just set the values of the three inputs either high or low, and that's it. With a shift register, we need to feed it high and low signals one at a time, and the shift registers remember what we've given it. Then we tell it that we've finished, and the values which we've already entered are then used to activate the outputs. So it's not an instantaneous process, we'll need to carefully send the signals in the right sequence and then trigger it to go.
Another major difference between shift registers and demultiplexers is that with demultiplexers only one of the outputs can be active at a time. With a shift register, we can control the outputs how we like, with any number of them active. This is why the diagram above shows each output having its own resistor, whereas the demultiplexer outputs can share a common resistor.
This diagram shows the outputs of the shift register sourcing current which drains through the LEDs to ground. Some shift registers work this way, and some sink current from +V through LEDs to the output.
We've seen that with three GPIOs and one shift register, we can control eight outputs. But the great thing about these is that you can connect several shift registers in a chain, and the bits which you feed in get passed on down the chain.
An example of a chain of shift registers, controlling any number of outputs with 3 pins.
If you feed in 8 bits, they just go into the first register. But if you then feed in another 8 bits, the first 8 get passed on to the second chip, and the subsequent ones go into the first register.
However many shift registers you have, you still only need three GPIOs to control them all. But of course the more shift registers you chain together, the longer the bit sequence you have to feed in one bit at a time, so the longer it takes to change from one state to another.
There are three inputs required to the shift register, and it's important to control these properly in the right sequence. Unfortunately there are a few different names in use to describe the three inputs, so the data sheet for your chip might use a different name. However it should include a state diagram so you can figure out which is which.
The first input is called "data", and you set this to be either high or low. However, you can wave the value of this input around as much as you like, at whatever speed you like, and you're not going to send any bits into the shift register without using the second input.
The second input is called "clock", and when this goes from low to high, that tells the shift register to take the value of the data pin to be the next bit. And you can pulse this clock pin whenever you like, as slowly as you like, and as irregularly as you like. So to send in a 0 bit, you make sure the data pin in low, and then pulse the clock pin from low to high. To send in a 1 bit, you set the data pin to high and then again pulse the clock pin from low to high. Nothing happens when the clock pin goes back down to low again.
However, during all this sequence of pushing bits into the shift register, nothing happens to the outputs. The outputs stay doing what they were doing before (which is useful, because we don't want the outputs to flicker while we're setting bits). We need a way to tell the chip to copy the contents of the shift register to the outputs, and we do this using the third input which is called the "shift register clock". When this goes from low to high, the chip takes the bits in the shift register and sets the state of the 8 outputs.
If all of our chained shift registers have the same clock, and the same shift register clock, then each shift register takes its data pin from the previous shift register in the chain. When we push bits into the first chip, the previous bits will "pop out" the other side and be clocked into the next shift register in the chain.
One example of such a shift register chip is the
M74HC595. This is cheap and popular, and available in many packages including a 16-pin DIP. The outputs of this chip act as current sources, so it's a good choice to use with a common-cathode 7-segment LED display. One of these chips can then control all the segments of one digit including the decimal point.
An example of a two-digit, 7-segment LED switched by two shift registers, controlled by the Raspberry Pi.
Here, the raspberry pi has 3 GPIO pins connected to the pair of shift registers, with 14 of the LED segments being controlled. After setting the bits, the raspberry pi doesn't have to do anything any more, it can just keep all those GPIO pins low and go to sleep, because the shift registers are keeping responsibility for the outputs. Hence, there's zero flicker. If the pi was trying to scan across the digits to refresh them all the time, the pi would have lots of work to do and you'd get annoying flicker.
Another example is the
TPIC6B595N which is described as a power shift register and is able to cope with higher voltages and higher currents. The outputs of this one behave as current sinks, so our current will flow from the 12V line through the LED strips and into the outputs of the shift register chips.
An example of 12V LED strips switched by TPIC power shift registers, which sink current from a 12V supply.
There's nothing really difficult about these circuits, it's just wires and resistors. The fun comes with the software, using python and RPi.GPIO to control the shift registers and manipulate the symbols shown by the LEDs. This four-digit display is controlled by 3 outputs and four TPIC6B595N chips, leaving plenty of scope left to do other things with the remaining GPIOs.