Skip to main content
Support forum pcf8574 English
Forum supporto pcf8574 italiano

www.mischianti.org

PCF8574 PCF8574AP digital input and output expander with i2c bus.

Changelog

  • 18/04/2023: v0.0.2 Add static declaration for Px constants inside class.
  • 14/04/2023: v0.0.1 Initial commit of stable version.

I try to simplify the use of this IC, with a minimal set of operations.

Tested with esp8266, esp32, Arduino, Arduino SAMD (Nano 33 IoT, MKR etc.), STM32 and rp2040 (Raspberry Pi Pico and similar)

PCF8574P address map 0x20-0x27 PCF8574AP address map 0x38-0x3f

Installation

To install the library execute the following command:

pip install pcf8574-library

Constructor: Pass the address of I2C

    from PCF8574 import PCF8574
    
    pcf = PCF8574(0x38, sda=21, scl=22)

To use interrupt you must pass the interrupt pin and the function to call when interrupt raised from PCF8574

    from PCF8574 import PCF8574
    
    def keyPressedOnPCF8574(pin):
        # Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
        keyPressed = True
    
    pcf = PCF8574(0x38, sda=21, scl=22, interrupt_callback=keyPressedOnPCF8574, interrupt_pin=18)

You must set input/output mode:

    from machine import Pin
    from PCF8574 import PCF8574

    pcf.Pin(PCF8574.P0, Pin.IN)
    pcf.Pin(PCF8574.P1, Pin.IN, Pin.PULL_UP)
    pcf.Pin(PCF8574.P2, Pin.IN)
    pcf.Pin(PCF8574.P3, Pin.IN)
    
    pcf.Pin(PCF8574.P7, Pin.OUT)
    pcf.Pin(PCF8574.P6, Pin.OUT, 1)
    pcf.Pin(PCF8574.P5, Pin.OUT, 0)
    pcf.Pin(PCF8574.P4, Pin.OUT, 0)

then IC as you can see in the image has 8 digital input/output ports:

PCF8574 schema

To read all analog input in one trasmission you can do (even if I use a 10millis debounce time to prevent too much read from i2c):

    digital_input = pcf.digital_read_all()
    
    print(digital_input.p0)
    print(digital_input.p1)
    print(digital_input.p2)
    print(digital_input.p3)
    print(digital_input.p4)
    print(digital_input.p5)
    print(digital_input.p6)
    print(digital_input.p7)
    
    array_input = pcf.digital_read_all_array()
    print(array_input)
    
    byte_input = pcf.digital_read_all_byte()
    print(bin(byte_input))

If you want to read a single input:

    digital_input = pcf.digital_read(PCF8574.P1)
    print(digital_input)

If you want to write a digital value:

    pcf.digital_write(PCF8574.P1, 1)

You can also use an interrupt pin: You must initialize the pin and the function to call when interrupt raised from PCF8574

    def callback(pin):
        now = utime.ticks_ms()
        global count
        count += 1
        print("Time: {} {}".format(now, count))
    
    
    pcf.attach_interrupt(18, callback)

For the examples I use this wire schema on breadboard: Breadboard Breadboard Breadboard

Release files for pcf8574-library 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pcf8574-library 0.0.2
File Size Uploaded
pcf8574-library-0.0.2.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pcf8574-library 0.0.2
File Interpreter ABI Platform
pcf8574_library-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / pcf8574-library-0.0.2.tar.gz

Download URL pcf8574-library-0.0.2.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e2604a1f01de37bfdb7f3d16c9a23901acb1fea4a91e712e7ea8010104eab8df
BLAKE2b-256 checksum
How to use checksums
8166a3a38c84a032d722406d7b4a7e23ac88214e63a56399291804e7be00f674
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / pcf8574_library-0.0.2-py3-none-any.whl

Download URL pcf8574_library-0.0.2-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6eb6f1f61b15430b4383daf40c4222f26ff743b73431dd5b9bebf4a3788e23e
BLAKE2b-256 checksum
How to use checksums
a5234d45306a238bbc7dd3cbabd625a1eb7f67b30486910a41153915b8fcf614
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page