Due to Mega 2560 communication: No Luck

I am collecting data using a Due that need to be transmitted to a Mega for filing. They share a power supply using 5V and GND jumpers that connect both boards and a USB cable to the Mega. Mega SDL and SDA jumpers (with 3K pullup connections) are connected to the Due SDL and SDA connectors near the reset button.

There are two sets of SDL and SDA connectors on the Due, how do I select which one I am using?

The code I use on the Due is:

//Arduino Due, Slave Device for 2 channel data collection.

#include <Wire.h>
#include "Arduino.h"

void setup() {
  Wire.begin(2);// join i2c bus, address for this unit is #2 as a slave (#1 is master)  
  Serial.begin(9600);
}

unsigned long NData=0; 
String logd(150);
int ils ; 
int i ; 
char x;

void loop() {
    NData=NData+1;
    logd="";  
  logd +=NData;
  logd +=", ";
  logd +=analogRead(A0);
  logd +=", ";
 
    ils= logd.length(); // This is the length of the string
    Wire.beginTransmission(1); // transmit to device #1 (master, stores data) 
    Wire.write('

There is no response on the Mega receiving end what so ever.Some where I must be making a major mistake. Any ideas anyone? Thanks!

The Mega code is below:

//Master Device used for Data Collection with an Arduino Mega 2560

#include <Wire.h>
#include "Arduino.h"

/*
  Read measurements from other Arduino slave devices using  I2C/TWI procedures.
  This Master is device #1
  Data is collected by device #2 (Arduino Due) and transmitted to this unit.
 */

void setup() {
  Wire.begin(1);// join i2c bus as Master,address= #1 (used, but not needed)
  Wire.onReceive(ReceiveData); // register event 
  // initialize serial communication:
  Serial.begin(9600);
}

unsigned long NData=0; 
String logd="";// what gets filed.
int i ; //Length of the measurement string
char x;

void loop()
{
  delay(100);
}

// function that executes whenever data is received from slave
// this function is registered as an event, see setup()
void ReceiveData(int howMany)
{
  logd="";
  // Start the message at the $ sign as this is the start byte.
      if(Wire.read() == '

);           // sends recognizable start byte
   for(i=0;i< ils;++i) {
     char x = logd[i];
     Wire.write(x);
   }
   Wire.endTransmission('#');  // sends recognizable stop byte
}


There is no response on the Mega receiving end what so ever.Some where I must be making a major mistake. Any ideas anyone? Thanks!

The Mega code is below:

§DISCOURSE_HOISTED_CODE_1§


){
    for(i=0; i<100; i++){
     logd[i] = Wire.read();
  // check if this message ends and we need to store the line:     
     if(logd[i]=='#')
     break;
    }
    Serial.print("Transmitted: "); 
    Serial.print(logd); 
    Serial.println(); 
    }
}

);           // sends recognizable start byte
   for(i=0;i< ils;++i) {
     char x = logd[i];
     Wire.write(x);
   }
   Wire.endTransmission('#');  // sends recognizable stop byte
}


There is no response on the Mega receiving end what so ever.Some where I must be making a major mistake. Any ideas anyone? Thanks!

The Mega code is below:

§DISCOURSE_HOISTED_CODE_1§

There are two sets of SDL and SDA connectors on the Due, how do I select which one I am using?

You have only one set labeled SDA and SCL, the other set is labeled SDA1 and SCL1. SDA and SCL is represented by the Wire object, while SDA1 and SCL1 is represented by the Wire1 object.

Regarding the comments in your code you misunderstood the meaning of master and slave on the I2C bus. In your code the Due is the master and the Mega is the slave.

Take care that the I2C pullups go to 3V3 and NOT 5V, else you may damage your Due.

For the I2C communication there is no need for a start "*" or end "#" marker in the data stream. You have a start and end condition in the electrical protocol of the bus, so the slave always know when a "packet" of data arrives and where it starts.

And take care, the sending (and reception) buffer of the Wire library is only 32 bytes, so don't write more than 32 bytes (characters) to the Wire object between beginTransmission() and endTransmission().

The ReceiveData() function is used as an interrupt handler in your code. You must not use Serial.print()s in an interrupt handler, that can produce a dead lock.

Pylon,

THANKS! I got a lot more advice than I could hope for.

MJMes