06-25-2023, 12:23 PM
How to use two PCF8574s for DI and DO both?
Next program works correctly when I use only one PCF8574 (adress: 0x22; I2C pins: 4,15) that controls DI inputs on the board:
1.png
But it doesn't work when I uncomment code in red frames, trying to add another PCF8574 (adress: 0x24; I2C pins: 4,15) that controls DO outputs on the board.
2.png
Correct code for using digital input PCF8574 without errors "1.png"
Next program works correctly when I use only one PCF8574 (adress: 0x22; I2C pins: 4,15) that controls DI inputs on the board:
1.png
But it doesn't work when I uncomment code in red frames, trying to add another PCF8574 (adress: 0x24; I2C pins: 4,15) that controls DO outputs on the board.
2.png
Correct code for using digital input PCF8574 without errors "1.png"
Code:
// Library: PCF8574_Library by RENZO MISCHIANTI
// https://mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
#include "Arduino.h"
#include "PCF8574.h"
// Set i2c address
PCF8574 pcf8574i(0x22,4,15);
// PCF8574 pcf8574(0x24,4,15);
unsigned long timelapsed;
void setup()
{
Serial.begin(115200);
delay(1000);
pcf8574i.pinMode(0, INPUT);
pcf8574i.pinMode(1, INPUT);
// pcf8574.pinMode(0, OUTPUT);
// pcf8574.pinMode(1, OUTPUT);
// sleep(100);
// pcf8574.digitalWrite(0, LOW);
// pcf8574.digitalWrite(1, LOW);
delay(2000);
Serial.print("Init pcf8574i...");
if (pcf8574i.begin()){
Serial.println("OK");
}else{
Serial.println("KO");
}
// Serial.print("Init pcf8574...");
// if (pcf8574.begin()){
// Serial.println("OK");
// }else{
// Serial.println("KO");
// }
}
void loop()
{
uint8_t val1 = pcf8574i.digitalRead(0);
uint8_t val2 = pcf8574i.digitalRead(1);
if (val1==LOW) Serial.println("KEY1 PRESSED");
if (val2==LOW) Serial.println("KEY2 PRESSED");
// if (val1==LOW) {
// Serial.println("KEY1 PRESSED");
// // pcf8574.digitalWrite(0, HIGH);
// }
// if (val2==LOW) {
// Serial.println("KEY2 PRESSED");
// // pcf8574.digitalWrite(1, HIGH);
// }
delay(100);
// Serial.println("--");
}