How to use 74hc595 Shift Register with Raspberry PI Pico and MicroPython
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).
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.
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 the needed hardware, so that at the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:
- A common computer (maybe with Windows, Linux or Mac). It can also be a Raspberry PI Computer board
- Raspberry PI Pico microcontroller (with a common micro USB cable)
- Dupont Wiring
- Solderless breadboard
- 74HC9595 chip
You can also evaluate the good Elegoo kit which includes all electronics required (except Raspberry PI Pico).
Check hardware prices with the following links:
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.
Please find below some pictures with details:
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:
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:
Interested to do more with your Raspberry PI Pico? Try to look at my Raspberry PI Pico tutorials for useful and funny projects!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
7 thoughts on “How to use 74hc595 Shift Register with Raspberry PI Pico and MicroPython”
Thanks for posting this. I have your program running fine but I am puzzling over whether it is really necessary to convert the bit sequence to a string before loading the bits on to the data pin of the 74HC595.
That said, I have just noticed an error in your write up. Towards the end, just before the link to your sample pico74HC595.py code, you have written:
“Now download my main file to test your DHT11 sensor:
!! Not a big deal but might cause some confusion, so you might want to fix this!
(Incidentally I have obtained a DHT11 and I got it working without too much difficulty.)
So I have figured out a slightly simpler way to get the control bits into the DATA pin without using string representations. Here is my code for the shift_update() function and how to call it.
def shift_update(data_bits, data, clock,latch): # Here data_bits is an integer; using “input” for this
# argument is probably not a good idea, as “input”
# seems to be a reserved word
#put latch down to start data sending
# clock.value(1) # Not needed? It just gets set to 1 and immediately set back to 0
#load data in reverse order
for i in range(7, -1, -1):
data.value(data_bits & (1 << i)) # Use bitwise operators directly on the input value
#put latch up to store data on register
#main program, calling shift register() function
bit_pattern = urandom.randint(0, 255) # This is actually more like a genuine random number
shift_update(bit_pattern, dataPIN, clockPIN, latchPIN)
Hi David, sorry for the late reply.
Every code logic can be expressed in several ways, all working. Working with strings, you can convert the value, before passing t function, with the str() python operator.
The clock value to 1, just before the revert to 0 at the start of function, is required according to shift register working diagrams.
Regarding the confusing phrase, please can you help me understanding what was the misunderstanding, so that I can improve it?
Many thanks for your feedback!
Thanks for responding. Absolutely no problem about a late reply! For sure, there are many ways to skin the coding cat…. but in general whatever runs fastest is best. I really doubt that creating strings from ints and then converting the strings back to single bit integers to load the data register can be as fast as creating an int and loading its bits directly.
Interesting that setting the clock value to 1 and then reverting to 0 is required. My code runs perfectly well with that line commented out.
The confusion I mentioned is simply that you evidently copied and pasted a text from another article which was about the DHT11 humidity sensor! So your text about getting code for programming a multiplexing circuit actually says:
“Now download my main file to test your DHT11 sensor:”
Like I said, it’s not a big deal but it could cause confusion.
Thanks for all your efforts on this. It is immensely helpful to people like me getting up the microPython learning curve.
Hi, how can manage 3 or n shift register???
To manage multiple Shift registers, you can put them in serial connection or in parallel.
In serial connection, you attach the serialout of first SR to data of second and so on. Latch for allow SR are connected together, same forclock. You can use my code, running it as times as the number of connected SR:at the first update you load data for last SR, then you update data for the prev one and so in up to the first.
Parallel configuration require one GP for each data, while clock and latch are still together. In this case you need to change my code.
For quite all project, the serial configuration is the best option as the SR are so fast that user will never see intermediate effects
Thanks a lot peppe8o. Works like a charm… thanks for help.