I2C connection between arduino due and arduino mega 2560

I am trying to interface a mega with a due per I2C. I have done:

  • grounds are shared.
  • SCL/SDA on mega 2560 (not 20/21) are connected to SCL1/SDA1 on due. The connection is with 4.7k Ohm resistors (both SCL and SDA) is connected to 3.3 V on both arduinos. Note: SCL/SDA of due (20/21) are connected to an imu bno055 which is working fine.

on mega I do:

#include <Time.h>
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <EasyTransferI2C.h>

EasyTransferI2C ET_GPS_data; 
struct SEND_DATA_STRUCTURE{
  double GPS_la;
  double GPS_lo;
  double GPS_alt;
};

SEND_DATA_STRUCTURE GPS_data;
#define I2C_SLAVE_ADDRESS 9

SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);

double GPS_la = 0.0;
double GPS_lo = 0.0;
double GPS_alt = 0.0;
#define GPS_Sampling_Time_ms 20
unsigned long currentMillis_GPS = 0;
unsigned long previousMillis_GPS = 0;

void getGPS();

void setup() {
  mySerial.begin(9600);
  Wire.begin();

  // ******** Initialize sim808 module *************
  while(!sim808.init())
  {
     delay(1000);
  }
  delay(3000);

  if(sim808.attachGPS()){
     //Serial.println("Open the GPS power success");
  }else{
     //Serial.println("Open the GPS power failure");
  }
  ET_GPS_data.begin(details(GPS_data), &Wire);

}

void loop() {
  currentMillis_GPS = millis();
  if (currentMillis_GPS - previousMillis_GPS > GPS_Sampling_Time_ms) {
    getGPS();
    sim808.detachGPS();
    previousMillis_GPS = currentMillis_GPS;
    ET_GPS_data.sendData(I2C_SLAVE_ADDRESS);
  }
  GPS_data.GPS_la = GPS_la;
  GPS_data.GPS_lo = GPS_lo;
  GPS_data.GPS_alt = GPS_alt;
}

void getGPS(){ 
  while(!sim808.attachGPS()){}
  delay(80);
    
  while(!sim808.getGPS()){}
  GPS_la = sim808.GPSdata.lat;
  GPS_lo = sim808.GPSdata.lon;
  GPS_alt = sim808.GPSdata.altitude;
}

on due:

#include <Arduino.h>
#include <Wire.h>
#include <EasyTransferI2C.h>
EasyTransferI2C ET_GPS_data; 
struct RECEIVE_DATA_STRUCTURE{
  double GPS_la;
  double GPS_lo;
  double GPS_alt;
};
RECEIVE_DATA_STRUCTURE GPS_data;
double GPS_la = 0.1;
double GPS_lo = 0.1;
double GPS_alt = 0.1;
#define GPS_Sampling_Time_ms 100
unsigned long currentMillis_GPS = 0;
unsigned long previousMillis_GPS = 0;

void setup() {
 Serial.begin(57600);
 Wire1.begin(I2C_SLAVE_ADDRESS);
 ET_GPS_data.begin(details(GPS_data), &Wire1);
 Wire1.onReceive(receive);
}

void loop() {
if(ET_GPS_data.receiveData()){
    GPS_la = GPS_data.GPS_la;
    GPS_lo = GPS_data.GPS_lo;
    GPS_alt = GPS_data.GPS_alt;
    Serial.println(GPS_alt);
  }
}

without the i2c, both of the boards work fine. What am I doing wrong?

One of them needs to be "Master" and all others be "Slaves".
2 Slaves will not communicate.

Wire.h library don’t allow you to run both I2C buses at the same time with the DUE, you have to choose one of them. To use both I2C buses at the same time, direct register programming is needed.

Note: There is neither builtin glitch filtering nor slope control on Sam3x I2C buses.

The Arduino Mega 2560 has three spare Serial ports.
The Arduino Due has three spare Serial ports.
I don't see the problem :o You need to fix the different voltage levels between them but then they can communicate with each other via the Serial port.

Use the I2C bus for sensors. Only when you are looking for trouble, then you can use the I2C bus to communicate between two Arduino boards.

The DUE has 5 hardware serial :slight_smile:

Railroader:
One of them needs to be "Master" and all others be "Slaves".
2 Slaves will not communicate.

yeah, I know that and I thought I made the mega 2560 the master by sending the data while the due is waiting to receive the data. Since I am using GitHub - madsci1016/Arduino-EasyTransfer: An Easy way to Transfer data between Arduinos and the guidelines there do it this way. Can you be more specific about what I have done wrong?

I have to be vague but somewhere I think I picked up a difficulty here. I can't tell if it was to run the controller as slave, only master worked, but I might be wrong.

ard_newbie:
Wire.h library don't allow you to run both I2C buses at the same time with the DUE, you have to choose one of them. To use both I2C buses at the same time, direct register programming is needed.

Note: There is neither builtin glitch filtering nor slope control on Sam3x I2C buses.

yeah, I didn't pay attention to this. Thanks

Koepel:
The Arduino Mega 2560 has three spare Serial ports.
The Arduino Due has three spare Serial ports.
I don't see the problem :o You need to fix the different voltage levels between them but then they can communicate with each other via the Serial port.

Use the I2C bus for sensors. Only when you are looking for trouble, then you can use the I2C bus to communicate between two Arduino boards.

I went with this, getting rid of I2C. thanks.