Arduino to Arduino I2C, getting 0xFF

Hello, I’m trying to simulate an I2C part using an arduino, so I set up this little test case to try to pass information from an Arduino as a slave on the I2C bus.

Here’s the issue:
I should be receiving 0x68, 0x65.
I am receiving: 0x65, 0xFF.

Any ideas?

I have a common ground between the boards.

Master Sketch:

#include <Wire.h>

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(2,2 );    // request 2 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    byte b = Wire.receive();
    Serial.print(b, HEX);         // print the character
  }
  delay(1000);
}

Slave Sketch

#include <Wire.h>

void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{ 
  byte b;
  b = 0x68;
  Wire.send(b);
  b = 0x65;
  Wire.send(b);

}

and the default sketch works just fine....

There is also Wire.send(data, quantity)
Its not really documented
http://arduino.cc/en/Reference/WireSend
I never made a slave but give it a try:

char [] answer={0x68,0x65};
Wire.send(answer,2);

Eberhard

Are you using version 0017?

You say you have a common ground between the two boards. What about pullup resistors, length of wires between the boards? Are both boards 5V?

Have you tried requesting/returning three bytes to see if the issue is consistent (first byte lost)?

Wayoda,

I tried it....changed declaration to:

char answer[2]={0x68,0x65};

Arduino says invalid conversion from char* to unit8_t;

BenF, The bus isn't the issue, the default sketch works just fine, I just can't seem to send and receive bytes correctly.

I rewrote the slave code to send 3 bytes:

  b = 0x68;
  Wire.send(b);
  b = 0x65;
  Wire.send(b);
  b = 0x62;
  Wire.send(b);

What I get back is: 62FFFF

So it looks like I'm receiving the last byte first, and then everything else is 1's.

Oh, and I am using 017, but I had the same issues in 016

Wayoda, THANK YOU.

I changed the code to uint8_t array, and it works!

Here’s the new sending code that works.

#include <Wire.h>

void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{ 
  uint8_t b[3] = {0x68, 0x65, 0x62};
  Wire.send(b,3);
  /* This code does not work, but should (in my opinion!)
  b = 0x68;
  Wire.send(b);
  b = 0x65;
  Wire.send(b);
  b = 0x62;
  */

}

Hi, sorry about the char issue , but I'm glad it works now. I looked at the sources for the Wire lib and found out , you have to use the send(data, length) method if you want to return more than 1 byte in a request to a I²C slave. The way it is implemented is correct, but as I wrote earlier it is very badly documented. I will file a bug report for this...

Thanks for keeping us updated!

Eberhard

This code does not work, but should (in my opinion!)

Looking at the source I can see why, but as you say - it is far from obvious.

BenF,

I'm not trying to slam anyone, just adding a running commentary in my code. I should have removed it before posting.

I'd love to write up my experiences and document them...if anyone knows the right person to update the Arduino wire library documentation.

I'd love to write up my experiences and document them...if anyone knows the right person to update the Arduino wire library documentation.

There are only a few people who have permissions to update the docs on the reference-section of the site. I filed a request for this, which is about all one can do.

http://code.google.com/p/arduino/issues/detail?id=180 Eberhard