I have an issue with a PCF8575 connected to a CYD. I’m using the PCF8575 library by Renzo Mischianti.. I’ve set up everything correctly as you can see from my Sketch. The problem is……. the pinouts are all labelled different on the board after P07 which is not an issue. This is the board I bought:
And here’s my sketch.
/*
******************* SETUP FOR CYD USING CN1 CONNECTER PINS 22 AND 27 *********************
https://www.donskytech.com/esp32-pcf8575-port-expander/
*/
#include <Wire.h>
#include "PCF8575.h"
// Define the custom I2C pins
#define I2C_SDA_PIN 27
#define I2C_SCL_PIN 22
byte error, address;
// Initialize the PCF8575 at address 0x20
PCF8575 pcf8575(0x20);
//For loop
uint8_t i = 0;
//in the Global Scope (Below the #include and above the setup())
//Up top period is set. 1000 for 1 second
const long period = 1000; //delay period
unsigned long time_now = millis();
void setup() {
Serial.begin(115200);
//CYD Pins
//Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
// Use the constructor that accepts SDA and SCL pins directly
// Check your specific library documentation for exact signature
Wire.end(); // End default I2C (optional but good practice)
Wire.setPins(I2C_SDA_PIN, I2C_SCL_PIN); // Set custom pins: SDA to GPIO 27, SCL to GPIO 22
Wire.begin();
//PCF8575 pcf8575(0x20, I2C_SDA_PIN, I2C_SCL_PIN); // Example: Address 0x20, SDA GPIO 14, SCL GPIO 15
// Set outputs
pcf8575.pinMode(0, OUTPUT);
pcf8575.pinMode(1, OUTPUT);
pcf8575.pinMode(2, OUTPUT);
pcf8575.pinMode(3, OUTPUT);
pcf8575.pinMode(4, OUTPUT);
pcf8575.pinMode(5, OUTPUT);
pcf8575.pinMode(6, OUTPUT);
pcf8575.pinMode(7, OUTPUT);
pcf8575.pinMode(8, OUTPUT);
// Set inputs
pcf8575.pinMode(9, INPUT);
pcf8575.pinMode(10, INPUT);
pcf8575.pinMode(11, INPUT);
pcf8575.pinMode(12, INPUT);
pcf8575.pinMode(13, INPUT);
pcf8575.pinMode(14, INPUT);
pcf8575.pinMode(15, INPUT);
//Initialise PCF8575
pcf8575.begin();
//Set time_now value to now
time_now = millis(); //Set time_now to the millis() value
//Turn all LEDs off
allLEDsOff();
Scan();
}
void loop() {
if (millis() > time_now + period) {
Serial.print("Lighting LED: ");
Serial.println(i);
pcf8575.digitalWrite(i, HIGH);
i = i + 1;
time_now = millis();
//uint8_t val0 = pcf8575.digitalRead(8);
uint8_t val1 = pcf8575.digitalRead(9);
uint8_t val2 = pcf8575.digitalRead(10);
uint8_t val3 = pcf8575.digitalRead(11);
uint8_t val4 = pcf8575.digitalRead(12);
uint8_t val5 = pcf8575.digitalRead(13);
uint8_t val6 = pcf8575.digitalRead(14);
uint8_t val7 = pcf8575.digitalRead(15);
/*
Serial.print("******** Pin 8 value: ");
Serial.println(val0);
*/
Serial.print("******** Pin 9 value: ");
Serial.println(val1);
Serial.print("******** Pin 10 value: ");
Serial.println(val2);
Serial.print("******** Pin 11 value: ");
Serial.println(val3);
Serial.print("******** Pin 12 value: ");
Serial.println(val4);
Serial.print("******** Pin 13 value: ");
Serial.println(val5);
Serial.print("******** Pin 14 value: ");
Serial.println(val6);
Serial.print("******** Pin 15 value: ");
Serial.println(val7);
}
if(i == 10){
i = 0;
allLEDsOff();
}
}
void allLEDsOff() {
for (int LEDPin = 0; LEDPin <= 8; LEDPin++) {
Serial.println("Resetting LEDs");
pcf8575.digitalWrite(LEDPin, LOW);
}
}
void Scan() {
int nDevices = 0;
delay(5000);
Serial.println("Scanning for I2C devices ...");
for (address = 0x01; address < 0x7f; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.printf("I2C device found at address 0x%02X\n", address);
nDevices++;
} else if (error != 2) {
Serial.printf("Error %d at address 0x%02X\n", error, address);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found");
}
}
I’ve set up pins 0 to 8 (9 pins) set to OUTPUTS and 9 through 15 as INPUTS. As you can see from the diagram pins 8 & 9 are missing, which again is not an issue. I’ve proved that the pins continue as P10 is actually P8, P11 is P9 etc which all seem to work. BUT…. This is where it starts getting weird. I’ve only tried the following on pins 9 & 10 so far. Pin 9 works great as an input until pin 8 goes high then low, then it stays high no matter what state is applied to the pin. The same also happens to pin 10, when I had pin 9 as an output, pin 10 stays in a high, after pin 9 goes high then low, even when connected to ground. I’ve tried pull-up and pull down resistors and left them without resistors but nothing seems to help.
I’ve got A0, A1 and A2 all soldered to ground so I2C address is 0x20. Am I missing something here?


