Raspberry PI GPIO ports are a really useful resource and allow PI to interface real world with a wide number of external sensors. However, some projects requires so many connections with many sensors/devices that the rich 40-PIN GPIO can become limited. In these cases you can get help from a simple and cheap electronic piece: the Shift Register or Serial to Parallel Converter.
In this tutorial I’m going to show you how to use the 74hc595 chip with a Raspberry PI and python, in an easy configuration which drives 8 LEDs.
I’m going to use a Raspberry PI Zero W, but this procedure works also with other Raspberry PI boards.
The shift register chip is an electronic element able to acquire 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:
As you can see in this picture, it is important that your data to transfer is stable before rising edge of clock signal because this is the moment when transfer is done.
Beside output PINS (Q0…Q7), data, clock and latch, other PINS are exposed from 74hc595:
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 to continue with the project or remove them from the shopping cart. So, hardware will be only:
You can also evaluate the good Elegoo kit which includes all electronics required (except from Raspberry PI, SD and power supply).
Check hardware prices with 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.
Please find below some pictures with details:
Start preparing your PI installing Raspberry PI OS Lite. This tutorial applies also to Raspberry PI OS Desktop, by using its internal terminal.
Make your OS up to date. From terminal:
sudo apt update -y && sudo apt upgrade -y
RPI.GPIO should be already installed (otherwise, you can get it installed with the command “sudo apt install python3-rpi.gpio”).
You can get my shift_register.py python script directly in your RPI from terminal:
wget https://peppe8o.com/download/python/shift_register.py
Code is explained in following paragraph.
Required packages are imported:
import RPi.GPIO as GPIO
import sys
Then PIN are assigned to variables for better management. These values match showed wiring diagram. If you used other PINs, please edit according to Raspberry PI BCM pinout according to your cabling:
dataPIN = 16
latchPIN = 20
clockPIN = 21
PIN are thes set to output:
GPIO.setmode(GPIO.BCM)
GPIO.setup((dataPIN,latchPIN,clockPIN),GPIO.OUT)
A function is used to manage shift register update call. This function requires 4 parameters:
This function also manages clock signal before and after each step:
Data is transmitted 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
GPIO.output(clock,0)
GPIO.output(latch,0)
GPIO.output(clock,1)
#load data in reverse order
for i in range(7, -1, -1):
GPIO.output(clock,0)
GPIO.output(data, int(input[i]))
GPIO.output(clock,1)
#put latch up to store data on register
GPIO.output(clock,0)
GPIO.output(latch,1)
GPIO.output(clock,1)
With this function available, using it inside our main script requires simply:
shift_update(sys.argv[1],dataPIN,clockPIN,latchPIN)
The “sys.argv[1]” is used to pass our byte directly as argument on script execution. Infact, sys.argv[] system property allows passing arguments from terminal to script with an array ordered as following:
$ python test.py arg1 arg2 arg3 ...
| | | |
sys.argv[0] sys.argv[1] sys.argv[2] sys.argv[3] ...
with sys.argv[0] returning “test.py”, sys.argv[1] returning “arg1” and so on.
Final operation cleans GPIO PINs status, while last update remains stored on Shift Register:
GPIO.cleanup()
Running our script requires a simple terminal command:
python3 shift_register.py 00000000
changing the string of 8 zeroes with “1” or “0” according to your need.
Some examples:
python3 shift_register.py 10101010
One more example:
python3 shift_register.py 11100011
Last example:
python3 shift_register.py 00000011
Enjoy!
In this tutorial, we will use two Arduino Uno to demonstrate the Serial Peripheral Interface…
In this tutorial, we will be making an automatic irrigation system (AIS) with Arduino and…
This tutorial will show you how to use Python Virtual Environment with Raspberry PI computer…
This tutorial will show you how to get betting odds with Raspberry PI by using…
This tutorial will show you how to perform the backup of Raspberry PI (computer board)…
This tutorial will show you how to install Honeygain on a Raspberry PI computer board…
View Comments
Excellent, thank you !. I can understand now this shift register.
Do you know a similar post about SN74LS165 parallel to serial SR ?
Hi Jean.
Thank you for your feedback. Honestly, I've never get a SN74LS165. From what I googled, this is a parallel to serial device and could make sense only as input for Raspberry PI (as for output the RPI is already capable to transmit in serial). So, something similar to the current code should applg also for these chips