Skip to main content

7.5 Shift Register

Every pin on the board is spoken for eventually. This project shows how to get more outputs than you have pins: a 74HC595 shift register turns three pins into eight, and we use four of them to build a counter that counts from 0 to 15 in binary, the way a computer does.

In this documentation you will learn:

  • How a 74HC595 shift register turns three pins into eight outputs
  • What shifting and latching mean, and why the LEDs all change at the same instant
  • How to write your own shift_out() with the bit shift and bitwise AND operators
  • How to seat a 16-pin chip on a breadboard without getting it backwards
  • How to read a binary number off a row of LEDs

Hardware required

  • 1x Soldered NULA MINI board
  • 1x Breadboard
  • 1x 74HC595 shift register
  • 4x LEDs
  • 4x 330 Ω resistors
  • 14x Jumper wires
  • 1x USB-C cable
ℹ️
The four 330 Ω resistors are the ordinary LED current limiters you have used in every example so far: one per LED, bands orange-orange-brown. The control pins of the chip need no resistors: they are CMOS inputs and draw practically no current, so they connect straight to 3V3 or to the ground rail.
⚠️
Use red, yellow or orange LEDs. A shift register output sits at about 2.9 V under load, which is plenty for those, but a blue or a modern green LED needs 3.0 V or more just to switch on and will stay dark, which looks exactly like a wiring fault.

Putting the components together

This build has more connections than anything else in the kit, so it is worth going slowly. Every photo below is the board after one more thing went in, twenty-one steps in all: fourteen jumper wires, four resistors, four LEDs and the chip.

1. Insert the board and build the ground rail

Push the NULA MINI into one end of the breadboard so it occupies rows 25 to 30, then build the shared ground before anything else. This is the first example that needs ground on both sides of the board, and the two blue rails along the edges are not joined to each other inside the breadboard.

They are also too far apart to reach with one jumper, so the connection is made in hops, using row 1 as a stepping stone:

  • j30 (GND) to the bottom blue rail
  • the bottom blue rail to j1
  • e1 to f1, crossing the centre channel
  • a1 to the top blue rail

After those four jumpers, both rails and both halves of row 1 are one single ground.

NULA MINI board on the breadboard with four blue jumpers forming the ground rail
The board in rows 25–30, with the ground bridged around the far end

2. Press in the 74HC595

The chip straddles the centre channel and occupies rows 14 to 21, with the half-moon notch facing row 14, away from the board. One row of pins lands in column e, the other in column f.

⚠️
The chip also has a small round dimple in the plastic, about one row in from the row 21 end. That is a mark left by the mould, not the pin-1 indicator, and it points at the wrong end. Go by the half-moon notch cut into the end of the package. Get this backwards and 3V3 lands on an output pin.

With the notch at row 14, the pins sit like this:

Row1415161718192021
a–e side1 · Q12 · Q23 · Q34 · Q45 · Q56 · Q67 · Q78 · GND
f–j side16 · VCC15 · Q014 · DS13 · OE12 · ST_CP11 · SH_CP10 · MR9 · Q7'
74HC595 pressed into the breadboard across the centre channel
The chip in rows 14–21, straddling the channel, notch toward row 14

3. Ground the chip

A short blue jumper takes pin 8 from a21 to the top blue rail.

Short blue jumper connecting the shift register ground pin to the top rail
Blue: a21 (pin 8, GND) to the top blue − rail

4. Give the chip power

The white wire carries 3V3 from j29 to j14, the VCC pin of the chip. It loops down below the board rather than crossing straight over, because that is the length of wire that was to hand. Nothing is connected underneath.

ℹ️
The chip runs from 3V3, not from the VCC pin of the board. VCC is the 5 V arriving over USB, and a 74HC595 powered at 5 V needs 3.5 V to recognise a high, more than the 3.3 V pins of the board can produce. It would work on the bench and fail somewhere else.
White wire carrying 3V3 to the shift register VCC pin
White: j29 (3V3) to j14, the VCC pin of the chip

5. Tie Master Reset high

The red wire runs i20 to i14, pin 10 (MR) across to pin 16 (VCC). MR wipes the whole register whenever it is low, so it has to be held high, and the nearest high is the supply pin you just connected.

Red jumper wire connecting MR to VCC on the shift register
Red: i20 to i14, Master Reset tied to the supply

6. Enable the outputs

