import pcf8574
The pcf8574 module controls PCF8574 and PCF8574A-compatible 8-bit GPIO expanders over I2C. The chip uses quasi-bidirectional pins, so there is no direction register: writing 0 drives a pin low, and writing 1 releases it high so it can be read as an input or used as a weak high output.
Quick example
import pcf8574
expander = pcf8574.PCF8574(sda=21, scl=22, address=0x20)
expander.low(0)
expander.high(1)
expander.input(2)
if expander.read_pin(2):
print("pin is high")
print(expander.state())
importpcf8574
PCF8574 I2C GPIO expander module.
The pcf8574 module controls PCF8574 and PCF8574A-compatible 8-bit GPIO expanders over I2C. The chip uses quasi-bidirectional pins, so there is no direction register: writing 0 drives a pin low, and writing 1 releases it high so it can be read as an input or used as a weak high output.
pcf8574.PCF8574(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, initial: int = None) -> pcf8574_expander
pcf8574.PCF8574(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, initial: int = None) -> pcf8574_expanderCreate and initialize an independent PCF8574 GPIO expander.
Use one PCF8574 object for each physical expander. Multiple expanders can share one I2C bus when each chip has a unique address.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
sda |
int |
positional or keyword | Yes | GPIO pin used for I2C SDA. |
scl |
int |
positional or keyword | Yes | GPIO pin used for I2C SCL. |
address |
int |
positional or keyword | No | Optional I2C address. PCF8574 commonly uses 0x20 through 0x27. PCF8574A commonly uses 0x38 through 0x3f. Defaults to 0x20. |
frequency |
int |
positional or keyword | No | Optional I2C bus speed in hertz. Defaults to 100000. |
port |
int |
positional or keyword | No | Optional ESP-IDF I2C port number. Defaults to 0. |
initial |
int |
positional or keyword | No | Optional 8-bit latch value written during initialization. Defaults to 0xff, which releases all pins high. |
pcf8574_expander pcf8574_expander object.
pcf8574.PCF8574().write(value) -> bool
pcf8574.PCF8574().write(value) -> boolWrite this expander's full 8-bit output latch.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
any |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().read() -> int
pcf8574.PCF8574().read() -> intRead this expander's current 8-bit pin state.
pcf8574.PCF8574().state() -> dict
pcf8574.PCF8574().state() -> dictRead this expander's pins and latch as a dict.
pcf8574.PCF8574().write_pin(pin, value) -> bool
pcf8574.PCF8574().write_pin(pin, value) -> boolWrite one pin by updating this expander's latch.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required value. |
value |
any |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().read_pin(pin) -> bool
pcf8574.PCF8574().read_pin(pin) -> boolRead one pin from this expander.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().high(pin) -> bool
pcf8574.PCF8574().high(pin) -> boolRelease one pin high.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().low(pin) -> bool
pcf8574.PCF8574().low(pin) -> boolDrive one pin low.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().input(pin) -> bool
pcf8574.PCF8574().input(pin) -> boolRelease one pin so it can be used as an input.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().toggle(pin) -> bool
pcf8574.PCF8574().toggle(pin) -> boolToggle one latch bit.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().set_mask(mask) -> bool
pcf8574.PCF8574().set_mask(mask) -> boolSet latch bits using a mask.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
mask |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().clear_mask(mask) -> bool
pcf8574.PCF8574().clear_mask(mask) -> boolClear latch bits using a mask.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
mask |
int |
positional or keyword | Yes | Required value. |
pcf8574.PCF8574().reset() -> bool
pcf8574.PCF8574().reset() -> boolRelease all pins high.
pcf8574.PCF8574().info() -> dict
pcf8574.PCF8574().info() -> dictReturn I2C settings and current latch state for this expander.
pcf8574.PCF8574().close() -> bool
pcf8574.PCF8574().close() -> boolRelease this PCF8574 handle.
pcf8574.PCF8574().Example
pcf8574.PCF8574().Exampleexpander = pcf8574.PCF8574(sda=21, scl=22, address=0x20) expander.low(0) print(expander.state())
pcf8574.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, initial: int = None) -> bool
pcf8574.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, initial: int = None) -> boolLegacy: initialize the module-level default PCF8574 expander.
Prefer ``expander = pcf8574.PCF8574(...)`` for new projects. The legacy wrapper keeps older singleton-style scripts working.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
sda |
int |
positional or keyword | Yes | GPIO pin used for I2C SDA. |
scl |
int |
positional or keyword | Yes | GPIO pin used for I2C SCL. |
address |
int |
positional or keyword | No | Optional I2C address. PCF8574 commonly uses 0x20 through 0x27. PCF8574A commonly uses 0x38 through 0x3f. Defaults to 0x20. |
frequency |
int |
positional or keyword | No | Optional I2C bus speed in hertz. Defaults to 100000. |
port |
int |
positional or keyword | No | Optional ESP-IDF I2C port number. Defaults to 0. |
initial |
int |
positional or keyword | No | Optional 8-bit byte value written to the output latch during initialization. Defaults to 0xff, which releases all pins high. |
bool True when the I2C bus is ready and the initial latch value is accepted.
pcf8574.write(value: int) -> bool
pcf8574.write(value: int) -> boolWrite the full 8-bit output latch.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
int |
positional or keyword | Yes | 8-bit byte value from 0 through 255. A 0 bit drives the matching pin low. A 1 bit releases it high. |
bool True when the expander acknowledges the write.
pcf8574.read() -> int
pcf8574.read() -> intRead the current 8-bit pin state.
int Integer pin state from 0 through 255.
pcf8574.state() -> dict
pcf8574.state() -> dictRead the current pin state as a dict.
dict Dict with ``ok``, ``value``, ``latch``, ``address``, and boolean ``p0`` through ``p7`` fields.
pcf8574.write_pin(pin: int, value: bool) -> bool
pcf8574.write_pin(pin: int, value: bool) -> boolWrite one pin by updating the module's output latch.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Pin number from 0 through 7. |
value |
bool |
positional or keyword | Yes | Boolean pin value. False drives the pin low. True releases it high. |
bool True when the updated latch is written.
pcf8574.read_pin(pin: int) -> bool
pcf8574.read_pin(pin: int) -> boolRead one pin.
For a pin used as input, call ``input(pin)`` first so the pin is released high before reading the external state.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Pin number from 0 through 7. |
bool Boolean pin state.
pcf8574.high(pin: int) -> bool
pcf8574.high(pin: int) -> boolRelease one pin high.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Pin number from 0 through 7. |
bool True when the updated latch is written.
pcf8574.low(pin: int) -> bool
pcf8574.low(pin: int) -> boolDrive one pin low.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Pin number from 0 through 7. |
bool True when the updated latch is written.
pcf8574.input(pin: int) -> bool
pcf8574.input(pin: int) -> boolRelease one pin so it can be used as an input.
The PCF8574 has quasi-bidirectional pins, not direction registers. This method writes a 1 to the selected latch bit.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Pin number from 0 through 7. |
bool True when the pin is released high.
pcf8574.toggle(pin: int) -> bool
pcf8574.toggle(pin: int) -> boolToggle one latch bit.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | Pin number from 0 through 7. |
bool True when the updated latch is written.
pcf8574.set_mask(mask: int) -> bool
pcf8574.set_mask(mask: int) -> boolSet latch bits using a mask.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
mask |
int |
positional or keyword | Yes | 8-bit byte value. Bits set to 1 in the mask are set high in the latch. |
bool True when the updated latch is written.
pcf8574.clear_mask(mask: int) -> bool
pcf8574.clear_mask(mask: int) -> boolClear latch bits using a mask.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
mask |
int |
positional or keyword | Yes | 8-bit byte value. Bits set to 1 in the mask are driven low in the latch. |
bool True when the updated latch is written.
pcf8574.reset() -> bool
pcf8574.reset() -> boolRelease all pins high.
bool True when 0xff is written to the latch.
pcf8574.info() -> dict
pcf8574.info() -> dictReturn PCF8574 module state.
dict Dict with initialization state, I2C pins, address, frequency, port, and current output latch value.