MicroPython driver for 74HC595 shift registers

Overview

MicroPython 74HC595

A MicroPython library for 74HC595 8-bit shift registers.

There's both an SPI version and a bit-bang version, each with a slightly different interface.

demo

SPI Version

You can use either HSPI or SPI. This version is significantly faster than the bit-bang version, but is limited to writing whole bytes of data.

SPI Example

Basic Usage

from machine import Pin, SPI
from sr_74hc595_spi import SR

spi = SPI(1, 100000)
rclk = Pin(5, Pin.OUT)

oe = Pin(33, Pin.OUT, value=0)    # low enables output
srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data

sr = SR(spi, rclk, 2) # chain of 2 shift registers

sr.pin(2,1)  # set pin 2 high of furthest shift register
sr.pin(2)    # read pin 2
sr.pin(2,0)  # set pin 2 low

sr.toggle(8) # toggle first pin of closest shift register

sr[0] = 0xff # set all pins high on furthest shift register
sr[1] = 240  # set half pins high on closest shift register
sr[1]        # read pins

oe.value(0)  # disable outputs
oe.value(1)  # enable outputs

# pulse to clear shift register memory
srclr.value(1)
srclr.value(0)

SPI Methods

Construct with a reference to spi and the rclk pin used for latching and an optional number of cascading shift registers.

Pins srclr and oe are optional. If you don't need to clear the outputs, connect srclr to vcc. If you don't need to disable the outputs, connect oe to gnd.

__init__(spi, rclk, len=1, srclr=None, oe=None)

Read the boolean value of a pin. First pin is index 0. If you are cascading shift registers, the first pin of the second shift register is index 8 and so on. Index 0-7 are the furthest away shift register from the serial data in.

pin(index)

Writes a boolean value to a pin. This updates the internal buffer of pin values then writes all of the values to each shift register in the chain.

pin(index, value, latch=True)

This toggles a single pin by index. Helper for reading a pin then writing the opposite value.

toggle(index, latch=True)

This lets you treat the class like a list, where each index represents a whole shift register. Returns an 8-bit value for the shift register by index, where lowest index is furthest away.

__getitem__(index)

Write an 8-bit value to a shift register at the given index.

__setitem__(index, value)

Private method for sending the entire internal buffer over SPI.

_write(latch=False)

Private method for pulsing the rclk pin, which latches the outputs from the shift register to the storage register and makes the outputs appear.

_latch()

Bit Bang Version

This version lets you have greater control over sending individual bits of data at the expense of the performance you get using SPI.

Bit Bang Example

Basic Usage

from machine import Pin
from sr_74hc595_bitbang import SR

ser = Pin(23, Pin.OUT)
rclk = Pin(5, Pin.OUT)
srclk = Pin(18, Pin.OUT)

# construct without optional pins
sr = SR(ser, srclk, rclk)

sr.clear()  # raises RuntimeError because you haven't provide srclr pin
sr.enable() # raises RuntimeError because you haven't provide oe pin

# reconstruct with all pins
oe = Pin(33, Pin.OUT, value=0)    # low enables output
srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data

sr = SR(ser, srclk, rclk, srclr, oe)

sr.bit(1)  # send high bit, do not latch yet
sr.bit(0)  # send low bit, do not latch yet
sr.latch() # latch outputs, outputs=0000_0010

sr.bit(1, 1) # send high bit and latch, outputs=0000_0101
sr.bit(0, 1) # send low bit and latch, outputs=0000_1010

sr.bits(0xff, 4) # send 4 lowest bits of 0xff (sends 0x0f), outputs=1010_1111

sr.clear(0) # clear the memory but don't latch yet
sr.latch()  # next latch shows the outputs have been reset

sr.bits(0b1010_1010, 8) # write some bits
sr.clear()  # clear the memory and latch, outputs have been reset

sr.enable()  # outputs enabled
sr.enable(0) # outputs disabled

Bit Bang Methods

Construct with references to each of the pins needed to write to the shift register(s).

Pins ser, srclk and rclk are required. Pins srclr and oe are optional. If you don't need to clear the outputs, connect srclr to vcc. If you don't need to disable the outputs, connect oe to gnd.

__init__(ser, srclk, rclk, srclr=None, oe=None)

Writes a single value and can optionally latch to make it visible.

bit(value, latch=False)

Write multiple (num_bits) values from the supplied value and optionally can latch.

bits(value, num_bits, latch=False)

Pulses the rclk pin to latch the outputs. Without this, all of the bits you have written are remain hidden.

latch()

Clears the shift register memory by pulsing the srclr pin. You will get a runtime error unless you have provided this pin on construct.

clear(latch=True)

Toggles the output of the shift register by toggling the output enable (oe) pin. You will get a runtime error unless you have provided this pin on construct.

