Arduino Mega2560 as I2C Slave not responding to Master??

Hello!! i am using ATtiny85 as Master to communicate with Arduino Mega2560, over I2C Protocol.

Here is the Slave Code:-

#include <Wire.h>

void setup()
{
  Serial.begin(9600);           // start serial for output
  pinMode(13,OUTPUT);
  Serial.println("Waiting For I2C Master");
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
}

void loop()
{
  digitalWrite(13,HIGH);
  //delay(100);
  digitalWrite(13,LOW);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

For ATtiny85 i am using AVR310 application note example to use USI as I2C, to communicate with Arduino Mega, but i am not able send data over I2C to Arduino Mega.
ATtiny85 code is fine as it is tested with DS1307 RTC and it gives proper response.(Tested on Proteus, simulation environment)

Mater code is as follow:-

#include "USI_TWI_Master.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int main( void )
{
  unsigned char buffer[] = {0x04, 0x55};
  USI_TWI_Master_Initialise();
  _delay_ms(2000);
  _delay_ms(2000);
  sei();  
  while(1)
  {
    USI_TWI_Start_Transceiver_With_Data(buffer,2);
    _delay_ms(1000);
  }
}

I had attached the result in attachment.
Similar results are obtained in real hardware, when tested on an oscilloscope.
Please someone help me, why this is not working.

Do you have an Arduino Uno ? If you run the i2c_scanner on it, you could try to detect the Mega.
The receiveEvent is an interrupt handler, it is not allowed to use a Serial function in there.

I don't know how compatible the ATtiny85 I2C is. The Arduino (as Slave) might perhaps use clock stretching, and not every chip (as Master) supports that.

Peter_n:
Do you have an Arduino Uno ? If you run the i2c_scanner on it, you could try to detect the Mega.
The receiveEvent is an interrupt handler, it is not allowed to use a Serial function in there.

I don't know how compatible the ATtiny85 I2C is. The Arduino (as Slave) might perhaps use clock stretching, and not every chip (as Master) supports that.

I don't know how compatible the ATtiny85 I2C is. The Arduino (as Slave) might perhaps use clock stretching, and not every chip (as Master) supports that.
Then how can i solve this problem, i have to do this, otherwise it will be a big trouble for me.

ATtiny85 uses its USI Hardware as I2C.

I saw the SCL line is low and i think this is what clock stretching means, but why it is there, even if i disconnect mega2560, the result is same.

Please help me, what can i do in this case.

I don't know, I know almost nothing about the USI I2C.
Trying to get it to work in a simualation might not be useful, they often don't work. You will find many questions in this forum that a simulation doesn't work.

If you have an Arduino Uno and a oscilloscope or a Bus Pirate or so, you could compare that with the signals when the ATmeg85 is used.

Can you (secretly) make the Mega the Master and the ATmega85 the Slave ?

Peter_n:
I don't know, I know almost nothing about the USI I2C.
Trying to get it to work in a simualation might not be useful, they often don't work. You will find many questions in this forum that a simulation doesn't work.

If you have an Arduino Uno and a oscilloscope or a Bus Pirate or so, you could compare that with the signals when the ATmeg85 is used.

Can you (secretly) make the Mega the Master and the ATmega85 the Slave ?

Can you (secretly) make the Mega the Master and the ATmega85 the Slave ?

I liked the word secretly.
I think yes i can make it, for that i have to find USI Slave Code.

I had tested the same setup with oscilloscope, and found exactly the same thing.
When saw these waveform on oscilloscope, i found that there is no stop condition and thought there might be some issue in USI code, but then i came home and tested it with DS1307 on Simulation and it is behaving the same as with Arduino mega as on Hardware.
I am attaching that below.
Is there any other method to do this, clock stretching is due Wire library or it will also be there if i implement, I2C Slave code without using Arduino IDE and its libraries.

I see green glitches in the signal, is that okay ?
I'm sorry, but I think I can help any further. I don't know enough of it. I read there was also trouble when people tried to use a Raspberry Pi as Master and Arduino as Slave.

The Arduino as Slave uses hardware and software (with interrupts), that is why it behaves different than normal hardware I2C devices like the DS1307.

Thanks for your help.

I don't have any other option, has have to do it.

I will try writing Slave code for Mega2560, using Atmel Studio and then see, whether it works or not.
Will report here whatever the results are.

The green glitches was on real oscilloscope, as well as on Proteus Oscilloscope.
I will update the results here soon.

Thanks for your efforts.

When no one else replies, you can start a new topic with a better subject. The Mega as Slave is not the problem, it is the compatibility between the ATtiny85 USI I2C and an Arduino as Slave.

I changed this line
Wire.begin(4); // join i2c bus with address #4

with this one
Wire.begin(2); // join i2c bus with address #2

as one wire library uses, 7-bit addressing, and hence 2 will automatically be converted to 0x04.

This works for just one time, and after that i am again seeing the same error.
Just working for the first time and after that same situation is there.

Yes, the Arduino Wire library uses the 7-bit shifted address. The USI I2C doesn't ?

The receiveEvent() is an interrupt handler. You may not use delay() or Serial.print() or Serial.println() in it. Did you remove it ?
You could use the 'howMany' parameter, instead of Wire.available(). The 'howMany' parameter is the number of bytes that are received and are ready in the buffer (a buffer inside the Wire library).

Yes USI doesn't support this.
But even after removing the Serial function, nothing works fine. Just for two times and that two on I2C Debugger of Proteus Simulation

ATtiny85 Sends data -> Data Received by Mega + ACK
ATtiny85 Sends data -> Data Received by Mega + ACK
ATtiny85 Sends data -> Data Not Received by Mega + NACK (Clock Stretching is there again SCL line is low)

But one thing is strange, i am receiving Acknowledgement for Address, but NACK for data sent.

Here is my Slave Code, i just removed all Serial.print lines

#include <Wire.h>

void setup()
{
  Serial.begin(9600);           // start serial for output
  pinMode(13,OUTPUT);
  //Serial.println("Waiting For I2C Master");
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
}

void loop()
{
  digitalWrite(13,HIGH);
  //delay(100);
  digitalWrite(13,LOW);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    // Serial.write(c);         // print the character
  }
  char x = Wire.read();    // receive byte as an integer
  // Serial.write(x);         // print the integer
}

