Smart Home Automation Forum
[Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A16 (https://www.kincony.com/forum/forumdisplay.php?fid=25)
+--- Thread: [Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI (/showthread.php?tid=1619)



[Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI - KinCony Support - 01-05-2022

Code 6: //The demo code is PCF8574-DI    You can copy the code to your Arduino IDE

Code:
#include "Arduino.h"
#include "PCF8574.h"

// Set i2c input address 
PCF8574 pcf8574_1(0x22,4,5);  // channel 1-8    address: 100010
PCF8574 pcf8574_2(0x21,4,5);  //channel 9-16    address: 100001



unsigned long timeElapsed;
void setup()
{
    Serial.begin(115200);
    delay(1000);
pcf8574_1.pinMode(P0, INPUT);
pcf8574_1.pinMode(P1, INPUT);
pcf8574_1.pinMode(P2, INPUT);
pcf8574_1.pinMode(P3, INPUT);
pcf8574_1.pinMode(P4, INPUT);
pcf8574_1.pinMode(P5, INPUT);
pcf8574_1.pinMode(P6, INPUT);
pcf8574_1.pinMode(P7, INPUT);

pcf8574_2.pinMode(P0, INPUT);
pcf8574_2.pinMode(P1, INPUT);
pcf8574_2.pinMode(P2, INPUT);
pcf8574_2.pinMode(P3, INPUT);
pcf8574_2.pinMode(P4, INPUT);
pcf8574_2.pinMode(P5, INPUT);
pcf8574_2.pinMode(P6, INPUT);
pcf8574_2.pinMode(P7, INPUT);

    Serial.print("Init pcf8574...");
    if (pcf8574_1.begin()){
        Serial.println("pcf8574_1_OK");
    }else{
        Serial.println("pcf8574_1_KO");
    }

  Serial.print("Init pcf8574...");
  if (pcf8574_2.begin()){
    Serial.println("pcf8574_2_OK");
  }else{
    Serial.println("pcf8574_2_KO");
  }


}

void loop()
{
uint8_t val1 = pcf8574_1.digitalRead(P0);
uint8_t val2 = pcf8574_1.digitalRead(P1);
uint8_t val3 = pcf8574_1.digitalRead(P2);
uint8_t val4 = pcf8574_1.digitalRead(P3);
uint8_t val5 = pcf8574_1.digitalRead(P4);
uint8_t val6 = pcf8574_1.digitalRead(P5);
uint8_t val7 = pcf8574_1.digitalRead(P6);
uint8_t val8 = pcf8574_1.digitalRead(P7);

uint8_t val9 = pcf8574_2.digitalRead(P0);
uint8_t val10 = pcf8574_2.digitalRead(P1);
uint8_t val11 = pcf8574_2.digitalRead(P2);
uint8_t val12 = pcf8574_2.digitalRead(P3);
uint8_t val13 = pcf8574_2.digitalRead(P4);
uint8_t val14 = pcf8574_2.digitalRead(P5);
uint8_t val15 = pcf8574_2.digitalRead(P6);
uint8_t val16 = pcf8574_2.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");

if (val9==LOW) Serial.println("KEY1 PRESSED");
if (val10==LOW) Serial.println("KEY2 PRESSED");
if (val11==LOW) Serial.println("KEY3 PRESSED");
if (val12==LOW) Serial.println("KEY4 PRESSED");
if (val13==LOW) Serial.println("KEY5 PRESSED");
if (val14==LOW) Serial.println("KEY6 PRESSED");
if (val15==LOW) Serial.println("KEY7 PRESSED");
if (val16==LOW) Serial.println("KEY8 PRESSED");
delay(300);
}
   
   


RE: [Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI - -Teme- - 01-05-2022

Based of your schema, i2c address are another way around than in config above - which one is correct?
from schema; PCF8574 (0x22) ports IN D1...8 and (0x21) ports IN D9...16
https://www.kincony.com/download/KC868-A16-schematic.pdf


RE: [Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI - admin - 01-05-2022

You are right.

For KC868-A16

// Set i2c address for output
PCF8574 pcf8574_1(0x24);  // channel 1-8     adress:100100
PCF8574 pcf8574_2(0x25);  //channel 9-16    adress:100101

// Set i2c address for input
PCF8574 pcf8574_1(0x22);  // channel 1-8     adress: 100010
PCF8574 pcf8574_2(0x21);  //channel 9-16    adress: 100001

   


RE: [Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI - schel4ok - 02-07-2022

What is the reason to restrict 16 pins only for input and another 16pins only for output?
What happens if I configure certain output pin as input?
In my project I need more inputs and in Arduino boards pins can be configured without any limits.


RE: [Arduino IDE demo source code for KC868-A16]--#07-PCF8574-DI - admin - 02-11-2022

because input pins have optocoupler isolator. output pins connect with MOSFET.
how many input and output ports you will use?