enable(enabled=True)

Private method for pulsing the srclk pin, which tells the shift register to read the current state of the ser pin and copy it to the shift register memory.

_clock()

Chaining

You can connect multiple 74HC595 shift registers together to form a chain.

Connect each shift registers rclk, srclk, oe and srclr together. If you don't need to disable outputs, you can tie each oe to ground. If you don't need to clear any outputs, you can tie each srclr to vcc.

Your micro controller provides data to just the first shift registers ser pin.

The QH\`` output pin on the first shift register goes into the next shift register ser` pin and so on.

When clocking in data, the values appear on the closest shift register to the micro controller first, before overflowing into each chained shift register.

Parts

Connections

TinyPICO 74HC595
3V3 VCC
G GND
G (or a pin) OE
23 MOSI SER
18 SCK SRCLK
5 RCLK
3V3 (or a pin) SRCLR
Pin Name Description
OE Output Enable Active low. Drive high to disable outputs.
SER Serial Input Serial data sent LSB first.
RCLK Storage Register Clock Pulse to latch data to outputs.
SRCLK Shift Register Clock Serial input is read on rising edge.
SRCLR Shift Register Clear Active low. Drive high to clear contents.
QA-QH Outputs 8 output pins
QH` Serial Output Connect to the next 74HC595 SER pin

Links

License

Licensed under the MIT License.

Copyright (c) 2021 Mike Causer

You might also like...
Controlling fireworks with micropython
Controlling fireworks with micropython

Controlling-fireworks-with-micropython How the code works line 1-4 from machine

Pylorawan is a Micropython wrapper for lorawan devices from RAK Wireless.
Pylorawan is a Micropython wrapper for lorawan devices from RAK Wireless.

