Built-in project · Raspberry Pi Pico W

Pico W 74HC595 fills eight LEDs

MicroPython on a Pico W: a 74HC595 with DS on GP0, STcp on GP1 and SHcp on GP2 lights eight LEDs one more at a time, then clears them. The whole build, a Raspberry Pi Pico W and 17 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $7.55. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A shift register adds outputs using three pins
  • Bit shifting and masking in MicroPython
  • Building a pattern with (1 << n) - 1
  • Powering the chip from 3.3 V
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 20

How it works

A Pico W runs MicroPython and talks to a 74HC595 shift register with three pins: DS on GP0, STcp (the latch) on GP1 and SHcp (the clock) on GP2. The chip's eight outputs Q0 to Q7 each drive a red LED through a 220 ohm resistor.

To set the LEDs, the program puts one bit at a time on DS and pulses the clock, eight times. Then it pulses the latch, and all eight LEDs change together.

The loop fills the LEDs one at a time, 150 ms apart, from none to all eight, then empties them the same way. It also prints each pattern in binary.

Parts list

20 parts, plus the jumper wires. They retail for about $7.55; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
74HC595 shift register pin DSRaspberry Pi Pico W pin GP0
74HC595 shift register pin STCPRaspberry Pi Pico W pin GP1
74HC595 shift register pin SHCPRaspberry Pi Pico W pin GP2
Resistor, 220 Ω (2) pin 174HC595 shift register pin Q1
Resistor, 220 Ω (3) pin 174HC595 shift register pin Q2
Resistor, 220 Ω (4) pin 174HC595 shift register pin Q3
Resistor, 220 Ω (5) pin 174HC595 shift register pin Q4
Resistor, 220 Ω (6) pin 174HC595 shift register pin Q5
Resistor, 220 Ω (7) pin 174HC595 shift register pin Q6
Resistor, 220 Ω (8) pin 174HC595 shift register pin Q7
74HC595 shift register pin GNDGround
74HC595 shift register pin MR3.3 V
74HC595 shift register pin OEGround
Resistor, 220 Ω (1) pin 174HC595 shift register pin Q0
74HC595 shift register pin VCC3.3 V
LED, red (1) pin AResistor, 220 Ω (1) pin 2
LED, red (1) pin CGround
LED, red (2) pin AResistor, 220 Ω (2) pin 2
LED, red (2) pin CGround
LED, red (3) pin AResistor, 220 Ω (3) pin 2
LED, red (3) pin CGround
LED, red (4) pin AResistor, 220 Ω (4) pin 2
LED, red (4) pin CGround
LED, red (5) pin AResistor, 220 Ω (5) pin 2
LED, red (5) pin CGround
LED, red (6) pin AResistor, 220 Ω (6) pin 2
LED, red (6) pin CGround
LED, red (7) pin AResistor, 220 Ω (7) pin 2
LED, red (7) pin CGround
LED, red (8) pin AResistor, 220 Ω (8) pin 2
LED, red (8) pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Microchip 74HC595: eight LEDs from three pins. Bits go in on DS (GP0)
# one SHcp (GP2) pulse at a time, and an STcp (GP1) pulse shows all
# eight at once. The LEDs fill up one by one, then clear.
from machine import Pin
import time

ds = Pin(0, Pin.OUT)
latch = Pin(1, Pin.OUT)
clock = Pin(2, Pin.OUT)

def show(byte):
    for i in range(7, -1, -1):        # Q7 first, so Q0 ends up last
        ds.value((byte >> i) & 1)
        clock.value(1)
        clock.value(0)
    latch.value(1)
    latch.value(0)

while True:
    for n in range(9):
        byte = (1 << n) - 1           # 0, 1, 3, 7 ... 255
        show(byte)
        print("{:08b}".format(byte))
        time.sleep_ms(150)
    for n in range(8, -1, -1):
        show((1 << n) - 1)
        time.sleep_ms(150)

Code walkthrough

The key functions of the sketch, and what each one does.

show main.py

Sends a byte to the register. It goes from bit 7 down to bit 0. For each bit it sets ds to that bit, then raises and lowers clock, which shifts the bit in. After eight bits it pulses latch once so the outputs update. Sending bit 7 first leaves bit 0 on Q0.

def show(byte):
    for i in range(7, -1, -1):        # Q7 first, so Q0 ends up last
        ds.value((byte >> i) & 1)
        clock.value(1)
        clock.value(0)
    latch.value(1)
    latch.value(0)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • No LEDs light This project
    OE must go to ground and MR to 3.3 V. The chip is powered from 3.3 V here.
  • The LEDs fill from the wrong end This project
    Check that Q0 goes to the first LED and Q7 to the last, and that GP0, GP1 and GP2 go to DS, STCP and SHCP.
  • Outputs show garbage, or the register never clears. 74HC595 shift register
    Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte.
  • The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
    Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.