|
| 1 | +# PCF8574 / PCF8574A GPIO Expander |
| 2 | + |
| 3 | +## TL;DR; |
| 4 | + |
| 5 | +PCF8574 IO expander allows to add up to 32 digital IO pins to Tasmota |
| 6 | + |
| 7 | +!!! Warning |
| 8 | + The driver for PCF8574 is not included in any of Tasmota prebuild binaries. See [compiling for PCF8574](#compiling-for-pcf8574). |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +PCF8574 and PCF8574A are I2C 8-bit IOport extender originally designed by Philips (now NXP) but also |
| 13 | +now available from various manufacturer. |
| 14 | + |
| 15 | +* PCF8574 and PCF8574A at [NXP](https://www.nxp.com/docs/en/data-sheet/PCF8574_PCF8574A.pdf) |
| 16 | +* [PCF8574](https://www.ti.com/lit/ds/symlink/pcf8574.pdf) and [PCF8574A](https://www.ti.com/lit/ds/symlink/pcf8574a.pdf) at Texas Instruments |
| 17 | + |
| 18 | +As usual when using an electronic part, reading the datasheet is highly recommended as the below |
| 19 | +document only focus on Tasmota integration. |
| 20 | + |
| 21 | +A few different breakout boards are available although some are dedicated to be mounted as a backpack on |
| 22 | +standard 16x2 or 16x4 LCD displays and are not suitable for general I/Os (but works well with tasmota-display). |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +* On the left: generic modules suitable for extending IO |
| 27 | +* On the right: specific module to control a LCD display (not the purpose of this doc page) |
| 28 | + |
| 29 | +## Supported I2C addresses and number of PCF8574 |
| 30 | + |
| 31 | +PCF8574 and PCF8574A are identical functionally and each can be configured to work on 1 of 8 possible I2C address. |
| 32 | +PCF8574 can use one address of 0x20 to 0x27 and PCF8574A can use one of 0x38 to 0x3F. |
| 33 | + |
| 34 | +As of today, Tasmota driver supports : |
| 35 | + |
| 36 | +* Up to 4 PCF8574 OR PCF8574A is supported by Tasmota allowing up to 32 additional GPIO pins. |
| 37 | +* Addresses 0x27 and 0x38 are excluded to avoid conflict with other I2C peripheral which can't |
| 38 | + be differentiated at run-time. |
| 39 | +* if USE_MCP230xx_ADDR is defined, this address is reserved for MCP230XX IO expander. |
| 40 | + |
| 41 | +## Enabling the driver |
| 42 | + |
| 43 | +Once the firmware with the PCF8574 driver has been loaded, be sure to have it enabled with `I2Cdriver22 1`. |
| 44 | + |
| 45 | +## Compiling for PCF8574 |
| 46 | + |
| 47 | +The PCF8574 driver is not included by default in any Tasmota [build](https://github.com/arendst/Tasmota/blob/development/BUILDS.md). |
| 48 | +In order to use it you must [compile](https://tasmota.github.io/docs/Compile-your-build/) your own tasmota build |
| 49 | +by adding the following lines to your `user_config_override.h`: |
| 50 | +``` c++ |
| 51 | +#define USE_I2C // Add support for I2C |
| 52 | +#define USE_PCF8574 // [I2cDriver2] Enable PCF8574 I/O Expander (I2C addresses 0x20 - 0x26 and 0x39 - 0x3F) (+1k9 code) |
| 53 | +// #define USE_PCF8574_SENSOR // enable PCF8574 inputs and outputs in SENSOR message |
| 54 | +// #define USE_PCF8574_DISPLAYINPUT // enable PCF8574 inputs display in Web page |
| 55 | +// #define USE_PCF8574_MQTTINPUT // enable MQTT message & rule process on input change detection : stat/%topic%/PCF8574_INP = {"Time":"2021-03-07T16:19:23+01:00","PCF8574-1_INP":{"D1":1}} |
| 56 | +``` |
| 57 | + |
| 58 | +* The first 2 lines are mandatory to enable I2C support and including the driver in the build. |
| 59 | +* By default only "outputs" feature are enabled |
| 60 | +* The 3 other lines allows to add optional features to support inputs |
| 61 | + |
| 62 | +## PCF8574 configuration |
| 63 | + |
| 64 | +PCF8574 can be configured from Tasmota web GUI in "Configure" => "Configure PCF8574" |
| 65 | + |
| 66 | + |
| 67 | +* Each IO port can be configured as Input or Output in a similar way as an native GPIO of the ESP. |
| 68 | +* If you are using outputs to drive relays, it is possible to choose if the relay is activated by a |
| 69 | + HIGH signal (checkbox "Invert Ports" unchecked) or a LOW signal (checkbox checked). The selection |
| 70 | + applies to all output ports. This checkbox can also be controlled by [SetOption81](Commands#setoption81). |
| 71 | + |
| 72 | +Once configuration is complete. It must be saved by clicking on the green "Save" button. Like for |
| 73 | +general ESP GPIO configuration, this will trigger a reboot of the ESP. |
| 74 | + |
| 75 | +!!! note |
| 76 | + It is not possible to change pin definition at run-time. |
| 77 | + |
| 78 | +## PCF8574 Ouputs |
| 79 | + |
| 80 | +A PCF8574 pin configured as an output support all features of a Tasmora `Relay` component. |
| 81 | + |
| 82 | +* It is assigned a Power index and can be controlled by [Power](Commands#power) command (on, off, toggle). |
| 83 | + Power indexes of PCF8574 outputs are assigned after the ESP GPIO configured as `Relay`. For example, |
| 84 | + if you have Relay 1 (Power1) to Relay 4 (Power4) configured on the ESP's GPIO, PCF8574 outputs will start at Power5. |
| 85 | +* A state text and an on/off button is automatically created on the Web GUI and syncs with the pin state. |
| 86 | +* All `Power` features are supported including `PowerOnState`, `PulseTime`, `Blink`, `SetOption0`, ... |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +## PCF8574 Inputs |
| 91 | + |
| 92 | +In order to use PCF8574 inputs, you must add at least one of the following build-time option in |
| 93 | +your `user_config_override.h`: |
| 94 | + |
| 95 | +``` c++ |
| 96 | +#define USE_PCF8574_SENSOR // enable PCF8574 inputs and outputs in SENSOR message |
| 97 | +#define USE_PCF8574_DISPLAYINPUT // enable PCF8574 inputs display in Web page |
| 98 | +#define USE_PCF8574_MQTTINPUT // enable MQTT message & rule process on input change detection : stat/%topic%/PCF8574_INP = {"Time":"2021-03-07T16:19:23+01:00","PCF8574-1_INP":{"D1":1}} |
| 99 | +``` |
| 100 | + |
| 101 | +### PCF8574 in SENSOR message |
| 102 | + |
| 103 | +Enabling `USE_PCF8574_SENSOR` adds a `PCF8574-xx` field into the JSON payload of the `tele/topic/SENSOR` |
| 104 | +message. The form of the message is: |
| 105 | +``` json |
| 106 | +{"Time":"2021-03-11T19:50:58+01:00","PCF8574-1":{"D0":1,"D1":1,"D2":1,"D3":1,"D4":0,"D5":0,"D6":0,"D7":0}} |
| 107 | +``` |
| 108 | + |
| 109 | +As you can see, all pins are listed, including both inputs and outputs. The value reported is the |
| 110 | +digital level of the pin. If "Invert Ports" has been enabled, `Power ON` will be reported as 0 as |
| 111 | +the pin is at a LOW level. |
| 112 | + |
| 113 | +As for any sensor published in the `tele/topic/SENSOR` message, it is possible to use Rules triggers such as: |
| 114 | +``` |
| 115 | +ON tele-PCF8574-1#D0 DO something_with %value% ENDON |
| 116 | +``` |
| 117 | +Numerical operators such as `==` can be used to compare to 0 or 1. See also [change detection](#input-change-detection). |
| 118 | + |
| 119 | +### PCF8574 inputs pins in the Web GUI |
| 120 | + |
| 121 | +Enabling `USE_PCF8574_DISPLAYINPUT` will add the state of PCF8574 inpupts displayed as sensors |
| 122 | +in the Web GUI. Outputs are not represented here as they are already shown as Power. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +Value of pin is updated in almost "real-time". |
| 127 | + |
| 128 | +### Input change detection |
| 129 | + |
| 130 | +While reporting the pin level in SENSOR or on the GUI is interresting, it is more interresting to |
| 131 | +detect pin change. This is enabled by `USE_PCF8574_MQTTINPUT`. When this feature is enabled at |
| 132 | +build time, a test will be performed every 50ms to detect if an input pin has changed. In that case, |
| 133 | +Tasmota will publish on `stat/topic/PCF8574_INP` a JSON payload with the PCF8574 index |
| 134 | +and the pin level: |
| 135 | +``` json |
| 136 | +20:19:39.385 MQT: stat/topic/PCF8574_INP = {"Time":"2021-03-11T20:19:39+01:00","PCF8574-1_INP":{"D0":0}} |
| 137 | +20:19:39.584 MQT: stat/topic/PCF8574_INP = {"Time":"2021-03-11T20:19:39+01:00","PCF8574-1_INP":{"D0":1}} |
| 138 | +``` |
| 139 | + |
| 140 | +This can be catched in rules such as: |
| 141 | + |
| 142 | +* Implementing a Power push "Button": |
| 143 | +``` haskel |
| 144 | +ON PCF8574-1_INP#D0=0 DO Power2 toggle ENDON |
| 145 | +``` |
0 commit comments