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
Provide Unifi device info via api to Home Assistant that will give ap sensors

Unifi AP Device info Provide Unifi device info via api to Home Assistant that will give ap sensors

12 Jan 07, 2023
Open source home automation that puts local control and privacy first.

Home Assistant Open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiast

Home Assistant 57k Jan 01, 2023
Control DJI Tello with Raspberry Pi and PS4 Controller

Control-DJI-Tello-with-Raspberry-Pi-and-PS4-Controller Demo of this project see

MohammadReza Sharifi 24 Aug 11, 2022
a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico

pico_ws2812b a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico You'll first need to save the ws2812b.py file to your device (for

76 Nov 25, 2022
DongshanPI Seven for STM32MP157DAC.

STM32MP1 Buildroot External Tree

DongshanPI 14 May 06, 2022
A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.

PyUserInput PyUserInput is a group project so we've moved the project over to a group organization: https://github.com/PyUserInput/PyUserInput . That

Paul Barton 1k Dec 27, 2022
🐱 Petkit feeder components for HomeAssistant

Petkit for HomeAssistant Installing Download and copy custom_components/xiaomi_miot folder to custom_components folder in your HomeAssistant config fo

62 Dec 29, 2022
The goal of this project is for anyone with an old printer to be able to double-sided printing.

Welcome to PDF-double-side! Hi! I'm 15. I have a old printer so I can't print double-sided outs. The goal of this project is for anyone with an old pr

DejaVu 4 Dec 28, 2021
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

747Developments 1 Dec 17, 2021
Get the AltAz coordinates for a given object using astropy and output on a OLED screen.

Star Coordinates Get the AltAz coordinates for a given object using astropy and output on a OLED screen. As a very very newcomer to the astronomy scen

Craig Cmehil 1 Jan 31, 2022
Homeautomation system created with Raspberry Pi 3 and Firebase.

Homeautomation System - Raspberry Pi 3 Desenvolvido com Python, Flask com AJAX e Firebase permite o controle local e remoto Itens necessários Raspberr

Joselino Santos 0 Mar 09, 2022
Automatic Watering System using Soil Moisture Sensor and RTC Timer with Arduino

Automatic-Watering-System - Technical Answers to Real-World Problems. Evolution of Watering Manually to Watering Automatically.

Vaishnavi Pothugunta 4 Dec 31, 2021
Modi2-firmware-updater - MODI+ Firmware Updater With Python

MODI+ Firmware Updater 실행 준비 python3(파이썬3.9 혹은 그 이상의 버전)를 컴퓨터에 설치 python3 -m pip

LUXROBO 1 Feb 04, 2022
Point Density-Aware Voxels for LiDAR 3D Object Detection (CVPR 2022)

PDV PDV is LiDAR 3D object detection method. This repository is based off [OpenPCDet]. Point Density-Aware Voxels for LiDAR 3D Object Detection Jordan

Toronto Robotics and AI Laboratory 114 Dec 21, 2022
Add filters (background blur, etc) to your webcam on Linux.

webcam-filters Add filters (background blur, etc) to your webcam on Linux. Video conferencing applications tend to either lack video effects altogethe

Jashandeep Sohi 480 Dec 14, 2022
Christmasvillage-rpi - Raspberry Pi relay controller for ChristmasVillage.io

ChristmasVillage.io Relay Controller Links ChristmasVillage.io - Live Stream & Controls Youtube Instagram About This repository controls the light rel

Grant Windes 2 Feb 15, 2022
A 3rd party Moonraker component to create timelapse of 3D prints.

A 3rd party Moonraker component to create timelapse of 3D prints.

Mainsail-Crew 166 Dec 26, 2022
The robot is an autonomous small scale racing car using NVIDIA Jetson Nano.

The robot is an autonomous small scale racing car using NVIDIA Jetson Nano. This project utilizes deep learning neural network framework Keras/Tensorflow, together with computer vision library OpenCV

1 Dec 08, 2021
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
Hook and simulate global keyboard events on Windows and Linux.

keyboard Take full control of your keyboard with this small Python library. Hook global events, register hotkeys, simulate key presses and much more.

BoppreH 3.2k Dec 30, 2022