Arduino as I2C master to an IMU and I2C slave to second Arduino

I want my Arduino Mega to output certain data bits when a certain I2C address is called - so I guess I want it to be a slave to either my computer or to a second Arduino. It is taking in data from a sensor so it is already an I2C master to an IMU.

Is this possible? Or am I thinking about this the wrong way, sorry I am new to I2C protocol and still trying to understand what I can do.

Thanks!

Your line of thinking is perfectly alright with a little bit off-track. I am converting you idea into a block diagram which will certainly help you to do the I2C Bus coding.
megaI2CPullup.png

1. UNO is Master; MEGA is Slave-1 with 7-bit (0x08 = 0b0001000) I2C Bus address; MPU6050 is Slave-2 with 7-bit (0x69 = 0b1101001) I2C Bus address. MEGA is not working as the Master of Slave-2. This is possible in the Multi-master I2C Network which is not being currently supported by Arduino.

2. The I2C Bus is created by including the following lines at the appropriate place of the sketch.

#include<Wire.h>
Wire.begin();

3. Before exchanging data with Slave-1 (for example), the Master checks the presence of Slave-1 by executing the following codes in the setup() function.

Wire.beginTransmission(slave1Address); //0x08
byte busStatus = Wire.endTransmission();
if(busStatus !=0x00)
{
   Serial.print("Slave is absent....!");
   while(1); //wait for ever
}

4. In I2C Bus, the data exchange is always 1-byte at a time. The Master executes the following codes of lines to transfer data byte 0x35 (for example) to Slave-1.

Wire.beginTransmission(slave1Address);
Wire.write(0x35); //data byte 0x35
Wire.endTransmission();

5. When the data byte(s) has reached to the Slave and then has entered into the FIFO, the Slave goes to the void receiveEvent(int howMany){} sub-program (an interrupt context; no Serial.print()/write() commands are recommended). The user now can read the data byte(s) from the FIFO and save them into variables. For the said sub-program to become effective, the following declaration should be made in the setup() function of Slave-1.
Wire.onReceive(receiveEvent);.

6. ................................

megaI2CPullup.png

Block diagram is great thank you! Really helps me visualise how to get started - just a few quick questions...

  • Could I control both Arduinos from the same PC?

  • If the Mega is slave address 0x08 - Can I access the different addresses on the IMU or is it more - if UNO
    sends a certain command

  1. Read info from IMU
  2. Send back bytes received from address corresponding to the command

And just have different conditions for the different sensors

Thanks again for help - I just want to get the idea right in my head before I try to code it!

RachelDunwoody:

  • Could I control both Arduinos from the same PC?

Yes! You can open two Serial Monitors SM1 and SM2 in the desktop of one PC. In the diagram of Post#1, they are actually one PC though 2 PCs are shown for clarity.

  • If the Mega is slave address 0x08 - Can I access the different addresses on the IMU or is it more - if UNO sends a certain command

The MEGA will never conflict with the register addresses of the IMU. The IMU's registers are accessed through the gate of IMU's Bus address.