A second short blue jumper takes j17 (pin 13, OE) down to the bottom blue rail. Output Enable disconnects all eight outputs whenever it is high, so it has to be held low.

Short blue jumper connecting Output Enable to the bottom ground rail
Blue: j17 (pin 13, OE) down to the bottom blue − rail

7. Connect the data line

The dark green wire runs j25 (IO2) to j16, pin 14, DS. This is the pin the bits themselves travel over, one after another.

Dark green wire connecting IO2 to the shift register data pin
Dark green: j25 (IO2) to j16, the data line

8. Connect the latch

The dark blue wire runs j26 (IO3) to h18, pin 12, ST_CP. This is the one that makes all eight outputs change together instead of flickering through every step.

Dark blue wire connecting IO3 to the shift register latch pin
Dark blue: j26 (IO3) to h18, the latch

9. Connect the clock

The teal wire runs j27 (IO4) to g19, pin 11, SH_CP. Like the power wire it takes the long way round below the board.

That completes the wiring of the chip:

WireFromToChip pinWhy
Blue (short)a21top rail8 · GNDChip ground
Whitej29 (3V3)j1416 · VCCChip power
Redi20i1410 · MR to VCCMaster Reset held high
Blue (short)j17bottom rail13 · OEOutput Enable held low
Dark greenj25 (IO2)j1614 · DSData
Dark bluej26 (IO3)h1812 · ST_CPLatch
Tealj27 (IO4)g1911 · SH_CPClock
All seven power and control wires connected to the shift register
Teal: j27 (IO4) to g19, the clock and the last of the seven control wires
ℹ️
Every hole from here on is given exactly, so you can follow them one for one. Each LED sits in the outermost column, a on the a–e side and j on the f–j side, so its short leg reaches the rail without needing a jumper. Remember that the five holes in a row are all one connection: a16 and c16 are the same electrical point, which is how the resistor in the next step reaches the chip pin without a wire.

10. First 330 Ω resistor

Now the outputs. The first chain starts at Q3, pin 3, on the a–e side at row 16. The resistor reaches the chip pin directly, so this chain is the only one that needs no feed wire: one leg in c16, the other in c11.

330 ohm resistor bridging row 16 to row 11 on the a-e side
The first 330 Ω, c16 to c11, straight off the Q3 pin of the chip

11. First LED, bit 3

The long leg (anode) goes into a11, the row where the resistor ends. Column a is the outermost one, so the leg is as close to the rail as it can get. The short leg (cathode) bends straight into the top blue rail, which saves a jumper on every LED in this build.

Red LED with its anode in row 11 and its cathode bent into the ground rail
Bit 3 complete: the LED that counts 8

12. Feed wire for bit 2

Q2 is pin 2, at a15. Its LED goes on the other side of the channel, so an orange wire carries the output across to h11.

Orange feed wire carrying Q2 across the centre channel
Orange: a15 (Q2) across to h11 on the f–j side

13. Second 330 Ω resistor

From i11 to i8, continuing the chain on the f–j side.

Second 330 ohm resistor added on the f-j side
The resistor of bit 2, i11 to i8

14. Second LED, bit 2

Anode into j8, cathode bent into the bottom blue rail this time, since this chain lives on the f–j side.

Yellow LED added on the f-j side
Bit 2 complete: the LED that counts 4

15. Feed wire for bit 1

A second orange wire takes Q1 from a14 to d8, staying on the a–e side.

Second orange feed wire added for Q1
Orange: a14 (Q1) to d8

16. Third 330 Ω resistor

From c8 to c5.

Third 330 ohm resistor added on the a-e side
The resistor of bit 1, c8 to c5

17. Third LED, bit 1

Anode into a5, cathode into the top blue rail.

Second yellow LED added on the a-e side
Bit 1 complete: the LED that counts 2

18. Feed wire for bit 0

Q0 is pin 15, on the f–j side at j15, the only output on that side of the chip. A white wire takes it along to h5.

White feed wire carrying Q0 along the f-j side
White: j15 (Q0) to h5

19. Fourth 330 Ω resistor

From i5 to i2, the last one.

Fourth 330 ohm resistor added on the f-j side
The resistor of bit 0, i5 to i2

20. Fourth LED, bit 0

Anode into j2, cathode into the bottom blue rail. All four chains are now in place:

