It's easy to use the Adafruit PCF8574 with Python or CircuitPython, and the Adafruit CircuitPython PCF8574 module. This module allows you to easily write Python code that enables you to utilise the 8 I/O pins on the expander.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a PCF8574 to your board exactly as shown below. These wiring diagrams include a button and an LED, which are necessary for the example below.

Here's an example of wiring a Feather RP2040 to the expander with I2C using one of the handy STEMMA QT connectors:

Feather to expander:

  • Simply use a STEMMA QT cable to connect from the STEMMA QT connector on the microcontroller to the STEMMA QT connector on the breakout.

Follow the steps below to connect the LED and button.

You can also use the standard 0.100" pitch headers to wire it up on a breadboard:

Feather to expander:

  • Feather 3V to expander VIN (red wire)
  • Feather GND to expander GND (black wire)
  • Feather SCL to expander SCL (yellow wire)
  • Feather SDA to expander SDA (blue wire)

Follow the steps below to connect the LED and button.

Connect the LED and the button to the expander as follows:

LED to expander:

  • LED- to expander P7
  • LED+ to 470Ω resistor
  • 470Ω resistor to + on top edge of expander

Button to expander:

  • One leg of button to - on top edge of expander
  • Opposite leg of button to expander P0

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

These wiring diagrams include a button and an LED, which are necessary for the example below.

Here's the Raspberry Pi wired to the expander using I2C and a STEMMA QT connector.

Pi to expander:

  • Pi 3V to expander VIN (red wire)
  • Pi GND to expander GND (black wire)
  • Pi SCL to expander SCL (yellow wire)
  • Pi SDA to expander SDA (blue wire)

Follow the steps above to connect the LED and button.

Finally here is an example of how to wire up a Raspberry Pi to the expander using a solderless breadboard:

Pi to expander:

  • Pi 3V to expander VIN (red wire)
  • Pi GND to expander GND (black wire)
  • Pi SCL to expander SCL (yellow wire)
  • Pi SDA to expander SDA (blue wire)

Follow the steps above to connect the LED and button.

Python Installation of PCF8574 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • pip3 install adafruit-circuitpython-pcf8574

If your default Python is version 3, you may need to run pip instead. Make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython Usage

To use with CircuitPython, you need to first install the ADXL37x library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

Your CIRCUITPY/lib folder should contain the following folder and file:

  • adafruit_bus_device/
  • adafruit_pcf8574.mpy
CIRCUITPY

Python Usage

Once you have the library pip3 installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:

python3 code.py

Example Code

# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

import time
import board
import digitalio
import adafruit_pcf8574

print("PCF8574 digitalio LED + button test")

i2c = board.I2C()
pcf = adafruit_pcf8574.PCF8574(i2c)

# get a 'digitalio' like pin from the pcf
led = pcf.get_pin(7)
button = pcf.get_pin(0)

# Setup pin7 as an output that's at a high logic level default
led.switch_to_output(value=True)
# Setup pin0 as an output that's got a pullup
button.switch_to_input(pull=digitalio.Pull.UP)


while True:
    led.value = button.value
    time.sleep(0.01)  # debounce

Now, press the button to see the LED light up.

The GIF below shows a "lazy" way to connect the button and LED to the expander. You should always include a resistor when wiring an LED!

This GIF shows it wired to the QT Py, but the button press to turn on the LED is the same.

That's all there is to using the PCF8574 with CircuitPython!

This guide was first published on Aug 03, 2022. It was last updated on 2022-08-03 14:47:34 -0400.

This page (Python & CircuitPython) was last updated on Aug 06, 2022.

Text editor powered by tinymce.