ATtiny 85 back to back communication with I2C

Hi Guys,

I am working on a project that requires to have 2 ATTiny 85 linked with an I2C bus.

One will be the master and the other will be the slave. The slave is connected to a light sensor and will relay the value to the master via the I2C.

Both of my ATtiny are running with the arduino-tiny core files and are using the I2C libs from BroHogan (http://arduino.cc/playground/Code/USIi2c)

The problem is that most of the time, the master returns error code 0x01 at endTransmission(). According to source file, error code 1 is no ACK on address:

#define USI_TWI_NO_ACK_ON_ADDRESS   0x01  // The slave did not acknowledge  the address

However, on some occasion, the slave is receiving command the master sends (therefore ACKS the address) and the slave return the value I need. This returned value is sometime interpreted correctly by the Master, and sometime it is not.

I put the pullups (various values : 3.9k,4.7k,5.6k,10k,…) on SDA and SCL but no luck.
If I set the Arduino Uno as the master, the problem is more or less the same (The Wire library is stuck on the beginTransmission() line)

All of these make me think of either a synchronization issue, but I do not know how to solve it.

Do you have any ideas ?

Thanks

Code of the MASTER

#include <TinyWireM.h>                  // I2C Master lib for ATTinys which use USI

#define SENSOR_ADDR   0x20              // 7 bit I2C address for DS1621 temperature sensor
#define LED_PIN         4              // ATtiny Pin 2


void setup(){
  pinMode(LED_PIN,OUTPUT);             // for general DEBUG use
  blinkLed(LED_PIN,5);                    // show it's alive
  TinyWireM.begin();      // init I2C Master mode
  delay(5000);
}

int addr = SENSOR_ADDR; 
void loop(){

  TinyWireM.beginTransmission(addr);
  TinyWireM.send(0x17);        //0x17 is the code for "sending back sensor value"
  int r = TinyWireM.endTransmission();
   blinkLedSlow(LED_PIN,r);
   if(r != 1) {
     r = TinyWireM.requestFrom(addr,1); 
    blinkLedSlow(LED_PIN,r);
    delay(10);
    while( TinyWireM.available() != 0){
      char valueLight = TinyWireM.receive();
      if(valueLight == 10)  // Reply received and is correct
      {
          blinkLed(LED_PIN,3); 
      }
      else steady(LED_PIN,3);
    }
    //delay(1000);  
}
delay(300);

Code for the SLAVE

#include "TinyWireS.h"                  // wrapper class for I2C slave routines

#define I2C_SLAVE_ADDR  0x20            // i2c slave address (38)
#define LED_PIN         1              // ATtiny Pin 3
#define SENSOR_PIN         3            // ATtiny Pin 6

void setup(){
  pinMode(LED_PIN,OUTPUT);             // for general DEBUG use
  blinkLed(LED_PIN,2);                    // show it's alive
  TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
}


void loop(){
    if( TinyWireS.available() ){
      char cmd = TinyWireS.receive();
      if(cmd == 0x17){ // Meaning we need to return the light value from the light sensor
         TinyWireS.send(10);  // For test purposes, returns always 10
         blinkLed(LED_PIN,4);  // Blink 4times for OK
      }
      else {
         TinyWireS.send(0x00); // cmd was incorrect
         blinkLed(LED_PIN,1);    // Blink once for NOK
      }
    }
}

Hi,

Did you resolve your problem? I'm running into the exact same issue. I've tried the code on ATTiny85 and ATTiny2313. Same results. Would love to know if you were able to get the issue resolved. Thanks! ~Stephane