BitCountsChip pinFeed wire330 ΩLED anodeCathode
383 · Q3 (a16)none neededc16 to c11a11top rail
242 · Q2 (a15)a15 to h11i11 to i8j8bottom rail
121 · Q1 (a14)a14 to d8c8 to c5a5top rail
0115 · Q0 (j15)j15 to h5i5 to i2j2bottom rail
ℹ️
The LEDs zigzag across the centre channel instead of sitting in one straight line. That is not untidiness: Q0 is on the opposite side of the chip from Q1, Q2 and Q3, and four 10 mm LEDs will not fit side by side in the rows that are left. Reading along the board away from the chip you still get bit 3, bit 2, bit 1, bit 0, which is the order that matters.
All four LED chains complete on the breadboard
Bit 0 complete: all four chains in place

21. Plug in the USB-C cable

The finished shift register circuit with the USB-C cable connected
The finished circuit

How three pins become eight

A shift register is a row of eight memory cells with a door at one end. DS is the door. Every time the clock pin goes up, whatever is sitting on DS moves into the first cell and every cell passes its contents along to the next one. Eight clock pulses, eight bits stored.

If the outputs changed while that was happening, you would watch the bits stampede across the LEDs on their way to their seats. They do not, because of the third pin. ST_CP, the latch, holds a private copy: the outputs keep showing the previous eight bits until you raise the latch, and then all eight change at the same instant.

That is the whole trick, and it is why the script does the same three things every time round:

  1. Pull the latch low to stop the outputs changing
  2. Call shift_out() to send eight bits, clocking each one in
  3. Pull the latch high to show them all at once

Two more pins have to be told how to behave, and they are the ones people forget. MR (Master Reset) wipes the register whenever it is low, so it is tied high. OE (Output Enable) disconnects the outputs whenever it is high, so it is tied low. Neither is optional: a CMOS input left floating picks up whatever electrical noise is nearby and does as it pleases.

Writing shift_out() yourself

Arduino has a ready-made shiftOut(). MicroPython does not, so the script includes its own, and it is worth reading, because it is the clearest look at bit manipulation in the whole kit:

def shift_out(value):
for i in range(7, -1, -1):
bit = (value >> i) & 1
data_pin.value(bit)
clock_pin.value(1)
clock_pin.value(0)

Three ideas are doing all the work:

  • range(7, -1, -1) counts down: 7, 6, 5, … 1, 0. The three numbers are start, stop and step, and the stop is never included, which is why it ends at -1 to reach 0. Counting down is what sends the most significant bit first, which is the order the 74HC595 expects.
  • value >> i is a right shift: it moves every bit of the number i places to the right, so the bit we care about ends up at the bottom.
  • & 1 is a bitwise AND with 1, which keeps only that bottom bit and discards everything above it.

Put together, (value >> i) & 1 reads out bit number i of the value, on its own, as a 0 or a 1. Setting data_pin to it and then pulsing the clock up and down is one bit delivered.

ℹ️
Because data enters at Q0 and shifts along, the first bit sent travels furthest and ends up at Q7, while the last bit sent stays at Q0. That is what makes Q0 the ones, Q1 the twos, Q2 the fours and Q3 the eights.

