7.5. Shift Register

Short example demonstration video
In this project we will use 74HC595 shift register to implement a 4 bit binary counter, displaying values from 0 to 15 using four LEDs with only 3 pins.
✅
At the end of this example you will learn:
- How 74HC595 shift register works
- Bit manipulation (bit shifting)
- How to implement a counter and display it with LEDs
ℹ️
Parts required:
- Soldered NULA Mini Board
- Breadboard
- 1 × 74HC595 shift register
- 4 x LEDs
- 4 x 330Ω resistors
- Jumper wires

Required components
Putting the components together

Components connected together
Code Example
from machine import Pin
import time
# Bit value to send to the shift register
DATA = Pin(18, Pin.OUT)
# Clock pin, tells the shift register to read the data bit
CLOCK = Pin(19, Pin.OUT)
# Latch pin, when pulsed, updates the output pins of the shift register
LATCH = Pin(20, Pin.OUT)
# Send one byte to the shift register
def shift_byte(value):
# Shift out 8 bits, LSB first
for i in range(8):
DATA.value((value >> i) & 1)
CLOCK.value(1)
CLOCK.value(0)
# Pulse the latch line
def latch():
LATCH.value(1)
LATCH.value(0)
# Show a 4-bit nibble on the shift register outputs
def show_nibble(n):
shift_byte(n & 0x0F)
latch()
# Main loop that counts from 0 to 15 and displays with LEDs
count = 0
while True:
show_nibble(count)
time.sleep(0.3)
# Increment the counter and wrap it from 15 back to 0
count = (count + 1) & 0x0F