2021-01-29 12:31:58 +01:00
[![Arduino CI ](https://github.com/RobTillaart/PCF8574/workflows/Arduino%20CI/badge.svg )](https://github.com/marketplace/actions/arduino_ci)
[![License: MIT ](https://img.shields.io/badge/license-MIT-green.svg )](https://github.com/RobTillaart/PCF8574/blob/master/LICENSE)
[![GitHub release ](https://img.shields.io/github/release/RobTillaart/PCF8574.svg?maxAge=3600 )](https://github.com/RobTillaart/PCF8574/releases)
2020-05-23 13:33:43 +02:00
# PCF8574
2021-01-29 12:31:58 +01:00
Arduino library for PCF8574 - 8 channel I2C IO expander
2021-07-05 10:01:35 +02:00
2021-01-29 12:31:58 +01:00
## Description
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
Related to the PCF8575 16 channel IO expander library https://github.com/RobTillaart/PCF8575
2020-05-23 13:33:43 +02:00
This library gives easy control over the 8 pins of a PCF8574 and PCF8574A chip.
These chips are identical in behavior although there are two distinct address ranges.
2021-07-05 10:01:35 +02:00
| TYPE | ADDRESS-RANGE | notes |
|:---------|:-------------:|:------------------------:|
|PCF8574 | 0x20 to 0x27 | same range as PCF8575 !! |
|PCF8574A | 0x38 to 0x3F | |
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
So you can connect up to 16 PCF8574 on one I2C bus, giving access
to 16 x 8 = 128 IO lines. To maximize IO lines combine 8 x PCF8575 + 8 x PCF8574A giving
128 + 64 = 192 IO lines. Be sure to have a well dimensioned power supply.
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
The library allows to read and write both single pins or 8 pins at once.
Furthermore some additional functions are implemented that are
2021-07-05 10:01:35 +02:00
playful but useful.
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
## Interface
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
**PCF8574_INITIAL_VALUE** is a define that can be set compile time or before
the include of "pcf8574.h" to overrule the default value used with the
**begin()** call.
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
2021-01-29 12:31:58 +01:00
### Constructor
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
- **PCF8574(deviceAddress, TwoWire \*wire = & Wire)** Constructor with device address,
and optional the Wire interface as parameter.
2021-07-05 10:01:35 +02:00
- **bool begin(val = PCF8574_INITIAL_VALUE)** set the initial value for the pins and masks.
- **bool begin(sda, scl, val = PCF8574_INITIAL_VALUE)** idem, for the ESP32 where one can choose the I2C pins.
- **bool isConnected()** checks if the address set in the constructor or by **setAddress()** is visible on the I2C bus.
- **bool setAddress(const uint8_t deviceAddress)** sets the device address after construction. Can be used to switch between PCF8574 modules runtime. Note this corrupts internal buffered values, so one might need to call **read8()** and/or **write8()** . Returns true if address can be found on I2C bus.
- **uint8_t getAddress()** returns the device address.
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
### Read and Write
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
- **uint8_t read8()** reads all 8 pins at once. This one does the actual reading.
- **uint8_t read(uint8_t pin)** reads a single pin; pin = 0..7
- **uint8_t value()** returns the last read inputs again, as this information is buffered
2020-05-23 13:33:43 +02:00
in the class this is faster than reread the pins.
2021-07-05 10:01:35 +02:00
- **void write8(const uint8_t value)** writes all 8 pins at once. This one does the actual reading.
- **uint8_t write(const uint8_t pin, const uint8_t value)** writes a single pin; pin = 0..7; value is HIGH(1) or LOW (0)
2021-01-29 12:31:58 +01:00
- **valueOut()** returns the last written data.
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
2021-01-29 12:31:58 +01:00
### Button
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
- **void setButtonMask(const uint8_t mask)**
- **uint8_t readButton8()**
- **uint8_t readButton8(const uint8_t mask)**
- **uint8_t readButton(const uint8_t pin)**
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
### Special
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
- **void toggle(const uint8_t pin)** toggles a single pin
- **void toggleMask(const uint8_t mask = 0xFF)** toggles a selection of pins,
2021-01-29 12:31:58 +01:00
if you want to invert all pins use 0xFF (default value).
2021-07-05 10:01:35 +02:00
- **void shiftRight(const uint8_t n = 1)** shifts output channels n pins (default 1) pins right (e.g. leds ).
2020-05-23 13:33:43 +02:00
Fills the higher lines with zero's.
2021-07-05 10:01:35 +02:00
- **void shiftLeft(const uint8_t n = 1)** shifts output channels n pins (default 1) pins left (e.g. leds ).
2020-05-23 13:33:43 +02:00
Fills the lower lines with zero's.
2021-07-05 10:01:35 +02:00
- **void rotateRight(const uint8_t n = 1)** rotates output channels to right, moving lowest line to highest line.
- **void rotateLeft(const uint8_t n = 1)** rotates output channels to left, moving highest line to lowest line.
- **void reverse()** revers the "bit pattern" of the lines, high to low and vice versa.
2021-01-29 12:31:58 +01:00
### Misc
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
- **int lastError()** returns the last error from the lib. (see .h file)
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
## Error codes
2020-05-23 13:33:43 +02:00
2021-07-05 10:01:35 +02:00
| name | value | description |
|:-------------------|:-----:|:------------------------|
| PCF8574_OK | 0x00 | no error |
| PCF8574_PIN_ERROR | 0x81 | pin number out of range |
| PCF8574_I2C_ERROR | 0x82 | I2C communication error |
2020-05-23 13:33:43 +02:00
2021-01-29 12:31:58 +01:00
## Operation
2020-05-23 13:33:43 +02:00
See examples