Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

How to use 74hc595 Shift Register with Raspberry PI Pico and MicroPython

Raspberry PI Pico 74hc595 shift register featured image
5
(1)
74hc595 shift register chip

Complex Raspberry PI Pico projects can require many connections with many sensors/devices so that the rich 40-PIN GPIO can become limited. In these cases you can get help from a simple and cheap electronic piece: the 74HC595 Shift Register, also known as Serial to Parallel Converter.

In this tutorial, I’m going to show you how to use the 74hc595 chip with a Raspberry PI Pico and MicroPython, in an easy configuration that drives 8 LEDs. I also will add an example code showing how to use it with a random LED effect.

Plese note that if you want to connect this chip with a Raspberry PI computer board (like Raspberry PI Zero W, RPI 4 Model B and so on)you can refer to my Using 74hc595 Shift Register with Raspberry PI tutorial.

What is the Shift Register

The shift register chip has been already explained in detail in my Using 74hc595 Shift Register with Raspberry PI, so I’m not going to deep again its explanation. However, a brief refresh of how input and output are linked is useful to remind its use.

The shift register acquires a serial data flow from one of its PINs (data PIN), store these data and expose them in its 8 output PINs (Q0, Q1, …Q7).

RPI Shift register serial to parallel

Each output pin can have 0 (off) or 1 (on) value. To set each of these values on or off, we feed in the data using the data and clock PINs of the chip in a precise timing diagram. The clock needs to receive nine pulses. At each pulse (on rising edge), if the data PIN is high then a 1 gets pushed into the shift register; otherwise, a 0.

When all eight pulses have been received, enabling the ‘latch’ pin stores those eight values to register and exposes them in output PINs.

74hc595 Shift register pinout

Beside output PINS (Q0…Q7), data, clock and latch, other PINS are exposed from 74hc595:

  • Vcc and GND: of course, 5v and ground to power output
  • OE (Output Enable): This PIN enables or disables output. 74hc595 exposes output when OE is low (0)
  • MR (Master Reclear): This PIN cleans memory when put to 0. So, having 74hc595 working means MR connected to 1.
  • serOut (Serial Output): is used when you need to drive a second shift register attached to this one.

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from the shopping cart. So, hardware will be only:

raspberry pi pico microcontroller

You can also evaluate the good Elegoo kit which includes all electronics required (except Raspberry PI Pico).

Check hardware prices with the following links:

amazon raspberry pi pico box
Amazon Dupont Wiring box
Amazon Breadboard box
Amazon Resistors box
Amazon 74hc595 shift register box

Wiring Diagram

Please find in following picture the wiring diagram for this project. Note that LED longer PIN (positive) goes toward Shift Register. SerOUT remains unconnected. For microcontroller ports, you can refer my Raspberry PI Pico pinout article.

Raspberry PI Pico 74hc595 shift register wiring diagram

Please find below some pictures with details:

Raspberry PI Pico 74hc595 shift register details 03
Raspberry PI Pico 74hc595 shift register details 04
Raspberry PI Pico 74hc595 shift register details 05

Step-by-step Procedure

Prepare cabling according to the previous paragraph. Connect RPI Pico to Thonny (you can refer to my tutorial about the First steps with Raspberry PI Pico).

Get and Understand my pico74HC595.py Code

Now download my main file to test your DHT11 sensor:

I’ll explain all code lines in the following paragraphs.

First of all, required libraries are imported:

from machine import Pin
import utime
import random

utime and random are not strictly required to use the 74hc595 shift register, but it is only required for our test example.

Then PIN are assigned to variables for better code management and understanding. These values match showed wiring diagram. If you use different GPs, please edit according to your cabling (ports number can be found in Raspberry PI Pico pinout):

dataPIN = 13
latchPIN = 15
clockPIN = 14

With next rows we accomplish two actions with each single line. Each line converts the dataPIN, latchPIN or clockPIN variable (which are simple integer numbers) into micropython PIN objects and set them to output:

dataPIN=Pin(dataPIN, Pin.OUT)
latchPIN=Pin(latchPIN, Pin.OUT)
clockPIN=Pin(clockPIN, Pin.OUT)

We use a function to manage the shift register update call. This function requires 4 parameters:

  • input -> a string containing 8 chars composed only of “1” and “0”
  • data -> to identify data transmission PIN
  • clock -> to identify clock signal PIN
  • latch -> to identify latch PIN

This function also manages clock signal before and after each step:

  • latch to zero,
  • data transmission
  • latch to 1

We transmit data in reverse order because Q7 output must enter the data port first, followed by other data according to timing diagram.

def shift_update(input,data,clock,latch):
  #put latch down to start data sending
  clock.value(0)
  latch.value(0)
  clock.value(1)
  
  #load data in reverse order
  for i in range(7, -1, -1):
    clock.value(0)
    data.value(int(input[i]))
    clock.value(1)

  #put latch up to store data on register
  clock.value(0)
  latch.value(1)
  clock.value(1)

With this function available, if you want (for example) to set all outputs to 0 you can use simply:

shift_update("00000000",dataPIN,clockPIN,latchPIN)

But our test code also implements a usage example. This example starts from a configuration with all LEDs off. Then starts an infinite loop generating at each iteration a random number (0 or 1) and assigning it to Q0. All other outputs are shifted by one position, so the previous Q0 value becomes Q1, Q1 goes to Q2 and so on, with Q7 value disappearing. Finally we set a time delay to make this effect visible to human eyes:

bit_string="00000000"

while True:
    shift_update(bit_string,dataPIN,clockPIN,latchPIN)
    bit_string = str(random.randint(0, 1))+bit_string[:-1]
    utime.sleep(0.3)

Running the pico74HC595.py Script

Run this script in your Thonny IDE (F5) and your LEDs will start with a result similar to the one from the following youtube video:

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy