This library is for use of I2C analog IC with Arduino and ESP8266. It can read and write digital values with only 2 wires (perfect for ESP-01).
You can find updated version of my library on my site https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/Tutorial:
To download, click the DOWNLOADS button in the top right corner and rename the uncompressed folder PCF8574. Check that the PCF8574 folder contains PCF8574\\.cpp and PCF8574.h. Place the DHT library folder in your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if it's your first library. Restart the IDE.
Complete the PCF8574 digital input and output expander with i2c bus. I try to simplify the use of this IC with a minimal set of operations.
Constructor: you must pass the address of the I2C (to check the address, use this guide: I2cScanner).
For ESP8266 if you want specify SDA and SCL pins, use this method. You must set input/output mode:
pcf8574.pinMode(P0, OUTPUT);
pcf8574.pinMode(P1, INPUT);
pcf8574.pinMode(P2, INPUT);
Then IC, as you can see in the image, has 8 digital inputs/outputs:
So, to read all analog inputs in one transmission, you can use this (even if I use a 10 millis debounce time to prevent too much read from I2C):
PCF8574::DigitalInput di = PCF8574.digitalReadAll();
Serial.print(di.p0);
Serial.print(" - ");
Serial.print(di.p1);
Serial.print(" - ");
Serial.print(di.p2);
Serial.print(" - ");
Serial.println(di.p3);
If you want to read a single input or if you want to write a digital value you can. Or you can also use an interrupt pin. You must initialize the pin and the function to call when interrupt is raised from PCF8574:
// Function interrupt
void keyPressedOnPCF8574();
// Set i2c address
PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
Remember you can't use Serial or Wire on the interrupt function. The better way is to set only a variable to read on loop:
void keyPressedOnPCF8574(){
// Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
keyPressed = true;
}
For the examples, I used this wire schema on my breadboard:





Comments