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

Owner
Mike Causer
Mike Causer
This is a python script to grab data from Zyxel NSA310 NAS and display in Home Asisstant as sensors.

Home-Assistant Python Scripts Python Scripts for Home-Assistant (http://www.home-assistant.io) Zyxel-NSA310-Home-Assistant Monitoring This is a python

6 Oct 31, 2022
Testing additional addon devices, and their working scripts

ESP32-addon-devices-potpurri Testing additional addon devices, and their micropython working scripts 📑 List of device addons tested so far Ethernet P

f-caro 0 Nov 26, 2022
Tools and documentation to aid in modifying the ADI ADALM Pluto firmware

Pluto firmware modifications This repository contains tools and documentation to aid in modifying the ADI ADALM Pluto firmware. Extraction of the Plut

Daniel Estévez 28 Dec 21, 2022
emhass: Energy Management for Home Assistant

emhass EMHASS: Energy Management for Home Assistant Context This module was conceived as an energy management optimization tool for residential electr

David 70 Dec 24, 2022
Baseline model for Augmented Home Assistant

Dataset Preparation Step 1. Rename the Virtual-Home output directory to 'vh.[name]', for example: 'vh.door' Make sure the directory contains 100+ fram

Stanford HCI 1 Aug 24, 2022
CPU benchmark by calculating Pi, powered by Python3

cpu-benchmark Info: CPU benchmark by calculating Pi, powered by Python 3. Algorithm The program calculates pi with an accuracy of 10,000 decimal place

Alex Dedyura 20 Jan 03, 2023
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

8 Oct 06, 2022
Software framework to enable agile robotic assembly applications.

ConnTact Software framework to enable agile robotic assembly applications. (Connect + Tactile) Overview Installation Development of framework was done

Southwest Research Institute Robotics 29 Dec 01, 2022
[unmaintained] WiFi tools for linux

Note: This project is unmaintained. While I would love to keep up the development on this project, it is difficult for me for several reasons: I don't

Rocky Meza 288 Dec 13, 2022
Implementation of Forwards Kinematics, Inverse Kinematics, Point to Point Movement and Synchronous movement for Kuka KR 120 R2700-2.

I made this project for my university course in robotics. I rarely found any information regarding the implementation of mathematics in code. So I decided to make this repo in order to help others :)

2 Dec 27, 2022
Mycodo is open source software for the Raspberry Pi that couples inputs and outputs in interesting ways to sense and manipulate the environment.

Mycodo Environmental Regulation System Latest version: 8.12.9 Mycodo is open source software for the Raspberry Pi that couples inputs and outputs in i

Kyle Gabriel 2.3k Dec 29, 2022
ok-system-helper是一个简单的系统硬件的实时信息收集工具,使用python3.x开发

ok-system-helper ok-system-helper是一个简单的系统硬件的实时信息收集工具,使用python3.x开发,支持哪些硬件:CPU、内存、SWAP、磁盘、网卡流量。用户可在自己的项目中直接引入、开箱即用,或者结合flask等web框架轻松做成http接口供前端调用,亦可通过注

xlvchao 1 Feb 08, 2022
A python script for macOS to enable scrolling with the 3M ergonomic mouse EM500GPS in any application.

A python script for macOS to enable scrolling with the 3M ergonomic mouse EM500GPS in any application.

3 Feb 19, 2022
从零开始打造一个智能家居系统

SweetHome 从零开始打造智能家居系统的尝试,主要的实现有 可以扫码添加设备并控制设备的Android App 可以控制亮灭的灯,并可以设置在Android App连接到指定Wifi后自动亮起 可以控制开关的窗帘,机械结构部分自己设计并3D打印出来 树莓派主控,实现Http请求接口和ZigBe

金榜 5 May 01, 2022
MicroPython driver for 74HC595 shift registers

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

Mike Causer 17 Nov 29, 2022
A dashboard for Raspberry Pi to display environmental weather data, rain radar, weather forecast, etc. written in Python

Weather Clock for Raspberry PI This project is a dashboard for Raspberry Pi to display environmental weather data, rain radar, weather forecast, etc.

Markus Geiger 1 May 01, 2022
A simple Picobot project implemented in Python

Python-Picobot A simple Picobot project implemented in Python About Explanation This is my first programming project. Picobot use rules.txt file which

Shayan Shiravani 0 Apr 03, 2022
Philippe 1 Jan 09, 2022
Isaac Gym Environments for Legged Robots

Isaac Gym Environments for Legged Robots This repository provides the environment used to train ANYmal (and other robots) to walk on rough terrain usi

Robotic Systems Lab - Legged Robotics at ETH Zürich 372 Jan 08, 2023
This Home Assistant custom component adding support for controlling Midea dehumidifiers on local network.

This custom component for Home Assistant adds support for Midea air conditioner and dehumidifier appliances via the local area network. homeassistant-

Nenad Bogojevic 92 Dec 31, 2022