Why only four LEDs? The chip has eight outputs, but counter & 0x0F keeps the value inside four bits, so Q4 to Q7 are always zero. 0x0F is 15, which in binary is 1111; combining any number with 1111 this way keeps its lowest four bits and throws the rest away. Pin 9 (Q7') is the spare bit falling out of the far end. That is what you would wire to the DS of a second chip to chain two registers into sixteen outputs.

ℹ️
counter = (counter + 1) % 16 is the same wrap-round trick as the alarm clock uses for hours and minutes. % gives the remainder, so 16 becomes 0 and the counter starts again.

Code

from machine import Pin
import time

# This is a variable to which we pass the number of pin that we had connected the data pin of the shift register to,
# marked DS on the chip. This is the pin the bits themselves travel over, one after another.
#
# Remember that each of the four LEDs on the shift register outputs needs its own 330 Ohm resistor in series with it,
# exactly as if it were wired straight to the board.
DATA_PIN = 2

# This is a variable to which we pass the number of pin that we had connected the latch pin of the shift register to.
# On the 74HC595 chip this pin is marked ST_CP. The latch is what tells the chip "the data I sent you is complete,
# show it now", which is why the LEDs change all at once instead of flickering through every step.
LATCH_PIN = 3

# This is a variable to which we pass the number of pin that we had connected the clock pin of the shift register to,
# marked SH_CP on the chip. The clock is a pin we switch up and down, and every time it goes up the chip takes in one
# more bit.
CLOCK_PIN = 4

# Here we create our three Pin objects. Pin.OUT tells the board that these pins should write a value instead of
# reading one, since all three of them send information to the chip.
data_pin = Pin(DATA_PIN, Pin.OUT)
latch_pin = Pin(LATCH_PIN, Pin.OUT)
clock_pin = Pin(CLOCK_PIN, Pin.OUT)

# Start with all three pins low, so the chip begins from a known state.
data_pin.value(0)
latch_pin.value(0)
clock_pin.value(0)

# This variable holds the value of our counter. Each pass through the loop it grows by one, and after 15 it starts
# over from zero.
counter = 0


def shift_out(value):
# This is a function we wrote ourselves, because there is no ready-made function for shifting a byte out.
# It sends one byte out over the data pin, one bit at a time, pulsing the clock pin after each bit.
# range(7, -1, -1) counts down from 7 to 0, so we start with the most significant bit, meaning the leftmost one,
# which is the order the 74HC595 expects.
# Shifting the value right by i positions and combining it with 1 using the bitwise AND ("&") is how we pick out
# the single bit we want to send.
for i in range(7, -1, -1):
bit = (value >> i) & 1
data_pin.value(bit)
clock_pin.value(1)
clock_pin.value(0)


while True:

# Here we keep our counter inside four bits. The "&" is a bitwise AND, and 0x0F is the hexadecimal way of writing
# the number 15, which in binary is 1111. Combining a number with 1111 this way keeps only its lowest four bits.
value = counter & 0x0F

# Pulling the latch pin low tells the chip that we are about to send new data and that it should not change its
# outputs yet. Without this the LEDs would visibly flicker while the bits are still arriving.
latch_pin.value(0)

# Send the eight bits of our value to the chip, using our own function above.
shift_out(value)

# Pulling the latch pin back high tells the chip that the data is complete. Only now do the outputs change, and
# all eight of them change together.
latch_pin.value(1)

# Print the value too, so you can compare the number with the pattern of lit LEDs.
print(value)

# Count one up, and start over from zero once we pass 15, because four LEDs cannot show any number higher than
# that. The "%" operator gives the remainder of a division, which is a short way of wrapping a counter around.
counter = (counter + 1) % 16

# time.sleep() starts a pause in the code, given in seconds. Half a second is slow enough to follow the counting
# with your eyes. Feel free to experiment with this value.
time.sleep(0.5)

What you should see

The four LEDs count from 0 to 15 and start again. Each number is held for half a second, so one full lap takes eight seconds.

Reading a number off the LEDs is a matter of adding up the ones that are lit. Along the board, away from the chip, the four are worth 8, 4, 2 and 1. So one LED on its own at the far end is 1; the two nearest the chip together are 8 + 4 = 12; all four lit is 15, the largest number four LEDs can show, and the next step after that is back to 0.

Unlike the Arduino version of this project, the script also prints each number as it displays it, so you can check the LEDs against the count:

0
1
2
3
4
5

Watch the 1 LED for a while and you will see the pattern that makes binary counting work: it changes every single step. The 2 changes half as often, the 4 half as often again, and the 8 only twice in the whole lap. Each LED flips at exactly half the rate of the one before it. The printed column shows the same thing in numbers.

If nothing lights up at all, unplug the USB cable and check the notch on the chip first. A 74HC595 seated the wrong way round is by far the most common cause, and it is worth catching quickly: turning the chip end for end swaps rows 14 and 21 across the channel, so the 3V3 wire lands on pin 8 (GND) and the ground jumper lands on pin 16 (VCC). The chip then has its supply backwards rather than absent, which forward-biases the diode inside it that is normally reverse-biased. It will not work, it may get noticeably warm, and it can be damaged for good, so do not leave it powered while you work out what is wrong. If the LEDs are lit but never change, the clock or latch wire is in the wrong row; the console counting up while the LEDs sit still proves the fault is in the wiring and not the code. If one LED never lights, its own chain is at fault, and the other three prove the chip is fine.

ℹ️
Pressing Stop leaves whatever number was on display frozen on the LEDs. The latch is holding it, and nothing clears it until the next run or a reset of the board.

Full example

Check out the full example code on the link below:

7.5_Shift_Register.py

Project demonstrating how to use a 74HC595 shift register to control four LEDs as a 4-bit binary counter using only three NULA MINI pins.