I tried to set up an I2C conversation between ab ATTiny85 (8MHz) and an Arduino Uno. However I always get a 255 response from my I2C slave. I am using Arduino 1.8.5.
What am I doing wrong?
ATTiny85-Slave Code:
#include "TinyWireS.h" // wrapper class for I2C slave
routines
#define I2C_SLAVE_ADDR 0x26 // i2c slave address (38)
byte t=10;
void setup() {
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
TinyWireS.onRequest(requestEvent);
}
void loop() {
}
void requestEvent() {
TinyWireS.send(t);
}
Arduino Uno-Master Code:
#include <Wire.h>
#define I2C_SLAVE_ADDR 0x26 // i2c slave address (38)
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
byte num;
// read 1 byte, from address 0x26
Wire.requestFrom(I2C_SLAVE_ADDR, 1);
while(Wire.available()) {
num = Wire.read();
}
Serial.print("num = ");
Serial.println(num,DEC);
}
-
Where is the TinyWireS_stop_check ? The value of 255 is when the Wire.read() returns -1 which means that you did not receive valid data from the slave. You could run a i2c scanner and keep it running on the Uno, while trying to fix the wiring or the slave sketch. If that is working, then try to receive or send data.Jot– Jot2018年01月10日 23:21:29 +00:00Commented Jan 10, 2018 at 23:21
-
@Jot - can you explain why Wire.read() would yield -1 when wire.available() is nonzero?Chris Stratton– Chris Stratton2018年01月11日 02:35:15 +00:00Commented Jan 11, 2018 at 2:35
-
@ChrisStratton you are right, when wire.available shows that there is data, then -1 is not returned. I didn't think that through. I was trying to tell it in a common way, because there is an other possibility: when the slave behaves badly, perhaps the slave aknowledges with a ack to its address, but fails to send data so the sda stays high resulting in 255. Maybe there are even more possibilities when the slave does weird things.Jot– Jot2018年01月11日 02:51:00 +00:00Commented Jan 11, 2018 at 2:51
-
1Seems that am not the only one. forum.pjrc.com/threads/… ...strange :-(Wolfgang– Wolfgang2018年01月11日 12:08:54 +00:00Commented Jan 11, 2018 at 12:08
1 Answer 1
It may seem that the current commit of January 2018 from the TinyWire library is corrupt. Try using an older version from 2017, for example this one: https://github.com/rambo/TinyWire/tr...72a13504bbfc4e
This solves the problem for me as well.
Source: Teensy 3.5 - I2C - ATTiny85-20PU (TinyWireS.OnRequest not triggering!) - Post #11