I would like to use the PCF8574 I/O expander with my Nano ESP32.
I'm trying to control a midi note button through the expander but I'm unable to assign the input to the button. The button is connected to the pin P0 of the expander.
Could you please help?
Please see the schematic and sketch attached.
Sorry I am not familiar with your libraries. I simply write a 1 to the appropriate bit that I want as an input. I then read the port and when the switch is closed that bit will be zero otherwise it will be a 1.
#define PCF8574_ADDR 0x20
// This gets called from setup to initialize the port
void initializePCF8574() {
// Configure all pins as inputs by writing 0xFF to the PCF8574
Wire.beginTransmission(PCF8574_ADDR);
Wire.write(0xFF); // Set all pins as inputs (pull-up resistors are enabled by default)
Wire.endTransmission();
}
//This fnction will return the port value, call from anywhere
uint8_t readPCF8574() {
Wire.beginTransmission(PCF8574_ADDR);
Wire.endTransmission();
Wire.requestFrom(PCF8574_ADDR, 1); // Request 1 byte from PCF8574
if (Wire.available()) {
return Wire.read(); // Read and return the byte (input values of all 8 pins)
}
return 0xFF; // Return 0xFF if no data is available (indicating an error)
}
Have fun!
I'm sure your code will work to read the pin, but...
I agree with @rsmls. The Control_Surface library will work only with native Arduino pins, not the pins of a pcf857x chip or any other I/o expander (unless you modify the library).
No, the op wants the NoteButton class from the Control_Suface library to work with a pin of an I/o expander , but it expects to be given an Arduino pin number. There is no pin number you could give it that would make it read the pin on the I/o expander.
In projects with large numbers of inputs and outputs, Control Surface allows you to seamlessly add multiplexers, shift registers and other port expanders, and treat them as if they were ordinary GPIO pins.