Problem with multiple arduino and I2C sensors

Hi Everyone,

I've spent the all past week trying to communicate with 3 Uno and I2C sensors. But I haven't find any solution, that why I post here my problem.

Here is the setup I want :

1 Uno (Master) with SD breakout (SPI) and Xbee
1 Uno (Slave) with different I2C sensors such as MPU-6050, BMP180, .... (and some analogue sensors)
1 Uno (Slave) with temperature sensors (Analogue)

And here is the setup I've tried :

1 Uno (Master)
1 Uno (Slave) with different I2C sensors such as MPU-6050, BMP180, ....

When I tried to send data from the Slave to the Master, I have only two values (0 or 255).

I'm asking myself if this setup is possible or if I can only have one Slave (previously the master) and two Master (the 2 slaves) ?

Any ideas ?

Thank you a lot !

Happy New Year btw :slight_smile:

You don't explain how you communicate between master and slave. I2C?

Posting (a simplified version of) your codes might also help us to help you.

Yes I'm using I2C.

I'll do that when I have my computer back :wink:

Here is the slave code :

//SLAVE
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

MPU6050 accelgyro;
#define Slave 10                    // define slave address as 10

int16_t ax, ay, az;
int16_t gx, gy, gz;

  char myData[6]= "#12";           // declare string
  byte AX;                       // declare byte
  /*byte AY;
  byte AZ;
  byte GX;
  byte GY;
  byte GZ;
*/
void setup() {
    Wire.begin(Slave);
        
    Serial.begin(115200);

    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  Wire.onRequest(requestEvent); // register event


}

void loop() {
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
       ax= AX; 
     Serial.print(ax);   Serial.print(" ");
     Serial.print(AX);   Serial.print(" ");
     /*Serial.print(ay);   Serial.print(" ");
     Serial.print(az);   Serial.print(" ");
     Serial.print(gx);   Serial.print(" ");
     Serial.print(gy);   Serial.print(" ");
     Serial.print(gz);   Serial.print(" ");*/
     Serial.println();
}

void requestEvent(){
  Wire.write(ax);
  Serial.println(ax);
  }

and the Master :

//MASTER
#include <Wire.h>

#define Slave 10                    // define slave address as 10

  char myData[6]= "#12";           // declare string
  byte AX;                       // declare byte
  byte AY;
  byte AZ;
  byte GX;
  byte GY;
  byte GZ;

 void setup() {
   Serial.begin(9600);
   Wire.begin();                   // set as master
}
 
 void loop(){
   delay(25);                      // delay 25ms             
   
   Wire.requestFrom(Slave, 6);      // Request 6 bytes from NANO

   while(Wire.available())
     {        // wait for byte, and do this while recieving
       AX = int(Wire.read());     // Recieving data as a string, then converts to int.
       /*AY = int(Wire.read());
       AZ = int(Wire.read());
       GX = int(Wire.read());
       GY = int(Wire.read());
       GZ = int(Wire.read());*/
     }
   
   
     Serial.print(AX);   Serial.print(" ");
    /* Serial.print(AY);   Serial.print(" ");
     Serial.print(AZ);   Serial.print(" ");
     Serial.print(GX);   Serial.print(" ");
     Serial.print(GY);   Serial.print(" ");
     Serial.print(GZ);   Serial.print(" ");*/
   Serial.println();

   delay(25);
}

1 Uno (Master)
1 Uno (Slave) with different I2C sensors such as MPU-6050, BMP180, ....

Is the Uno slave and the sensors on the same I2C bus?

If so then the master can talk directly to the sensors can it not?

Have you got external pull up resistors fitted and have you got a common ground between the Arduinos.

Just a quick look at the code you posted and it does not look right. You seen to be requesting 4 bytes but never sending anything like that. Also you seem to have master and slave mixed up in the headings / code.

Yes They are on the same bus but I need to have different board. The master in my first setup is only for transmitting data but the second need to do some calculations.

Yes, I've put some pullup resistors (10kO or something similar) and the common ground and 5V between them.

Indeed I've mixed up slave and master header :s

Yes They are on the same bus but I need to have different board.

Not sure I understand that.

The master in my first setup is only for transmitting data but the second need to do some calculations.

So the Uno has to talk to the bus as well as the Mega? That means you need a multimaster bus.

You still have the problem with bytes requested / sent.