Easy configuration and control of 74HC595 Shift Registers on a Raspberry Pi
Project description
Pizazz
A utility class to leverage 74HC595 shift register chips with a Raspberry Pi.
The 74HC595 shift register is an incredibly useful chip. A single chip has 8 output pins which can be controlled with only 3 input pins (excluding Vcc and Gnd of course). That is great in itself however 595's can be daisy-chained together to give you multiples of 8 pin outputs yet still always controlled by only 3 input pins! Wow!
If you are not sure why this is useful then let me explain.
I had a requirement to create a LED "Status Board" for a monitoring and automation application that I am also writing. The status board would reflect the current operation status of things like Jenkins jobs, Github Actions, Linux services etc etc. I needed a minimum of 16 LEDs. Now there already exists a status board HAT. However it only tracks 5 items (that is 10 LED's). However, each LED requires it's own GPIO and the HAT masks all other pins making them unavailable.
Using the Raspberry RPi.GPIO library it is possible to individually switch the 27 GPIO pins. However each LED would require a wire from the GPIO pin. This is very physically unwieldy and clunky to control in Python.
Enter the 74HC595...
This class enables you to individually control any number of LEDS (or other output devices) with only 3 GPIO pins.
Basic Wiring of the 74HC595 8-bit shift register to a Raspberry Pi
Pin | Tag | Description |
---|---|---|
1 - 7 | Q1 - Q7 | Parallel Data output pins 1-7 |
8 | Gnd | Ground |
9 | Q7-> | Serial data output pin |
10 | MR | Master Reset |
11 | SH | Clock pin |
12 | ST | Latch pin |
13 | OE | Output enable |
14 | DS | Serial data input |
15 | Q0 | Parallel data output pin 0 |
16 | Vcc | Positive voltage supply |
Chaining 2 or more shift registers together
How the register works
The 595 has two registers (which can be thought of as “memory containers”), each with just 8 bits of data.
- The Shift Register
- The Storage Register
Whenever we apply a clock pulse to a 595, two things happen:
-
The bits in the Shift Register move one step to the left. For example, Bit 7 accepts the value that was previously in bit 6, bit 6 gets the value of bit 5 etc.
-
Bit 0 in the Shift Register accepts the current value on DATA pin. At the rising edge of the pulse, if the data pin is high, then a 1 gets pushed into the shift register. Otherwise, it is a 0.
On enabling the Latch pin, the contents of Shift Register are copied into the Storage Register. Each bit of the Storage Register is connected to one of the output pins Q0–Q7 of the IC, so in general, when the value in the Storage Register changes, so do the outputs.
Installation
Raspberry Pi:
pip3 install pizazz
Connecting the Raspberry Pi
The 40 pins of the Raspberry Pi are GPIO, 5v, 3.3V and ground. Some of the GPIO pins can have special purposes. However, all of them can be controlled by the RPi.GPIO Python Library. The RPi.GPIO requires that you specify how you will identify the pins that you use. There are 2 ways:
-
GPIO.BOARD: option specifies that you are referring to the pins by the number of the pin.
-
BCM: option means that you are referring to the pins by the "Broadcom SOC channel" number, these are the numbers after "GPIO"
So referring to the diagram below: BCM mode GPIO2 is the same as BOARD mode pin 2
Connect any GPIO's to the clock, latch and data pins of the register and connect the the 5v supply and earth as indicated in the register diagram. If you are connecting the outputs to LED's then you need to wire 330 ohm resistors serially to protect them in the usual way.
Library Usage examples
For more examples and usage, please refer to the Wiki.
Import the library
from pizazz.pizazz import HC595
Instantiate the library passing the initialisation arguments
shifter = HC595(mode="BCM", data=17, clock=27, latch=18, ics=2)
the 'ics' parameter defines the number of registers daisey-chained together.
There are four public methods in the library:
Method | Description |
---|---|
clear() | sets shift and storage registers to zero |
test() | Cycles sequentially through all outputs |
set_output() | explicitly sets specific pin outputs |
set_pattern() | sets output using a bit pattern |
1. Using the set_output(output, mask) method
Args:
output (int) - decimal value of the binary bits you want to set to "on"
mask (int) - decimal value of the binary bits to consider in this operation.
Consider the following setup:
Using a mask has 2 benefits:
- It enables the library to explicitly turn LEDS 'off'. e.g. sending an output value of 16 means turn pin 5 'on'. it has no concept of turning pin 6 'off'. Using a mask facilitates this.
- It isolates the pins to consider in the update. For a status board this is important. The inputs from the sensors can now be considered in isolation from the other sensors making asynchronous updates possible.
Consider sensor 2:
method values | LED3 | LED4 |
---|---|---|
set_output(0, 12) | OFF | OFF |
set_output(4, 12) | ON | OFF |
set_output(8, 12) | OFF | ON |
set_output(12, 12) | ON | ON |
NOTE: All other LED outputs remain the same and are untouched by these operations
This now makes programming the shift register a simple process. e.g. consider a Jenkins job
jenkins_mask = 48
jenkins_pass = 16
jenkins_fail = 32
# 'sensor' receives a failing indication
shifter.set_output(jenkins_fail, jenkins_mask)
# 'sensor' receives a passing indication
shifter.set_output(jenkins_pass, jenkins_mask)
The second value is the bit mask (similar to an IP bit mask) - Explained later
2. Using the set_pattern(chip_pattern) method
Args:
chip_pattern (List or Nested List) - Bit pattern representing the pins to set 'on' or 'off'. If more than two registers are used then the pattern should be a nested list.
Using the bit pattern (for a two chip configuration)
shifter.set_pattern([[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
For a single chip a simple list should be used:
shifter.set_pattern([0, 0, 1, 1, 0, 0, 0, 0])
Documentation
Meta
Stephen R A King : sking.github@gmail.com
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pizazz-1.3.5.tar.gz
.
File metadata
- Download URL: pizazz-1.3.5.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.4 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bec417f7c39cd277badb222b1beb575b6815fcda9ab7dc949ab75121c733090f |
|
MD5 | 18f3a6bab743e7d15bacf561abfd5988 |
|
BLAKE2b-256 | 35547784d4c78c56f290f04590a8f42765fe76ffb171767edad19dc87fc86661 |
File details
Details for the file pizazz-1.3.5-py3-none-any.whl
.
File metadata
- Download URL: pizazz-1.3.5-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.4 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd2ddfd0b9292394b287234465b49429548b117b09501777aa7d297778ac4bea |
|
MD5 | 32f3e117383da54d5b120a50e019d594 |
|
BLAKE2b-256 | f4c79b272a203d948de39d36f1ee2ab00158adeefd73864e4d34d667663031e3 |