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);
}
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...
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.