Help needed with PCF 8574 i2c

I'm having an issue with the PCF8574 on esp8266 project. Mainly, in my prototyping, I used some PCF8574N chips that I had in stock. My PCB has them configured at address 0x20 by connecting all address pins to VCC. I just ordered a lot of new PCF8574's and they are the P type (PCF8574P) vs N type which I can't find any more. Turns out their default address is 0x38 when all address pins are connected to vcc.

I had sent my pcb's to the assembly house and they intermixed the chips on my boards. So I need to develop code to determine what type of chip is installed and use the address 0x20 or 0x38 as appropriate.

I am able to scan the i2c bus with both addresses and determine what address to use, except I can't figure out how to redeclare the library to use the new address. Arduino compiler requires that I declare the library before setup() as shown below or else throws all kinds of errors, and if I redeclare with the proper address it compiles but does not accept the redeclared address. Can someone help me how I can re-assign the address a library is using? Thanks in advance!

Here are my declares

#include <pcf8574_esp.h>
#include <Wire.h>

PCF857x pcf8574(0x39, &Wire);

setup(){

Wire.begin(4, 5);
Wire.setClock(100000L);

byte error, address;

//look for I/O address 0x20 or 0x38
address=0x20;
Wire.beginTransmission(address);
error = Wire.endTransmission();

if (error == 0) { //we found it on 0x20
PCF857x pcf8574(0x20, &Wire);
Serial.println("0x21");
}
else { //address not found, try 0x38
PCF857x pcf8574(0x38, &Wire);
Serial.println("0x39");
}

pcf8574.begin();

}

Is it possible to change the adress on the circuitboard to fit the library?
Does the library not accept that You set the adress when iniating in Setup?

Railroader:
Is it possible to change the adress on the circuitboard to fit the library?

Dont think so, different versions of the PCF8474 do seem to have differerent addresses.

Thats life I guess, just because someone sells a device that is compatible with a PCF8474, there is no guarantee that the addreses are the same.

You better go down in the grotto and do some heavy thinking. Unexpectedly different adresses sounds difficult.
I hope the software support can bring You a solution.

It would be pretty straight forward to add a new function setAddress() that changes the i2c address (which is a private variable in the class).

You can either do it yourself, or possibly suggest it to the author

This is the method that should be modified to specify the I2C address:
pcf8574.begin();

You should be able get away using the existing library code by creating a new object that is copied on top of the global object you have created.
Note that depending on the esp implementation it might end up creating a tiny fragmentation of RAM for the original object, but it should be a non issue given the small size of the object and the size of the RAM on the esp parts.

If you are always going to fill in the address then the address in the global object doesn't matter.
However, alternatively you could assume a particular address - say 0x38 - and only overwrite the object if you are using a part with a different address.

Here is some updated code from your sample code.

#include <pcf8574_esp.h>
#include <Wire.h>

// create an object and override it if it isn't 0x38
PCF857x pcf8574(0x38, &Wire); // create default object

// If library code supported a default constructor you could do this instead to create a dummy object:
// PCF857x pcf8574;

setup(){
  Wire.begin(4, 5);
  Wire.setClock(100000L);

  byte error, address;

  //look for I/O address 0x20 or 0x38
  address=0x20;
  Wire.beginTransmission(address);
  error = Wire.endTransmission();

  if (error == 0) {   //we found it on 0x20
    pcf8574 = PCF857x(0x20, &Wire);  // overwrite object with real parameters
  }
  else {     //address not found, try 0x38
    pcf8574 = PCF857x(0x38, &Wire); // overwrite object with real parameters
  }

  pcf8574.begin();
}

--- bill