pylorawan Pylorawan is a Micropython wrapper for lorawan devices from RAK Wireless. Tested on a Raspberry PI Pico with a RAK4200(H) Evaluation Board (

BMP180 sensor driver for Home Assistant used in Raspberry Pi

BMP180 sensor driver for Home Assistant used in Raspberry Pi Custom component BMP180 sensor for Home Assistant. Copy the content of this directory to

CircuitPython Driver for Adafruit 24LC32 I2C EEPROM Breakout 32Kbit / 4 KB

Introduction CircuitPython driver for Adafruit 24LC32 I2C EEPROM Breakout Dependencies This driver depends on: Adafruit CircuitPython Bus Device Regis

Python module for the qwiic serial control motor driver
Python module for the qwiic serial control motor driver

Qwiic_SCMD_Py Python module for the qwiic motor driver This python package is a port of the existing SparkFun Serial Controlled Motor Driver Arduino L

 Paradigm Shift in NLP -
Paradigm Shift in NLP - "Paradigm Shift in Natural Language Processing".

Paradigm Shift in NLP Welcome to the webpage for "Paradigm Shift in Natural Language Processing". Some resources of the paper are constantly maintaine

Set of scripts that schedules employees for shifts throughout the week based on availability, shift times, and shift necessities

Automatic-Scheduler Set of scripts that schedules employees for shifts throughout the week based on availability, shift times, and shift necessities *

Micropython-wifimanager-esp8266 - Simple Wifi Manager for ESP8266 using MicroPython

micropython-wifimanager-esp8266 Simple Wifi Manager for ESP8266 using MicroPytho

ENC28J60 Ethernet chip driver for MicroPython (RP2)

micropy-ENC28J60 ENC28J60 Ethernet chip driver for MicroPython v1.17 (RP2) Rationale ENC28J60 is a popular and cheap module for DIY projects. At the m

creates a batch file that uses adb to auto-install apks into the Windows Subsystem for Android and registers it as the default application to open apks.

wsa-apktool creates a batch file that uses adb to auto-install apks into the Windows Subsystem for Android and registers it as the default application

SPI driven CircuitPython driver for PCA9745B constant current LED driver.

Introduction THIS IS VERY MUCH ALPHA AND IN ACTIVE DEVELOPMENT. THINGS WILL BREAK! THIS MAY ALSO BREAK YOUR THINGS! SPI driven CircuitPython driver fo

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems
MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems

The MicroPython project This is the MicroPython project, which aims to put an implementation of Python 3.x on microcontrollers and small embedded syst

Implementation of our paper 'RESA: Recurrent Feature-Shift Aggregator for Lane Detection' in AAAI2021.
Implementation of our paper 'RESA: Recurrent Feature-Shift Aggregator for Lane Detection' in AAAI2021.

RESA PyTorch implementation of the paper "RESA: Recurrent Feature-Shift Aggregator for Lane Detection". Our paper has been accepted by AAAI2021. Intro

《Truly shift-invariant convolutional neural networks》(2021)

Truly shift-invariant convolutional neural networks [Paper] Authors: Anadi Chaman and Ivan Dokmanić Convolutional neural networks were always assumed

Simple GUI menu for micropython using a rotary encoder and basic display.

Micropython encoder based menu This is a simple menu system written in micropython. It uses a switch, a rotary encoder and an OLED display.

Official code for
Official code for "Mean Shift for Self-Supervised Learning"

MSF Official code for "Mean Shift for Self-Supervised Learning" Requirements Python = 3.7.6 PyTorch = 1.4 torchvision = 0.5.0 faiss-gpu = 1.6.1 In

Supporting code for the paper
Supporting code for the paper "Dangers of Bayesian Model Averaging under Covariate Shift"

Dangers of Bayesian Model Averaging under Covariate Shift This repository contains the code to reproduce the experiments in the paper Dangers of Bayes

Multi-Task Temporal Shift Attention Networks for On-Device Contactless Vitals Measurement (NeurIPS 2020)
Multi-Task Temporal Shift Attention Networks for On-Device Contactless Vitals Measurement (NeurIPS 2020)

MTTS-CAN: Multi-Task Temporal Shift Attention Networks for On-Device Contactless Vitals Measurement Paper Xin Liu, Josh Fromm, Shwetak Patel, Daniel M

This is official implementaion of paper
This is official implementaion of paper "Token Shift Transformer for Video Classification".

This is official implementaion of paper "Token Shift Transformer for Video Classification". We achieve SOTA performance 80.40% on Kinetics-400 val. Paper link

Comments
  • Example of using two 74hc595 to control a matrix of LEDs

    Example of using two 74hc595 to control a matrix of LEDs

    I'm trying to use this to control a matrix of LEDs by hooking the positive as rows to one SR and then the negatives as columns to another SR. I'm having them share the same rclk and srclk pins and just using different ser pins (simplifying by not having oe or srclr at least for now).

    I'm following the left half of this wiring diagram for reference.

    It would be nice if there was an example of how to use this for two different SR sharing the same clocks but different latches. No amount of fiddling seems to get them to actually work as expected.

    opened by pcon 3
Owner
Mike Causer
Mike Causer
ENC28J60 Ethernet chip driver for MicroPython (RP2)

micropy-ENC28J60 ENC28J60 Ethernet chip driver for MicroPython v1.17 (RP2) Rationale ENC28J60 is a popular and cheap module for DIY projects. At the m

null 11 Nov 16, 2022
SPI driven CircuitPython driver for PCA9745B constant current LED driver.

Introduction THIS IS VERY MUCH ALPHA AND IN ACTIVE DEVELOPMENT. THINGS WILL BREAK! THIS MAY ALSO BREAK YOUR THINGS! SPI driven CircuitPython driver fo

Andrew Ferguson 1 Jan 14, 2022
A Raspberry Pi Pico plant sensor hub coded in Micropython

plantsensor A Raspberry Pi Pico plant sensor hub coded in Micropython I used: 1x Raspberry Pi Pico - microcontroller 1x Waveshare Pico OLED 1.3 - scre

null 78 Sep 20, 2022
uOTA - OTA updater for MicroPython

Update your device firmware written in MicroPython over the air. Suitable for private and/or larger projects with many files.

Martin Komon 25 Dec 19, 2022
An open source operating system designed primarily for the Raspberry Pi Pico, written entirely in MicroPython

PycOS An open source operating system designed primarily for the Raspberry Pi Pico, written entirely in MicroPython. "PycOS" is an combination of the

null 8 Oct 6, 2022
ESP32 micropython implementation of Art-Net client

E_uArtnet ESP32 micropython implementation of Art-Net client Instalation Use thonny Open the root folder in thonny and upload the Empire folder like i

null 2 Dec 7, 2021
Micropython automatic watering

micropython-automatic-watering micropython automatic watering his code was developed to be used with nodemcu esp8266, but can be modified to work with

null 1 Nov 24, 2021
Testing out some (stolen) DMA code for RP2040 Micropython

RP2040_micropython_dma testing out some (stolen) DMA code for RP2040 Micropython. Heavy inspiration and some code from https://iosoft.blog/2021/10/26/

null 2 Dec 29, 2022
For use with an 8-bit parallel TFT touchscreen using micropython

ILI9341-parallel-TFT-driver-for-micropython For use with an 8-bit parallel TFT touchscreen using micropython. Many thanks to prenticedavid and his MCU

null 3 Aug 2, 2022
MPY tool - manage files on devices running MicroPython

mpytool MPY tool - manage files on devices running MicroPython It is an alternative to ampy Target of this project is to make more clean code, faster,

Pavel Revak 5 Aug 17, 2022