Smart Home Automation Forum
PCF8574 IO Expansion i2c digital input board -DI8 arduino demo code - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: Extender module (https://www.kincony.com/forum/forumdisplay.php?fid=57)
+--- Thread: PCF8574 IO Expansion i2c digital input board -DI8 arduino demo code (/showthread.php?tid=4590)



PCF8574 IO Expansion i2c digital input board -DI8 arduino demo code - admin - 02-23-2024

Code:
#include "Arduino.h"
#include "PCF8574.h"
// Set i2c address
PCF8574 pcf8574(0x27,4,5); //4 is SDA 5 is SCL need replace by your board's pin define
/*
A0  A1  A2
0    0    0    =0x20
0    0    1    =0x21
0    1    0    =0x22
0    1    1    =0x23
1    0    0    =0x24
1    0    1    =0x25
1    1    0    =0x26
1    1    1    =0x27
*/
unsigned long timeElapsed;
void setup()
{
    Serial.begin(115200);
    delay(1000);
//    pcf8574.pinMode(P0, OUTPUT);
pcf8574.pinMode(P0, INPUT);
pcf8574.pinMode(P1, INPUT);
pcf8574.pinMode(P2, INPUT);
pcf8574.pinMode(P3, INPUT);
pcf8574.pinMode(P4, INPUT);
pcf8574.pinMode(P5, INPUT);
pcf8574.pinMode(P6, INPUT);
pcf8574.pinMode(P7, INPUT);
    Serial.print("Init pcf8574...");
    if (pcf8574.begin()){
        Serial.println("OK");
    }else{
        Serial.println("KO");
    }
}
void loop()
{
uint8_t val1 = pcf8574.digitalRead(P0);
uint8_t val2 = pcf8574.digitalRead(P1);
uint8_t val3 = pcf8574.digitalRead(P2);
uint8_t val4 = pcf8574.digitalRead(P3);
uint8_t val5 = pcf8574.digitalRead(P4);
uint8_t val6 = pcf8574.digitalRead(P5);
uint8_t val7 = pcf8574.digitalRead(P6);
uint8_t val8 = pcf8574.digitalRead(P7);
if (val1==LOW) Serial.println("KEY1 PRESSED");
if (val2==LOW) Serial.println("KEY2 PRESSED");
if (val3==LOW) Serial.println("KEY3 PRESSED");
if (val4==LOW) Serial.println("KEY4 PRESSED");
if (val5==LOW) Serial.println("KEY5 PRESSED");
if (val6==LOW) Serial.println("KEY6 PRESSED");
if (val7==LOW) Serial.println("KEY7 PRESSED");
if (val8==LOW) Serial.println("KEY8 PRESSED");
    delay(300);
}

arduino code download:

.zip   DI8.zip (Size: 594 bytes / Downloads: 106)

PCF8574 arduino library ZIP file download:

.zip   PCF8574_library.zip (Size: 23.8 MB / Downloads: 129)