MojoScale Studio Docs
API Reference

pcf8574

PCF8574 I2C GPIO expander module.

Studio Docs Sensors & I/O

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.

API 30 available

pcf8574.PCF8574(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, initial: int = None) -> pcf8574_expander

Create 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.

Parameters
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.
Returns

pcf8574_expander pcf8574_expander object.

pcf8574.PCF8574().write(value) -> bool

Write this expander's full 8-bit output latch.

Parameters
Name Type Pass as Required Description
value any positional or keyword Yes Required value.

pcf8574.PCF8574().read() -> int

Read this expander's current 8-bit pin state.

pcf8574.PCF8574().state() -> dict

Read this expander's pins and latch as a dict.

pcf8574.PCF8574().write_pin(pin, value) -> bool

Write one pin by updating this expander's latch.

Parameters
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

Read one pin from this expander.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Required value.

pcf8574.PCF8574().high(pin) -> bool

Release one pin high.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Required value.

pcf8574.PCF8574().low(pin) -> bool

Drive one pin low.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Required value.

pcf8574.PCF8574().input(pin) -> bool

Release one pin so it can be used as an input.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Required value.

pcf8574.PCF8574().toggle(pin) -> bool

Toggle one latch bit.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Required value.

pcf8574.PCF8574().set_mask(mask) -> bool

Set latch bits using a mask.

Parameters
Name Type Pass as Required Description
mask int positional or keyword Yes Required value.

pcf8574.PCF8574().clear_mask(mask) -> bool

Clear latch bits using a mask.

Parameters
Name Type Pass as Required Description
mask int positional or keyword Yes Required value.

pcf8574.PCF8574().reset() -> bool

Release all pins high.

pcf8574.PCF8574().info() -> dict

Return I2C settings and current latch state for this expander.

pcf8574.PCF8574().close() -> bool

Release this PCF8574 handle.

pcf8574.PCF8574().Example

expander = 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

Legacy: initialize the module-level default PCF8574 expander.

Prefer ``expander = pcf8574.PCF8574(...)`` for new projects. The legacy wrapper keeps older singleton-style scripts working.

Parameters
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.
Returns

bool True when the I2C bus is ready and the initial latch value is accepted.

pcf8574.write(value: int) -> bool

Write the full 8-bit output latch.

Parameters
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.
Returns

bool True when the expander acknowledges the write.

pcf8574.read() -> int

Read the current 8-bit pin state.

Returns

int Integer pin state from 0 through 255.

pcf8574.state() -> dict

Read the current pin state as a dict.

Returns

dict Dict with ``ok``, ``value``, ``latch``, ``address``, and boolean ``p0`` through ``p7`` fields.

pcf8574.write_pin(pin: int, value: bool) -> bool

Write one pin by updating the module's output latch.

Parameters
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.
Returns

bool True when the updated latch is written.

pcf8574.read_pin(pin: int) -> bool

Read one pin.

For a pin used as input, call ``input(pin)`` first so the pin is released high before reading the external state.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Pin number from 0 through 7.
Returns

bool Boolean pin state.

pcf8574.high(pin: int) -> bool

Release one pin high.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Pin number from 0 through 7.
Returns

bool True when the updated latch is written.

pcf8574.low(pin: int) -> bool

Drive one pin low.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Pin number from 0 through 7.
Returns

bool True when the updated latch is written.

pcf8574.input(pin: int) -> bool

Release 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.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Pin number from 0 through 7.
Returns

bool True when the pin is released high.

pcf8574.toggle(pin: int) -> bool

Toggle one latch bit.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes Pin number from 0 through 7.
Returns

bool True when the updated latch is written.

pcf8574.set_mask(mask: int) -> bool

Set latch bits using a mask.

Parameters
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.
Returns

bool True when the updated latch is written.

pcf8574.clear_mask(mask: int) -> bool

Clear latch bits using a mask.

Parameters
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.
Returns

bool True when the updated latch is written.

pcf8574.reset() -> bool

Release all pins high.

Returns

bool True when 0xff is written to the latch.

pcf8574.info() -> dict

Return PCF8574 module state.

Returns

dict Dict with initialization state, I2C pins, address, frequency, port, and current output latch value.