PCF8574 I/O Expander

The PCF8574 component allows you to use PCF8574 and PCF8575 I/O port expanders with many of esphomelib’s components. With most components, you are able to specify GPIOOutputPin or GPIOInputPin for internal pins. The PCF8574 component subclasses those types.

Example Usage

auto *pcf8574 = App.make_pcf8574_component(0x21);
App.make_gpio_binary_sensor("PCF pin 0 sensor", pcf8574->make_input_pin(0, PCF8574_INPUT));
App.make_gpio_binary_sensor("PCF pin 0 sensor", 0);
App.make_gpio_switch("PCF pin 1 switch", pcf8574->make_output_pin(1));
auto *out = App.make_gpio_output(pcf8574->make_output_pin(2));
App.make_binary_light("PCF pin 2 light", out);

See Application::make_pcf8574_component().

API Reference

enum PCF8574GPIOMode

Modes for PCF8574 pins.

Values:

PCF8574_INPUT = INPUT
PCF8574_INPUT_PULLUP = INPUT_PULLUP
PCF8574_OUTPUT = OUTPUT
class PCF8574Component : public Component, public I2CDevice

Public Functions

PCF8574Component(I2CComponent *parent, uint8_t address, bool pcf8575 = false)
PCF8574GPIOInputPin make_input_pin(uint8_t pin, uint8_t mode = PCF8574_INPUT, bool inverted = false)

Make a GPIOPin that can be used in other components.

Note that in some cases this component might not work with incompatible other integrations because for performance reasons the values are only sent once every loop cycle in a batch. For example, OneWire sensors are not supported.

Return
An PCF8574GPIOPin instance.
Parameters
  • pin: The pin number to use. 0-7 for PCF8574, 0-15 for PCF8575.
  • mode: The pin mode to use. Only supported ones are PCF8574_INPUT, PCF8574_INPUT_PULLUP.
  • inverted: If the pin should invert all incoming and outgoing values.

PCF8574GPIOOutputPin make_output_pin(uint8_t pin, bool inverted = false)

Make a GPIOPin that can be used in other components.

Note that in some cases this component might not work with incompatible other integrations because for performance reasons the values are only sent once every loop cycle in a batch. For example, OneWire sensors are not supported.

Return
An PCF8574GPIOPin instance.
Parameters
  • pin: The pin number to use. 0-7 for PCF8574, 0-15 for PCF8575.
  • inverted: If the pin should invert all incoming and outgoing values.

void setup()

Check i2c availability and setup masks.

bool digital_read_(uint8_t pin)

Helper function to read the value of a pin.

void digital_write_(uint8_t pin, bool value)

Helper function to write the value of a pin.

void pin_mode_(uint8_t pin, uint8_t mode)

Helper function to set the pin mode of a pin.

float get_setup_priority() const

priority of setup().

higher -> executed earlier

Defaults to 0.

Return
The setup priority of this component

void dump_config()

Protected Functions

bool read_gpio_()
bool write_gpio_()

Protected Attributes

uint16_t ddr_mask_ = {0x00}
uint16_t input_mask_ = {0x00}
uint16_t port_mask_ = {0x00}
bool pcf8575_

TRUE->16-channel PCF8575, FALSE->8-channel PCF8574.

class PCF8574GPIOInputPin : public GPIOInputPin

Helper class to expose a PCF8574 pin as an internal input GPIO pin.

Public Functions

PCF8574GPIOInputPin(PCF8574Component *parent, uint8_t pin, uint8_t mode, bool inverted = false)
GPIOPin *copy() const
void setup()

Setup the pin mode.

void pin_mode(uint8_t mode)

Set the pin mode.

bool digital_read()

Read the binary value from this pin using digitalRead (and inverts automatically).

void digital_write(bool value)

Write the binary value to this pin using digitalWrite (and inverts automatically).

Protected Attributes

PCF8574Component *parent_
class PCF8574GPIOOutputPin : public GPIOOutputPin

Helper class to expose a PCF8574 pin as an internal output GPIO pin.

Public Functions

PCF8574GPIOOutputPin(PCF8574Component *parent, uint8_t pin, uint8_t mode, bool inverted = false)
GPIOPin *copy() const
void setup()

Setup the pin mode.

void pin_mode(uint8_t mode)

Set the pin mode.

bool digital_read()

Read the binary value from this pin using digitalRead (and inverts automatically).

void digital_write(bool value)

Write the binary value to this pin using digitalWrite (and inverts automatically).

Protected Attributes

PCF8574Component *parent_