Arduino library for PCF8574 IO expander module
Should work on most of the boards, tested on UNO, ESP32 and ESP8266
Everything is in the examples. Library is available in the Arduino Library Manager (Sketch -> Include Library -> Manage Libraries...).
#include <pcf8574.h>
PCF8574 ex1(0x20);
void setup() {
delay(200);
pinMode(ex1, 1, OUTPUT);
digitalWrite(ex1, 1, HIGH);
}
void loop() {}
#include <pcf8574.h>
PCF8574 ex1(0x20);
void setup() {
delay(200);
Serial.begin(115200);
pinMode(ex1, 0, INPUT_PULLUP);
}
void loop() {
int state = digitalRead(ex1, 0);
if(state == -1) Serial.println("PCF8574 not detected");
Serial.println(String(state));
delay(500);
}
#include <pcf8574.h>
PCF8574 ex1(0x20);
void setup() {
delay(200);
pinMode(ex1, 1, OUTPUT);
}
void loop() {
digitalToggle(ex1, 1);
delay(500);
}
#include <pcf8574.h>
void setup() {
delay(200);
Serial.begin(115200);
// SEARCH FOR PCF8574
for(int i = 0;i < 8;i++) {
int address = PCF8574::combinationToAddress(i, false);
if(PCF8574(address).read() != -1) {
Serial.print("Found PCF8574: addr = 0x");
Serial.println(address, HEX);
}
}
// SEARCH FOR PCF8574A
for(int i = 0;i < 8;i++) {
int address = PCF8574::combinationToAddress(i, true);
if(PCF8574(address).read() != -1) {
Serial.print("Found PCF8574A: addr = 0x");
Serial.println(address, HEX);
}
}
Serial.println("Search done. If you don't see anything, nothing was found.");
}
void loop() {}
PCF8574:
- Has only one 8-bit register to write
- So, the pin can be in two states - connected to GND or (100uA) pull-up'ed
- If register bit corresponding to IO is 0, pin is connected to GND
- If register bit corresponding to IO is 1, pin has 100uA pull-up
- This pull-up is used for both input-pullup and output high
- When pin changes state from 0 to 1 it's connected to VCC for a moment
- This "moment" is equal to I2C ACK time
- It uses two I2C addresses - one for write and second for read
- If "write" address is 0x20, read will be 0x21 (incremented)
- Addresses are set with onboard jumpers, but combination is not the address
- For PCF8574, first write address is 0x20 and last write address is 0x27
- For PCF8574A, first write address is 0x38 and last write address is 0x3F
- attachInterrupt(function, pcf, pin, mode)
- PCF8574 connection from any MCU pins