B4R Library library for PCF8574

it is a wrapper for PCF8574 based of arduino library from https://github.com/RobTillaart/PCF8574/

functions are similar to original library, except initialization adapted to B4R.
It can work on Wire, Wire1 or Wire2, on hard I2C and on software I2C and we can have several 8574 on same I2C
## initialization
- Initialize(deviceAddress) // for all board with default I2C
- Initializeesp32(deviceAddress, I2Cbus) //only for esp32
device address: for PCF8574 : 0x20 to 0x27 for PCF8574A : 0x38 to 0x3F
I2Cbus can be define with a parameter provided by library :Wire or Wire1 or Wire2 (Wire by default)
- begin(initB) // set the initial value for the pins and masks.
- beginesp(sda, scl, initB) // only for esp8266 and esp32 to change pins
- isConnected() // checks if the address is visible on the I2C bus

### Read and Write
- **read8()** reads all 8 pins at once. This one does the actual reading.
- **read(pin)** reads a single pin; pin = 0..7
- **value()** returns the last read inputs again, as this information is buffered in the class this is faster than reread the pins.
- **write8(value)** writes all 8 pins at once. This one does the actual reading.
- **write(pin, value)** writes a single pin; pin = 0..7; value is HIGH(1) or LOW (0)
- **valueOut()** returns the last written data.

### Button
- **setButtonMask(mask)**
- **readButton8()**
- **readButton8Mask(mask)**
- **readButton(pin)**

### More
- **toggle(pin)** toggles a single pin
- **toggleMask(mask)** toggles a selection of pins, if you want to invert all pins use 0xFF (default value).
- **shiftRight(n = 1)** shifts output channels n pins (default 1) pins right (e.g. leds ).Fills the higher lines with zero's.
- **shiftLeft(n = 1)** shifts output channels n pins (default 1) pins left (e.g. leds ).Fills the lower lines with zero's.
- **rotateRight(n = 1)** rotates output channels to right, moving lowest line to highest line.
- **rotateLeft(n = 1)** rotates output channels to left, moving highest line to lowest line.
- **reverse()** revers the "bit pattern" of the lines, high to low and vice versa.

### Misc
- **lastError()** returns the last error from the lib. (see .h file)

## Error codes
| name | value | description |
| PCF8574_OK | 0x00 | no error
| PCF8574_PIN_ERROR | 0x81 | pin number out of range |
| PCF8574_I2C_ERROR | 0x82 | I2C communication error |

22/08/02 new version for esp32 compatibility
a few changes in initialize and begin to be compatible multiple boards
'I2C config is depending depending of hard:
' with defaumt Wire, pins by default For only one I2C
' SCL : GPIO 22 (I2C -> Data)
' SDA : GPIO 21 (I2C -> Clock)
' pcf.Initialize(0x20)
' pcf.begin(0xFF)

' with TwoWire pins can be changed
' SDA : GPIO 33
' SCL : GPIO 32
' pcf.Initializeesp32(0x20,pcf.I2C_TwoWire0)
' pcf.beginesp(33,32,0xFF)

' with TwoWire both I2C can be used
' pins by default can be used For I2C one, but can be changes also
' I2C Bus 1: uses GPIO 27 (SDA) And GPIO 26 (SCL);
' I2C Bus 2: uses GPIO 33 (SDA) And GPIO 32 (SCL);
' pins must be defined For second I2C
' Private pcf1 As PCF8574
' Private pcf2 As PCF8574
' pcf1.Initializeesp32(0x20,pcf.I2C_TwoWire0)
' pcf1.beginesp(27,26,0xFF)
' pcf2.Initializeesp32(0x20,pcf.I2C_TwoWire1)
' pcf2.beginesp(33,32,0xFF)

' with Wire pins by default:
' SCL : GPIO 5 D1
' SDA : GPIO 4 D2
' pcf.Initialize(0x20)
' pcf.begin(0xFF) pins by default
' pins can be changed
' pcf.Initialize(0x20)
' pcf.beginesp(33,32,0xFF) if pins must be changed

'Arduino Nano
' SDA: A4
' SCL: A5
' pcf.Initialize(0x20)
' pcf.begin(0xFF)

'Arduino Uno
' SDA: PIN18
' SCL: PIN19
' pcf.Initialize(0x20)
' pcf.begin(0xFF)

'Arduino Mega
' SDA: PIN20
' SCL: PIN21
' pcf.Initialize(0x20)
' pcf.begin(0xFF)


  • rPCF8574_2.zip
    7.5 KB · Views: 259
Last edited:


Active Member
Licensed User
Sorry i need you help.

I have a old Arduino-Nano. I have use this Libs but i have a lot of Errors.

Can you say me, how i init This Libary.

her a a piece of the Error-Log

SerialDiscovery.java:96) [arduino-core.jar:?]
    at java.util.TimerThread.mainLoop(Timer.java:555) [?:1.8.0_191]
    at java.util.TimerThread.run(Timer.java:505) [?:1.8.0_191]
C:\0-daten\0-BASI~1\0-MY_P~1\16-MAT~1\Objects\bin\sketch\rPCF8574.cpp: In member function 'bool B4R::B4RPCF8574::begin(byte, byte, byte)':
rPCF8574.cpp:18:39: error: no matching function for call to 'PCF8574::begin(byte&, byte&, byte&)'
        return rcs->begin(sda, scl, add);
In file included from C:\0-daten\0-BASI~1\0-MY_P~1\16-MAT~1\Objects\bin\sketch\rPCF8574.h:4:0,
                 from C:\0-daten\0-BASI~1\0-MY_P~1\16-MAT~1\Objects\bin\sketch\B4RDefines.h:27,
                 from C:\0-daten\0-BASI~1\0-MY_P~1\16-MAT~1\Objects\bin\sketch\rPCF8574.cpp:2:
C:\0-daten\0-BASI~1\0-MY_P~1\16-MAT~1\Objects\bin\sketch\PCF8574.h:37:11: note: candidate: bool PCF8574::begin(uint8_t)
   bool    begin(uint8_t val = PCF8574_INITIAL_VALUE);
C:\0-daten\0-BASI~1\0-MY_P~1\16-MAT~1\Objects\bin\sketch\PCF8574.h:37:11: note:   candidate expects 1 argument, 3 provided
exit status 1

Sorry for my bad english. Im from Germany.




Active Member
Licensed User
in fact you have 2 cases, depending of hard of your board :

case hard wire (normally name is "wire")
pcf.Initialize(0x20,pcf.I2C_Wire) => wire selected
pcf.begin1(0xFF) => init at FF

case soft wire: you can select wire, wire1, wire2
pcf.Initialize(0x20,pcf.I2C_Wire2) => wire2 in this case
pcf.begin(5,14,0xFF) => pin 5 and pin 14 must exist in this configuration

after, if it is not working, please to share your hard configuration and your code.


Active Member
Licensed User
in fact you have 2 configurations in this library :
- Initialize(deviceAddress) => for all board with 1 I2C or more I2C bus, and only default I2C bus is used

- Initializeesp32(deviceAddress, I2Cbus) =>only for esp32 with 2 I2C bus and you don't want to use default I2C

if you are using an other processor with multiple I2C bus it will block...

after, you have 2 begin :
- begin(initB) => set the initial value for the pins and masks.(I2C pins are hard configuration)

- beginesp(sda, scl, initB) => only for esp8266 and esp32 and you can change pins configuration