Reconnect i2c communication while disconnect

Hello,
I'm working on a simple arduino project which generate square electrical signal.
Actually all is working well, except the i2c communication which sometime disconnect. But as the project must to be autonomous, I need an auto-reconnect i2c communication, but I'm not really sure how to do that.. wire.begin into the loop section ?? create an exception ?
Thank you for advicing me.

Here is the functionning code :

#include <Wire.h>

void setup()
{
  Wire.begin(8);
  Wire.onReceive(reveiveEvent);
  Serial.begin(2000000);
}

void loop()
{
  delay(500)
}

void receiveEvent(int howMany)
{
  if (howMany > 0)
   {
      uint16_t r1 = Wire.read();
      uint16_t r2 = Wire.read();
      uint16_t r3 = Wire.read();
      uint16_t valeur = r2 | r3 <<8;
      Serial.print("valeur= ");
      Serial.println(valeur);
   }
}

Your sketch is moderated as follows, which you may try:

volatile bool flag = false;
byte myData[10];

void loop()
{
   if(flag == true)
   {
         //process data and print
         flag = false;
   }
   delay(500);
}

void receiveEvent(int howMany)
{
  for(int i = 0; i< howMany; i++)
   {
      myData[i] = Wire.read();
   }
   flag = true;
}

What is going on with your I2C bus ? it is not a fault tolerant bus.
Can you tell more about: length, wires, cables, pullup resistors, other I2C devices, grounding.

The i2c connection is quite simple :

The sender is a RAspberry ( SDA / SCL )
The receiver is an Arduino ( SDA / SCL )
Raspberry and ARduino share the same ground.
All wires are well connected. Raspberry send data, and Arduino receives correctly data, but at one moment Arduino stop to receive data. It is not at the same time. Sometime the system works correctly during 1 minute, sometime the system stop at 10 seconds... I have no resistors, no other i2c devices.

Thank you for help.

We need to see the full sketch of the Slave. Some libraries turn off the interrupts (Neopixel, SoftwareWire, OneWire, DHT and others).

If you don't have pullup resistors than you don't have a I2C bus. Your data is transferred by magic at the moment :magic_wand:

Actually, my connection is like that :

but without resistance... I never saw any resistance in schematic circuit from arduino tutorial..

The Arduino Mega has onboard pullup resistors of 10k to 5V. In the green circle:
afbeelding

The Raspberry Pi has no 5V tolerant pins. Since the SDA and SCL are lifted to 5V (via the 10k onboard pullup resistor) a current leaks into the Raspberry Pi pins. A I2C level shifter prevents that.
In your picture, there are only the SDA and SCL lines, but the GND wire is the most important wire of the I2C bus (it really is).

If you connect a USB cable from the Mega board into the USB connector of the Raspberry Pi, then you can use the Serial bus and there are no problems by the I2C bus.

ok, if I understand, I could replace my blue and red wire using an USB cable ?!

BUt effectively the GND wire is not in the scheme but is really connected on my boards.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advice on your project.

That's high speed. Are You sure all I2C device handles that? You use "Arduino".... Which one?

What does Serial have to do with I2C?

Good question but still a remarkable speed.

Yes maybe it is too speed. However I didn't have problem when I use Arduino + Arduino with Serial.begin(2000000).
But maybe it's too speed for Arduino + Raspberry.
I will test with lower speed..

Hello, with a 9600 bds speed, result was the same..
So what I understand is i2c communication between arduino and arduino is very good.
I2C communication between raspberry and arduino isn't very reliable... So I test the UART communication between raspberry and arduino and result is very good, and programming easy :grinning:

So many thanks for helping me. Bye.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.