Most starred PCF8574 library. i2c digital expander for Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266. Can read write digital values with only 2 wire. Very simple to use
Project description
www.mischianti.org
PCF8574 PCF8574AP digital input and output expander with i2c bus.
Changelog
- 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, P0, P7, P6, P1, P2, P3, P5, P4
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, P0, P7, P6, P1, P2, P3, P5, P4
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, P0, P7, P6, P1, P2, P3, P5, P4
pcf.Pin(P0, Pin.IN)
pcf.Pin(P1, Pin.IN, Pin.PULL_UP)
pcf.Pin(P2, Pin.IN)
pcf.Pin(P3, Pin.IN)
pcf.Pin(P7, Pin.OUT)
pcf.Pin(P6, Pin.OUT, 1)
pcf.Pin(P5, Pin.OUT, 0)
pcf.Pin(P4, Pin.OUT, 0)
then IC as you can see in the image has 8 digital input/output ports:
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(P1)
print(digital_input)
If you want to write a digital value:
pcf.digital_write(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:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pcf8574_library-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96dfa49bb059fafaa1a0de918eed7b746ad8d0a1e31b086731d0b3eae6a21f52 |
|
MD5 | a830528825562924be009d831ec003c0 |
|
BLAKE2b-256 | 602e5b9e8c646fb76350c562edbe21ceef3969ea235b6f4a279517564e62915d |