PCF8574 służy do rozszerzania liczby portów wejścia/wyjścia poprzez interfejs I2C.
Każdy z 8 pinów PCF8574 może być ustawiony jako wejście lub wyjście.
Po uruchomieniu modułu piny przyjmują stan HIGH
Max. Prąd (sinking current) wynosi 25mA.
Dla stanu wysokiego prąd płynący z pinów wynosi maks. 300uA, a dla stanu niskiego 25mA. W związku z tym, elementy sterowane podłączane są anodą do zasilania 5V, a katodą do PCF8574 lub za pomocą tranzystora NPN.
0 ustawia piny jako wyjściowe, a 1 jako piny wejściowe.
Pin nr 13 (Interrupt) zmienia stan z wysokiego na niski przy zmianie stanu któregokolwiek z pinów cyfrowych. Powinien być podłączony z pullup rezystorem.
Expanderem można sterować za pomocą biblioteki lub standardowego połączenia I2C. 0x20 – Adres domyślny dla PCF8574
Projekt na Tinkercad
PCF8574 Expander

PCF8574 z biblioteką PCF8574 by Renzo Mischianti
Github: https://github.com/xreef/PCF8574_library
#include <Wire.h>
#include <PCF8574.h>
const int addr = 0x20; // Adres domyślny dla PCF8574
PCF8574 pcf8574(addr);
void setup() {
Wire.begin();
pcf8574.begin();
pcf8574.pinMode(0, OUTPUT); // Ustawienie pierwszego pinu jako wyjście
}
void loop() {
pcf8574.digitalWrite(0, HIGH);
delay(1000);
pcf8574.digitalWrite(0, LOW);
delay(1000);
}
Zarządzanie PCF8574 przez I2C
Wysyłanie danych do PCF88574
// Wysyłanie danych do PCF88574
#include <Wire.h>
const int PCF8574_ADDR = 0x20;
void setup() {
delay(1000);
Wire.begin();
Wire.beginTransmission(PCF8574_ADDR);
Wire.write(0b11110110); // Bit 0 i 3 OUTPUT
Wire.endTransmission();
}
void loop() {}
Pobieranie stanów pinów PCF8574
// Pobieranie stanów pinów PCF8574
#include <Wire.h>
const int PCF8574_ADDR = 0x20;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.requestFrom(PCF8574_ADDR, 1);
if (Wire.available()) {
byte data = Wire.read();
Serial.println(data, BIN);
}
delay(500);
}
Pobieranie stanów pinów po ich zmianie
// Pobieranie stanów pinów po ich zmianie
#include <Wire.h>
const int PCF8574_ADDR = 0x20;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
if (digitalRead(2) == 0) {
Wire.requestFrom(PCF8574_ADDR, 1);
if (Wire.available()) {
byte data = Wire.read();
Serial.println(data, BIN);
delay(200);
}
}
}
Pobieranie stanów pinów z interrupt
// Pobieranie stanów pinów z interrupt
#include <Wire.h>
#define PCF8574_ADDR 0x20
volatile bool canGetData = false;
void setup() {
Serial.begin(9600);
Wire.begin();
attachInterrupt(digitalPinToInterrupt(2), handleInterrupt, FALLING);
}
void loop() {
if (canGetData) {
canGetData = false;
Wire.requestFrom(PCF8574_ADDR, 1);
if (Wire.available()) {
byte newData = Wire.read();
Serial.println(newData, BIN);
}
}
}
void handleInterrupt() {
canGetData = true;
}