I2C communication between arduinos

As per title.

I know I can gather data from a slave arduino on an I2C, but right now the issue is that I need to both gather and write data to the slave device

For example: At startup, when the communication is established the master verifies that the slave is ready and unlocked. if this is not true the user is asked to insert a password. The master does not know what the password is, just relays it to the slave. After this the slave is checked again to see if the correct password has been entered. Therefore, the master has to read data from the slave, as in this example TechBitar - Home, yet it also needs to write data.

Any advice greatly appreciated.

The slave should have Wire.onRequest() and Wire.onReceive().

In the slave, the onRequest handler returns data to the master, and the onReceive should copy the incoming data to a buffer.

That has an example of a master acting as a slave as well. Scroll down to "Communicating with other devices".

Thank you both for your replies.

I believe the option that would be most suited would be to use both WireOnRequest and Wire OnReceive. Therefore the slave waits for a command from the master, points to the correct data location and on next master request sends the correct data. Is this possible at all or am I limited to either one or the other!?

Regards

I found something that does exactly what i need. For reference here is the link:

Many I2C devices (sensors, memory) have internal registers. To read from a register, first the register-address has to be written and after that the data can be retrieved. What you describe is like that. That is one of the options to communicate between Arduinos when the data is larger or depends on a command.

The other option is that the master tells the slave to send its data. The slave becomes a master (temporarily), and sends data to the master. The master receives it with its own Wire.onReceive().

Both options have advantages and disadvantages. I'm using the first option myself. It is safer but slows down the master a little.
The code in the link is the second option.

However, that code in the link is terribly wrong :stuck_out_tongue_closed_eyes:
There are a few severe errors in the slave receiveEvent():

  • reading when nothing is available is very bad. The I2C session has ended, and Wire.available() is the same as 'howMany' parameter.
  • It is not possible to use the Serial library inside that interrupt routine.
  • I think it is wrong to start a new I2C session there.

The normal way is to set a flag, or fill a buffer. In the loop() check for the flag and do something with the data.

Peter_n:
I'm using the first option myself.

Would you mind sharing your code peter?

My sketch is full of EEPROM use and many #ifdef and structures and unions, and the Master code is now 15 files.
I don't have a "show case" for it. The next sketch is a stripped down version, but not a working sketch.

Slave code

byte buffer[10];
byte command = 0;
boolean flagRead = false;
boolean flagCommand = false;

void setup() {
  Wire.begin(3);

  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void loop() {
  if( flagRead)
  {
    // The Master has read the data and all is well.
    flagRead = false;
  }

  if( flagCommand) {
    // A command, we have to do something.
    // the variable 'command' tells what to do
    ...

    flagCommand = false;
  }
}


void receiveEvent( int howMany) {
  // no need for Wire.available(), it is the same as 'howMany'
  if (howMany >= 1) {
    command = Wire.read();
    flagCommand = true;
  }
}

void requestEvent(void) {
  Wire.write( (uint8_t *) buffer, sizeof(buffer));
  flagRead = true;
}

Part of Master code

  int command;
  byte buffer[10];

  command = ...    // some command

  Wire.beginTransmission(3);
  Wire.write(command);
  Wire.endTransmission();
  delay(1);         // is needed for Wire library sync ?

  // The slave might need some time to perform the command.
  delay(10);

  // request data
  Wire.requestFrom(3, sizeof(buffer));

  // The available bytes should be the same as the number of bytes
  // that was requested. Something is wrong if it is not.

  int n = Wire.available();
  if(n==sizeof(buffer)) {
    for(int i=0; i<n; i++) {
      buffer[i] = Wire.read();
    }
  }

Thanks for sharing Peter this is more than enough.

  // request data
  Wire.requestFrom(3, sizeof(buffer));

  // The available bytes should be the same as the number of bytes
  // that was requested. Something is wrong if it is not.

  int n = Wire.available();
  if(n==sizeof(buffer)) {

Wire.requestFrom returns the number of bytes it received, so this can be written as:

  // request data
  if (Wire.requestFrom(3, sizeof(buffer)) == sizeof (buffer) {
...

You are right XD
The Wire.requestFrom() puts the received bytes into a buffer and returns the number of bytes in the buffer.
The Wire.available() check how many bytes are in the buffer. That is the same ofcourse.