Posts: 8
Threads: 3
Joined: Jun 2023
Reputation:
0
07-04-2023, 12:59 PM
(This post was last modified: 07-04-2023, 01:01 PM by alessiovietri.)
Hello, i'm tryng to read inputs from the PCF8574 channels. This is my code:
Code: //Apple Homekit for KC868-E16S/KC868-E16T
//first time to use: use serial port send "W" to config wifi ssid and password
//if you want to eraser all data, re-config, use serial port send "E"
//default qrcode is 46637726
#include "HomeSpan.h"
#include "PCF8574.h"
PCF8574 pcfInLeft(0x22);
void pcfInit(){
pcfInLeft.pinMode(P0, INPUT);
pcfInLeft.pinMode(P1, INPUT);
pcfInLeft.pinMode(P2, INPUT);
pcfInLeft.pinMode(P3, INPUT);
pcfInLeft.pinMode(P4, INPUT);
pcfInLeft.pinMode(P5, INPUT);
pcfInLeft.pinMode(P6, INPUT);
pcfInLeft.pinMode(P7, INPUT);
if (pcfInLeft.begin()) {
Serial.println("pcf OK");
}
else{
Serial.println("pcf ERROR");
}
}
void setup() {
Serial.begin(115200);
delay(1000);
pcfInit();
} // end of setup()
//////////////////////////////////////
void loop(){
uint8_t in0 = pcfInLeft.digitalRead(P0);
Serial.println("in0");
Serial.println(in0);
uint8_t in1 = pcfInLeft.digitalRead(P1);
Serial.println("in1");
Serial.println(in1);
uint8_t in2 = pcfInLeft.digitalRead(P2);
Serial.println("in2");
Serial.println(in2);
uint8_t in3 = pcfInLeft.digitalRead(P3);
Serial.println("in3");
Serial.println(in3);
uint8_t in4 = pcfInLeft.digitalRead(P4);
Serial.println("in4");
Serial.println(in4);
uint8_t in5 = pcfInLeft.digitalRead(P5);
Serial.println("in5");
Serial.println(in5);
uint8_t in6 = pcfInLeft.digitalRead(P6);
Serial.println("in6");
Serial.println(in6);
uint8_t in7 = pcfInLeft.digitalRead(P7);
Serial.println("in7");
Serial.println(in7);
delay(1000);
} // end of loop()
But the method pcfInLeft.begin() always returns false and the inputs are always read as off. What's wrong?
Thanks in advance
I forgot to mention that the KCS software knows when the inputs go on, so it's not a wiring-related problem
Posts: 6,675
Threads: 844
Joined: Oct 2020
Reputation:
162
Posts: 8
Threads: 3
Joined: Jun 2023
Reputation:
0
(07-04-2023, 01:14 PM)admin Wrote: you should install pcf8574 library firstly.
demo source code about INPUT and OUTPUT:
https://www.kincony.com/forum/showthread.php?tid=1620
https://www.kincony.com/forum/showthread.php?tid=1619
Thank you very much, i did it for both inputs and outputs:
Code: #include "Arduino.h"
#include "PCF8574.h"
// Set i2c address
PCF8574 pcf8574_in_left(0x22, 4, 5);// 0x22 is address, IO4 is SDA IO5 is SCL
PCF8574 pcf8574_in_right(0x21, 4, 5);// 0x22 is address, IO4 is SDA IO5 is SCL
PCF8574 pcf8574_out_right(0x24, 4, 5);
PCF8574 pcf8574_out_left(0x25, 4, 5);
void setup()
{
Serial.begin(115200);
delay(1000);
// INPUTS
pcf8574_in_left.pinMode(P0, INPUT);
pcf8574_in_left.pinMode(P1, INPUT);
pcf8574_in_left.pinMode(P2, INPUT);
pcf8574_in_left.pinMode(P3, INPUT);
pcf8574_in_left.pinMode(P4, INPUT);
pcf8574_in_left.pinMode(P5, INPUT);
pcf8574_in_left.pinMode(P6, INPUT);
pcf8574_in_left.pinMode(P7, INPUT);
pcf8574_in_right.pinMode(P0, INPUT);
pcf8574_in_right.pinMode(P1, INPUT);
pcf8574_in_right.pinMode(P2, INPUT);
pcf8574_in_right.pinMode(P3, INPUT);
pcf8574_in_right.pinMode(P4, INPUT);
pcf8574_in_right.pinMode(P5, INPUT);
pcf8574_in_right.pinMode(P6, INPUT);
pcf8574_in_right.pinMode(P7, INPUT);
Serial.print("Init pcf8574_in_left...");
if (pcf8574_in_left.begin()) {
Serial.println("OK");
}
else {
Serial.println("KO");
}
Serial.print("Init pcf8574_in_right...");
if (pcf8574_in_right.begin()) {
Serial.println("OK");
}
else {
Serial.println("KO");
}
// OUTPUTS
pcf8574_out_right.pinMode(P0, OUTPUT);
pcf8574_out_right.pinMode(P1, OUTPUT);
pcf8574_out_right.pinMode(P2, OUTPUT);
pcf8574_out_right.pinMode(P3, OUTPUT);
pcf8574_out_right.pinMode(P4, OUTPUT);
pcf8574_out_right.pinMode(P5, OUTPUT);
pcf8574_out_right.pinMode(P6, OUTPUT);
pcf8574_out_right.pinMode(P7, OUTPUT);
pcf8574_out_left.pinMode(P0, OUTPUT);
pcf8574_out_left.pinMode(P1, OUTPUT);
pcf8574_out_left.pinMode(P2, OUTPUT);
pcf8574_out_left.pinMode(P3, OUTPUT);
pcf8574_out_left.pinMode(P4, OUTPUT);
pcf8574_out_left.pinMode(P5, OUTPUT);
pcf8574_out_left.pinMode(P6, OUTPUT);
pcf8574_out_left.pinMode(P7, OUTPUT);
Serial.print("Init pcf8574_out_right...");
if (pcf8574_out_right.begin()) {
Serial.println("PCF8574_out_right_OK");
}
else{
Serial.println("PCF8574_out_right_KO");
}
Serial.print("Init pcf8574_out_left...");
if (pcf8574_out_left.begin()) {
Serial.println("PCF8574_out_left_OK");
}
else{
Serial.println("PCF8574_out_left_KO");
}
}
void loop()
{
/*
for(int i=0;i<=7;i++)
{
pcf8574_out_right.digitalWrite(i, LOW);
delay(500);
}
for(int j=0;j<=7;j++)
{
pcf8574_out_left.digitalWrite(j,LOW);
delay(500);
}
for(int i=0;i<=7;i++)
{
pcf8574_out_right.digitalWrite(i, LOW);
delay(500);
}
for(int j=0;j<=7;j++)
{
pcf8574_out_left.digitalWrite(j,LOW);
delay(500);
}
*/
uint8_t valInLeft1 = pcf8574_in_left.digitalRead(P0);
uint8_t valInLeft2 = pcf8574_in_left.digitalRead(P1);
uint8_t valInLeft3 = pcf8574_in_left.digitalRead(P2);
uint8_t valInLeft4 = pcf8574_in_left.digitalRead(P3);
uint8_t valInLeft5 = pcf8574_in_left.digitalRead(P4);
uint8_t valInLeft6 = pcf8574_in_left.digitalRead(P5);
uint8_t valInLeft7 = pcf8574_in_left.digitalRead(P6);
uint8_t valInLeft8 = pcf8574_in_left.digitalRead(P7);
// Check input 1 left
if (valInLeft1 == LOW) {
Serial.println("INPUT 1 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P0, LOW);
}
else {
pcf8574_out_left.digitalWrite(P0, HIGH);
}
// Check input 2 left
if (valInLeft2 == LOW) {
Serial.println("INPUT 2 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P1, LOW);
}
else {
pcf8574_out_left.digitalWrite(P1, HIGH);
}
// Check input 3 left
if (valInLeft3 == LOW) {
Serial.println("INPUT 3 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P2, LOW);
}
else {
pcf8574_out_left.digitalWrite(P2, HIGH);
}
// Check input 4 left
if (valInLeft4 == LOW) {
Serial.println("INPUT 4 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P3, LOW);
}
else {
pcf8574_out_left.digitalWrite(P3, HIGH);
}
// Check input 5 left
if (valInLeft5 == LOW) {
Serial.println("INPUT 5 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P4, LOW);
}
else {
pcf8574_out_left.digitalWrite(P4, HIGH);
}
// Check input 6 left
if (valInLeft6 == LOW) {
Serial.println("INPUT 6 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P5, LOW);
}
else {
pcf8574_out_left.digitalWrite(P5, HIGH);
}
// Check input 7 left
if (valInLeft7 == LOW) {
Serial.println("INPUT 7 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P6, LOW);
}
else {
pcf8574_out_left.digitalWrite(P6, HIGH);
}
// Check input 8 left
if (valInLeft8 == LOW) {
Serial.println("INPUT 8 LEFT ACTIVATED");
pcf8574_out_left.digitalWrite(P7, LOW);
}
else {
pcf8574_out_left.digitalWrite(P7, HIGH);
}
uint8_t valInRight1 = pcf8574_in_right.digitalRead(P0);
uint8_t valInRight2 = pcf8574_in_right.digitalRead(P1);
uint8_t valInRight3 = pcf8574_in_right.digitalRead(P2);
uint8_t valInRight4 = pcf8574_in_right.digitalRead(P3);
uint8_t valInRight5 = pcf8574_in_right.digitalRead(P4);
uint8_t valInRight6 = pcf8574_in_right.digitalRead(P5);
uint8_t valInRight7 = pcf8574_in_right.digitalRead(P6);
uint8_t valInRight8 = pcf8574_in_right.digitalRead(P7);
// Check input 1 right
if (valInRight1 == LOW) {
Serial.println("INPUT 1 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P0, LOW);
}
else {
pcf8574_out_right.digitalWrite(P0, HIGH);
}
// Check input 2 right
if (valInRight2 == LOW) {
Serial.println("INPUT 2 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P1, LOW);
}
else {
pcf8574_out_right.digitalWrite(P1, HIGH);
}
// Check input 3 right
if (valInRight3 == LOW) {
Serial.println("INPUT 3 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P2, LOW);
}
else {
pcf8574_out_right.digitalWrite(P2, HIGH);
}
// Check input 4 right
if (valInRight4 == LOW) {
Serial.println("INPUT 4 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P3, LOW);
}
else {
pcf8574_out_right.digitalWrite(P3, HIGH);
}
// Check input 5 right
if (valInRight5 == LOW) {
Serial.println("INPUT 5 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P4, LOW);
}
else {
pcf8574_out_right.digitalWrite(P4, HIGH);
}
// Check input 6 right
if (valInRight6 == LOW) {
Serial.println("INPUT 6 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P5, LOW);
}
else {
pcf8574_out_right.digitalWrite(P5, HIGH);
}
// Check input 7 right
if (valInRight7 == LOW) {
Serial.println("INPUT 7 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P6, LOW);
}
else {
pcf8574_out_right.digitalWrite(P6, HIGH);
}
// Check input 8 right
if (valInRight8 == LOW) {
Serial.println("INPUT 8 RIGHT ACTIVATED");
pcf8574_out_right.digitalWrite(P7, LOW);
}
else {
pcf8574_out_right.digitalWrite(P7, HIGH);
}
delay(1000);
}
Posts: 6,675
Threads: 844
Joined: Oct 2020
Reputation:
162
|