Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created February 18, 2015 15:38
Show Gist options
  • Save rwaldron/616e95b7c2637ebd21c5 to your computer and use it in GitHub Desktop.
Save rwaldron/616e95b7c2637ebd21c5 to your computer and use it in GitHub Desktop.
I2C slave: Wire.write() sending only one byte?
#include <Wire.h>
byte i2cdata[4];
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.requestFrom(4, 4);
int a = Wire.available();
Serial.print("Available: ");
Serial.println(a);
// Both of these produce the same result
// for (int i = 0; i < a; i++) {
// Serial.print(Wire.read());
// Serial.print(" ");
// }
while (Wire.available()) {
Serial.print(Wire.read());
Serial.print(" ");
}
Serial.println();
memset(&i2cdata, 0, 4);
delay(500);
}
#include <Wire.h>
int address = 4;
void setup() {
Wire.begin(address);
Wire.onRequest(onRequest);
}
void loop() {}
void onRequest() {
byte buffer[4] = {1, 2, 3, 4};
Wire.write(buffer, 4);
}
/*
When this sketch is running on the slave, the output of the master is:
Available: 4
1 2 3 4
...
*/
#include <Wire.h>
int address = 4;
void setup() {
Wire.begin(address);
Wire.onRequest(onRequest);
}
void loop() {}
void onRequest() {
for (int i = 1; i < 5; i++) {
Wire.write(i);
}
}
/*
When this sketch is running on the slave, the output of the master is:
Available: 4
4 255 255 255
...
*/
@rwaldron
Copy link
Author

Thanks for the details :)

@ajfisher
Copy link

Sounds like TinyWireS probably needs an update to bring things in line with this behaviour. I noticed there were a lot of branches doing different things so there's been a lot of fragmentation.

@soundanalogous
Copy link

@ajfisher have you tried this I2C library that Adafruit made for the Trinket? https://github.com/adafruit/TinyWireM

@soundanalogous
Copy link

actually it looks like TinyWireM only implements I2C master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment