Arduino and PCF8574 - Restart (return) communication I2c

I would ask colleagues for help, namely:
I’m testing PCF8574 connected to the Arduino Mega

PCF works as inputs.
I’m using the library PCF8574.h

I did a test - temporarily lost connection by disconnecting SDA or SCL line.

After this test the communication does not return .

How to write code to communicate returned - restart .

#include <Wire.h> 
#include <PCF8574.h>

PCF8574 expander1;
PCF8574 expander2;

// the setup routine runs once when you press reset:
void setup() 
{
  // initialize the digital pin as an output.
  
  expander1.begin(0x38);
  expander2.begin(0x39);
  
  for (int x=0; x <= 7; x++)
    {
      int b = 30+x;
      
      expander1.pinMode(x, INPUT);
      expander1.digitalWrite(x, HIGH);
      
      expander2.pinMode(x, INPUT);
      expander2.digitalWrite(x, HIGH);
      
      pinMode(b, OUTPUT);
    }
}

// the loop routine runs over and over again forever:
void loop() 
{
  for (int i=0; i <= 7; i++)
  {
      int P1 = expander1.digitalRead(i);
      int P2 = expander2.digitalRead(i);
      int a = 30+i;
      
      if (P1 == 0 || P2 == 0)
      {
        digitalWrite(a, HIGH);
      }
  
      if (P1 == 1 && P2 == 1)
      {
        digitalWrite(a, LOW);
      }
   } 
  
}

How to write code to communicate returned - restart .

Why? Using software to work around hardware problems is rarely satisfactory. The I2C protocol is meant to be used between chips that are soldered to the same board.

the communication fails because the pinMode is not set again.

so for every read and write you must set the pinmode and then it will work probably.

might even need the begin() with every call.

do you have a link to the library (as I wrote one myself)?

Hi,

I tried today, but the effect is the same.
It does not solve the problem

I tried this code:

#include <Wire.h> 
#include <PCF8574.h>

PCF8574 expander1;
PCF8574 expander2;

// the setup routine runs once when you press reset:
void setup() 
{
  // initialize the digital pin as an output.
  
  
  for (int x=0; x <= 7; x++)
    {
      
      int k = 30+x;
      pinMode(k, OUTPUT);
      
    }
}

// the loop routine runs over and over again forever:
void loop() 
{
  expander1.begin(0x38);
  expander2.begin(0x39);
  
  for (int x=0; x <= 7; x++)
    {      
      expander1.pinMode(x, INPUT);
      expander1.digitalWrite(x, HIGH);
      
      expander2.pinMode(x, INPUT);
      expander2.digitalWrite(x, HIGH);
      
    }
  
  
  for (int i=0; i <= 7; i++)
  {
      int P1 = expander1.digitalRead(i);
      int P2 = expander2.digitalRead(i);
      int a = 30+i;
      
      if (P1 == 0 || P2 == 0)
      {
        digitalWrite(a, HIGH);
      }
  
      if (P1 == 1 && P2 == 1)
      {
        digitalWrite(a, LOW);
      }
   } 
  
}

Link to library:

PCF8574.h

jakubsan:
Hi,

I tried today, but the effect is the same.
It does not solve the problem

I tried this code:

#include <Wire.h> 

#include <PCF8574.h>

PCF8574 expander1;
PCF8574 expander2;

// the setup routine runs once when you press reset:
void setup()
{
  // initialize the digital pin as an output.
 
 
  for (int x=0; x <= 7; x++)
    {
     
      int k = 30+x;
      pinMode(k, OUTPUT);
     
    }
}

// the loop routine runs over and over again forever:
void loop()
{
  expander1.begin(0x38);
  expander2.begin(0x39);
 
  for (int x=0; x <= 7; x++)
    {     
      expander1.pinMode(x, INPUT);
      expander1.digitalWrite(x, HIGH);
     
      expander2.pinMode(x, INPUT);
      expander2.digitalWrite(x, HIGH);
     
    }
 
 
  for (int i=0; i <= 7; i++)
  {
      int P1 = expander1.digitalRead(i);
      int P2 = expander2.digitalRead(i);
      int a = 30+i;
     
      if (P1 == 0 || P2 == 0)
      {
        digitalWrite(a, HIGH);
      }
 
      if (P1 == 1 && P2 == 1)
      {
        digitalWrite(a, LOW);
      }
  }
 
}

Did this compile ?

I get a lot of compile errors

sketch_sep03a.ino:5:9: error: no matching function for call to 'PCF8574::PCF8574()'
sketch_sep03a.ino:5:9: note: candidates are:
In file included from sketch_sep03a.ino:3:0:
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:27:14: note: PCF8574::PCF8574(uint8_t)
     explicit PCF8574(const uint8_t deviceAddress);
              ^
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:27:14: note:   candidate expects 1 argument, 0 provided
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:24:7: note: PCF8574::PCF8574(const PCF8574&)
 class PCF8574
       ^
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:24:7: note:   candidate expects 1 argument, 0 provided
sketch_sep03a.ino:6:9: error: no matching function for call to 'PCF8574::PCF8574()'
sketch_sep03a.ino:6:9: note: candidates are:
In file included from sketch_sep03a.ino:3:0:
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:27:14: note: PCF8574::PCF8574(uint8_t)
     explicit PCF8574(const uint8_t deviceAddress);
              ^
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:27:14: note:   candidate expects 1 argument, 0 provided
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:24:7: note: PCF8574::PCF8574(const PCF8574&)
 class PCF8574
       ^
C:\Users\Rob\Desktop\WORK\Arduino\libraries\PCF8574/PCF8574.h:24:7: note:   candidate expects 1 argument, 0 provided
sketch_sep03a.ino: In function 'void loop()':
sketch_sep03a.ino:26:13: error: 'class PCF8574' has no member named 'begin'
sketch_sep03a.ino:27:13: error: 'class PCF8574' has no member named 'begin'
sketch_sep03a.ino:31:17: error: 'class PCF8574' has no member named 'pinMode'
sketch_sep03a.ino:32:17: error: 'class PCF8574' has no member named 'digitalWrite'
sketch_sep03a.ino:34:17: error: 'class PCF8574' has no member named 'pinMode'
sketch_sep03a.ino:35:17: error: 'class PCF8574' has no member named 'digitalWrite'
sketch_sep03a.ino:42:26: error: 'class PCF8574' has no member named 'digitalRead'
sketch_sep03a.ino:43:26: error: 'class PCF8574' has no member named 'digitalRead'

You can only use the functions as defined in the PCF8574.h file !

Latest version of the library - https://github.com/RobTillaart/Arduino/tree/master/libraries/PCF8574 -

I'm sorry , I made a mistake

I have this version:

https://github.com/skywodd/pcf8574_arduino_library

And it is compiles.

Hop, Hop ...

I's there anybody ???

I repeat the question: how to restart the I2C bus ?