#include <Wire.h>
#include <Adafruit_PCF8574.h>
#define PCF8574_ADDRESS 0x27 // I2C address of the PCF8574
Adafruit_PCF8574 pcf8574;
const int INPUT_PIN_1 = 0; // Input pin 1
const int INPUT_PIN_2 = 1; // Input pin 2
const int INPUT_PIN_3 = 2; // Input pin 3
const int INPUT_PIN_4 = 3; // Input pin 4
const int OUTPUT_PIN_1 = 4; // Output pin 1
const int OUTPUT_PIN_2 = 5; // Output pin 2
const int OUTPUT_PIN_3 = 6; // Output pin 3
const int OUTPUT_PIN_4 = 7; // Output pin 4
void setup() {
Wire.begin();
// Initialize the PCF8574
if (!pcf8574.begin(PCF8574_ADDRESS)) {
Serial.println("Failed to initialize PCF8574!");
while (1);
}
// Set the input pins to INPUT_PULLUP mode
pcf8574.pinMode(INPUT_PIN_1, INPUT);
pcf8574.pinMode(INPUT_PIN_2, INPUT);
pcf8574.pinMode(INPUT_PIN_3, INPUT);
pcf8574.pinMode(INPUT_PIN_4, INPUT);
// Set the output pins to OUTPUT mode
pcf8574.pinMode(OUTPUT_PIN_1, OUTPUT);
pcf8574.pinMode(OUTPUT_PIN_2, OUTPUT);
pcf8574.pinMode(OUTPUT_PIN_3, OUTPUT);
pcf8574.pinMode(OUTPUT_PIN_4, OUTPUT);
// Initialize the serial communication
Serial.begin(9600);
Serial.println('Running');
}
void loop() {
// Read the state of the input pins
bool inputState1 = !pcf8574.digitalRead(INPUT_PIN_1);
bool inputState2 = !pcf8574.digitalRead(INPUT_PIN_2);
bool inputState3 = !pcf8574.digitalRead(INPUT_PIN_3);
bool inputState4 = !pcf8574.digitalRead(INPUT_PIN_4);
// Toggle the state of the output pins
pcf8574.digitalWrite(OUTPUT_PIN_1, !pcf8574.digitalRead(OUTPUT_PIN_1));
pcf8574.digitalWrite(OUTPUT_PIN_2, !pcf8574.digitalRead(OUTPUT_PIN_2));
pcf8574.digitalWrite(OUTPUT_PIN_3, !pcf8574.digitalRead(OUTPUT_PIN_3));
pcf8574.digitalWrite(OUTPUT_PIN_4, !pcf8574.digitalRead(OUTPUT_PIN_4));
// Print the state of the input and output pins
Serial.print("Input states: ");
Serial.print(inputState1);
Serial.print(" ");
Serial.print(inputState2);
Serial.print(" ");
Serial.print(inputState3);
Serial.print(" ");
Serial.print(inputState4);
Serial.print(" | Output states: ");
Serial.print(pcf8574.digitalRead(OUTPUT_PIN_1));
Serial.print(" ");
Serial.print(pcf8574.digitalRead(OUTPUT_PIN_2));
Serial.print(" ");
Serial.print(pcf8574.digitalRead(OUTPUT_PIN_3));
Serial.print(" ");
Serial.println(pcf8574.digitalRead(OUTPUT_PIN_4));
delay(1000);
}