Using the 74HC595 with CircuitPython is simple with the Adafruit CircuitPython 74HC595 library. You can easily write Python code to control multiple output pins from a single SPI connection. This example will control 8 LEDs from the SPI connection on a Feather M4.
CircuitPython Wiring
First, wire up the 74HC595 to your CircuitPython board. The following shows how to wire it up to a Feather M4.
- Feather 3V to power rail on breadboard
- Feather GND to ground rail on breadboard
- Ground rail on breadboard to ground rail on breadboard
- 74HC595 Pin 16 to power rail on breadboard
- 74HC595 Pin 10 to power rail on breadboard
- 74HC595 Pin 13 to ground rail on breadboard
- 74HC595 Pin 8 to ground rail on breadboard
- 74HC595 Pin 14 to Feather MO
- 74HC595 Pin 12 to Feather D5
- 74HC595 Pin 11 to Feather SCK
- 8 x LED - (negative) to ground rail on breadboard
-
8 x LED + (positive) to breadboard (470Ω resistors)
- 8 x 470Ω resistor FROM LED + (positive) TO 74HC595 output pins
- 74HC595 Pins 1-7, 15 to 8 x 470Ω resistors
CircuitPython Library Install
You'll need to install the Adafruit CircuitPython 74HC595 library on your CircuitPython board.
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle. Our CircuitPython starter guide has a great page on how to install the library bundle.
Copy the following libraries from the bundle into your lib folder on the CIRCUITPY drive which appears when you connect your board into your computer via a USB cable:
- adafruit_74hc595.mpy
- adafruit_bus_device
Before continuing make sure your board's lib folder or root filesystem has the adafruit_74hc595.mpy, and adafruit_bus_device file and folder copied over.
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
CircuitPython Usage
This example will display a chase pattern followed by blinking on 8 LEDs wired up to the 74HC595.
First we import the necessary libraries, setup the latch pin, and create the shift register object.
import time import board import digitalio import adafruit_74hc595 latch_pin = digitalio.DigitalInOut(board.D5) sr = adafruit_74hc595.ShiftRegister74HC595(board.SPI(), latch_pin)
Using the Adafruit CircuitPython 74HC595 library, you access the pins on the 74HC595 using get_pin(pin_number)
. For example, in this case, to access pin 0, it would be sr.get_pin(0)
. Since the pin objects need to be in a list to display the chase animation, we'll use a list comprehension to create the eight pin objects for the eight output pins.
pins = [sr.get_pin(n) for n in range(8)]
The above code results in the same thing as setting up each pin individually:
pins = [sr.get_pin(0), sr.get_pin(1), sr.get_pin(2), sr.get_pin(3), sr.get_pin(4), sr.get_pin(5), sr.get_pin(6), sr.get_pin(7)]
Inside the loop, we begin by running the first code block two times, to run the chase animation twice. It cycles through turning on each LED in sequence.
while True: for _ in range(2): for enabled_pin in range(len(pins)): for pin_number, pin in enumerate(pins): if pin_number == enabled_pin: pin.value = True else: pin.value = False time.sleep(0.01)
Finally, we blink the LEDs. We turn on the LEDs by setting all the pin values to True
, wait 0.5 seconds, and then turn off the LEDs by setting all the LED values to False
. This happens three times.
[...] for _ in range(3): for pin in pins: pin.value = True time.sleep(0.5) for pin in pins: pin.value = False time.sleep(0.5)
That's all there is to controlling eight LEDs using the 74HC595 shift register!
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import digitalio import adafruit_74hc595 latch_pin = digitalio.DigitalInOut(board.D5) sr = adafruit_74hc595.ShiftRegister74HC595(board.SPI(), latch_pin) # Create the pin objects in a list pins = [sr.get_pin(n) for n in range(8)] while True: for _ in range(2): # Run the chase animation twice for enabled_pin in range(len(pins)): for pin_number, pin in enumerate(pins): if pin_number == enabled_pin: pin.value = True else: pin.value = False time.sleep(0.01) for _ in range(3): # Run the blink animation three times for pin in pins: pin.value = True time.sleep(0.5) for pin in pins: pin.value = False time.sleep(0.5)
Text editor powered by tinymce.