And Master code is as follow:-

#include "USI_TWI_Master.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
char return_code = 0;
int main( void )
{
  unsigned char buffer[] = {0x08, 0x55, 0x56, 0x57};
  
  USI_TWI_Master_Initialise();
  sei();  
  while(1)
  {
    return_code = USI_TWI_Start_Transceiver_With_Data(buffer,4);
    _delay_ms(1000);
  }
}

Please don't tell me if it is working in a simulation or not. Those simulators are bad.
There are many topics on the forum about bad simulators.

Could you try it with real hardware ?

I tested it on real Hardware, and still no success :frowning:
Even i am not able to get data for single time, Clock line is low on mega2560

I'm out of ideas. Sorry. There is still a chance that they are incompatible. You need to find someone who knows about I2C on the ATtiny85.
The "Trinket" is an ATtiny85. Adafruit has written a TinyWireM wrapper around the USI I2C, to make it more compatible with the Arduino Wire functions. Would that make it possible to run the i2c_scanner on the ATtiny85 ?

Hi!!!
I am sorry,
i forget to un-comment this line

Wire.onReceive(receiveEvent); // register event

I checked on oscilloscope and found that everything was okay, then why i am not getting the response of serial port, when i came back on seat from lab, i found that Wire.onReceive(receiveEvent); is commented on code.
And after that everything works fine.

And yes once again, don't reply on simulations as same code is not working on simulation, and is working in Real Hardware.
Thanks for your support.

I do have one more query, in ATtiny85, i want to use PB4 as GPIO pin, but it is not working, always low, when i am using External Crystal Oscillator of 16Mhz, while working fine with internal 8MHz OSC.

Have a look at this thread.
http://www.avrfreaks.net/forum/pb4-pin-attiny85-low-when-using-external-crystal-16mhz-connected-xtal1-pin

I know this is off topic, if possible then please reply otherwise i will start a fresh topic for this.

So it is compatible after all. Awesome.

Can you distinguish two things ? There are crystals and there are oscillators. An oscillator creates a clock output.
When you select the external crystal, the two XTAL pins get the new function for the crystal. They can no longer be used for anything else.
Is there an option to use a pin as clock input for an external oscillator ? I don't know the ATtiny85 that well.

Try this : AVR® Fuse Calculator – The Engbedded Blog
Select the ATtiny85 (scroll down a lot in the drop-down list).
And there are only 2 options for external clock, and many options for external crystal.

The also call it "external crystal oscillator", they mean an oscillator that uses an external crystal.

Thanks a lot, it works now.

I had never used External Clock, i though external clock and external crystal are one and same thing.
​Thanks it working now.

As i am using External Clock and setting External Crystal fuse.