The 74HC595 shift register is an integrated circuit that converts serial information to parallel. It requires 3 pins (as well as power) from the micro:bit but allows us to have a total of 8 outputs. This means that we can control 8 LEDs using only pins 0, 1 & 2.
This page describes how to use a shift register in the Block Editor. That is not the most convenient way to write a program for a shift register but it does make it easier to understand what we have to do to make the shift register work.
The shift register used on this page looks like this and costs around 60p to buy, depending on the shop,
Electronic components normally have a datasheet which explains how it works, what voltages it can tolerate and what each of the pins do. From the datasheet for this integrated circuit, the pinning diagram is as follows,
This looks a little complicated but can be understood if we take our time.
|VCC||3V||This provides power to the integrated circuit.|
|GND||GND||This is the connection to ground that completes our circuit.|
|QA-QH||LEDs 1 - 8||These are the outputs. We connect them to the LEDs.|
|OE||GND||OE stands for output enable. The integrated circuit is enabled when we connect this to ground.|
|SRCLR||3V||We need to connect this pin to the power to enable the shift register.|
|SER||Pin 0||This is our data pin. We use this to send 1s and 0s to the shift register.|
|SRCLK||Pin 1||We call this the clock pin. We use this to tell the shift register to accept an input.|
|RCLK||Pin 2||We call this our latch pin. We use this to tell the shift register to output the mixture of high and low voltages we have previously sent.|
The notch on the integrated circuit tells you how to orient it when you put it into the breadboard. It goes at the top. Here is a diagram of the circuit you will need.
It looks complicated. It is easier to get the connections right if you follow the logic of the table above.
Programming 1 - Test
As a first test, we want to see if we can turn on all of the LEDs. To do this, we first set the clock and latch pins to 0. The shift register responds to a change on these pins that goes from low to high voltage. By writing a 0 to them, we get the pins ready for this change. The data pin is set to 1. This is because we want to send a series of high signals so that all of our LEDs go on.
The loop makes sure we do 8 iterations. There are 8 outputs and we want to set them all to 1. When we change the clock pin from low to high, we are 'sending' a 1 to the shift register. With the data pin set to 1, we do this 8 times. We set the clock pin low each time, ready for the next change from low to high.
In order to make the shift register output its signals, we make change the latch pin (2) from low to high. At this point, all of our LEDs should come on.
If you have a problem with any connections, you will not get all of the LEDs come on. If there is a problem with the program, the shift register will not receive the correct information. Be careful when you test this. A good way to check if you have connections and program correct is to pull out and reinsert the USB cable or press the reset button. The program only sends the instructions to the shift register once. By disconnecting and reconnecting, you will know that the micro:bit is running the program.
Programming 2 - More Testing
This component is only any use to us if we can have a choice about which LEDs are on and off. This programs sends 4 high data bits and 4 low.
When you run this program, you should have only 4 LEDs lit up. Setting the data pin low before the second loop means that our clock pin changes sent 0s to the shift register. This also shows you how the order of our 1s and 0s relates to the LEDs.
Programming 3 - A Bit More Testing
This last program is meant to turn on alternate LEDs. The loop does 4 iterations. Each time, a 1 and then a 0 is sent to the shift register.
- There has to be something interesting that you can do with so many LEDs. You could use them to show the amount of roll being sensed by the accelerometer, turning on different LEDs depending on whether the micro:bit is tilted to the left or right.
- You could use some LEDs as a temperature indicator for a thermometer.
- You could separate the LEDS into 2 groups of 4 and use them as a scoreboard for a digital game of rock, paper, scissors. After each round, the user should press button A or button B depending on which player won the round. When a player reaches 4, they win the game.
- Pretty patterns can be made with this many LEDs. Look up a Larson Scanner. You can use the shift register to make one of these with your micro:bit.
- 8 bits make a byte. Turn the LEDs into a binary counter that counts from 0 to 255. This is quite a challenge if you use the online editors.