I2C bi directionnal

Hi, I have the exacly the same system as in this link :

But I need to add a 3rd card, a Due.
It seems like it is more of a 2 slave with the
Wire.begin(0);
Wire.begin(1);

I need all 3 cards to performe critical speed opérations, so I need to send data without the onRequest, that a master would send. Is I2C the best for a few bytes data, sent really fast, beetween 2 Uno A4 A5 and a Due 20 21 ?
I use a logic voltage converter 5 to 3.3 compatible with I2C, and all grounds are connected.
Nothing works even with only a Uno and the Due

That link is full of beginner mistakes. Only beginners try a multi-master I2C bus, because there are many pitfalls and it will only bring trouble.

Can you tell what your project is about ?
Is it possible to do your project with a single (fast) Arduino board ? Maybe your sketch is not suitable yet to do multiple things at the same time, but that can be fixed.

Addresses below 8 are reserved, shall never be used for slaves.

I'd give a multi-master system a try. Details depend on your required communication.

Then you have to fix hardware bugs first. Please show a circuit diagram, no Fritzing nor photos.

The slave continuously checks the digital ports in the void loop, and when a change is detected, and sends a structure with all the data, and the slave must read I2C for commands:

#define SLAVE_MEGA 6
struct CommsStruct
{
    int fonctionSerial; 
    int composantName;  
    int composantId;    
    float valA;
    float valB;        
};



void display(int i)
{
    Wire.beginTransmission(0);

    CommsStruct txData;
    txData.fonctionSerial = int(0);
    txData.composantName = int(1);
    txData.composantId = int((i + 2) / 2);
    txData.valA = float(0);
    txData.valB = float(valeur_lisse[i]);

    Wire.write((byte *)&txData, sizeof(txData));
    Wire.endTransmission(true);
}

void receiveEvent(int howMany)
{
CommsStruct rxData;
    Wire.readBytes((byte *)&rxData, sizeof(rxData));
}

void setup(){
    Wire.begin(SLAVE_MEGA);
    Wire.onReceive(receiveEvent);    
}

It works, I don't need Wire.requestFrom, because it's my slave that knows when to send data.
My master also needs to send data to the slave, so I use the same logic of beginTransmission and change the address

Wire.begin(0);
    Wire.onReceive(receiveEvent);
    TWBR = 12;


void receiveEvent(int howMany)
{
CommsStruct rxData;
    Wire.readBytes((byte *)&rxData, sizeof(rxData));
}

    Wire.beginTransmission(SLAVE_MEGA);
    Wire.write((byte *)&txData, sizeof(txData));
    Wire.endTransmission(true);

I know I need to use >8 addresses, 25 or 42 should be good.

This all works, but I need to add a 3rd board, a due, which can read I2C and send data when it wants and not when a master calls Wire.requestFrom.

Maybe I2C is not what I need, because all cards needs to send data when they want

Please keep the explanations simple, I'm a beginner

Each I2C node can become a master by calling Wire.beginTransmission(). If that works it can send or request whatever is required.

In my opinion, a multi-Master I2C bus with Arduino boards and the Wire library is a fairy tale.

Can you tell me what would you use to send data beetween 3 cards
in both ways ? Data is no more than a few byte but really fast

Umm. Serial? How far apart? Serial can run much higher than 200kbaud (I've seen references to 2Mbaud, but haven't tried that), and if you need faster than that it's possible you've got the wrong processor in mind anyways; Due and Uno are a tad long-in-the-tooth at this point.
C

Sorry I see many future problems without assuming any hardware problems because of line length and capacitance. I2C is not really fast. Take a look at CAN, much easier to use and faster. CoryJFowler has posted a great library that comes with a simple send and receive examples. These can be combined into one sketch quite easily. The MPC2515 (CAN) modules are not very expensive. You can put 20 or more nodes on it if you want and wire a few hundred meters/yards is no problem.

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