Attiny85 with ArduinoUno for I2c comm.

Hello Everyone,

I am recently working on attiny85 for I2C communication. I have gone through different libraries already like <Wire.h> , <TinyWire.h>, <tinyWireM.h>, <tinyWireS.h>.

In the start I want to send some byte of data through I2C comm and tried to scope the pin with oscilloscope but its not giving me the appropriate results. Looking on the internet about different ways to make attiny85 work with I2c is really heartless and I could not achieve teh task. Finally, I tried to make attiny85 as master and arduino Uno as slave as it was spare in my box.

I tried to make attiny85 as master and send data to arduino and looks the output on serial monitor but its showing zero.

For the reference, the master and slave codes are attached and my task is just simple to check on serial.

Attiny85 Master

#include <TinyWireM.h>

void setup()
{
  TinyWireM.begin();
}
void loop()
{
  TinyWireM.begin();
  TinyWireM.beginTransmission(0x08);
  TinyWireM.send(0x99);  
  int Byte1 = TinyWireM.endTransmission();
  delay(1000);
}

Arduino Uno smd as Slave

#include <Wire.h>
const byte add = 0x08;
int byte1;
void setup()
{
  Wire.begin(add);                // join i2c bus with address #10
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  Serial.println ("Data receiving");
  Serial.println(byte1);
  delay(1000);
}

void receiveEvent(int bytes)
{
  byte1 = Wire.read(); // receive byte as a character
}

and the serial output I am getting is attached also below.
Kindly give your valuable suggestion what am i doing wring here…

P.S : I want to extend the task in future to control i2c and SPI comm. in one go as i need to control two devices that requires two different protocols. What do you think, its possible as I am doing SPI comm. with shiftOut function with attiny85 as Master.

Thanks you

What core are you using for the ATTiny? TinyCore supports I2C natively, so there is no need to include any additional I2C library.

I don't understand what do you mean by referring to the word core ? Can u explain what are you trying to ask about attiny core as I have never looked into attiny core before so I don't know your point of view00.
Thanks for your reply.

Be aware that USI-based I2C is not available when USI-based SPI is in use.

By this quote i assume that SPI and I2c would not be possible to control two different devices But I am confused as once my one comm. is done I can switch onto other mode. Is that possible ?

Thanks you again

RakehSheikh:
I don't understand what do you mean by referring to the word core ? Can u explain what are you trying to ask about attiny core as I have never looked into attiny core before so I don't know your point of view00.
Thanks for your reply.

The Arduino IDE doesn't support any of the ATTiny series without an additional plugin, called a 'core'. There are several different cores that support the ATTiny series, DrAzzy's TinyCore is the best one around for the Tiny85.

RakehSheikh:
By this quote i assume that SPI and I2c would not be possible to control two different devices But I am confused as once my one comm. is done I can switch onto other mode. Is that possible ?

You probably can, by using something like this:

Serial.begin[9600];
(do Serial stuff here)
Serial.end();

Wire.begin();
(do I2C stuff here)
Wire.end();

but it would be worth checking this with @DrAzzy.

Thanks :slight_smile:

Yeah - with my core, you can almost pretend it's an Uno and just include Wire.h and SPI.h - the core automatically detects what hardware the chip has, and uses an appropriate implementation

I haven't tried switching between I2C and SPI. It may work - it is supposed to work - but I am pretty sure nobody has tested it, and there are practical concerns due to sharing the same pins.

I2C and SPI via shiftout will coexist without issue, since you can pick pins that aren't shared with the I2C bus - though there aren't many pins to go around on an attiny85.

You might want to consider the tiny841 instead - the tiny841 has hardware SPI (like, real SPI), hardware I2C slave or software I2C master (handled transparently in my core via the special version of Wire it includes - though for I2C on the 841 (and 828) you select from a menu in tools if you want slave, master, or both - picking both uses a lot more flash, but usually you only want one or the other).

I tried to follow your instructions and setup the Atiinycore for my attiny85 uC but after following all the steps and hardware steps accordingly when I tried to upload sketch it gives me the following error.
The IDE using is 1.6.4 because I downgraded it earlier as SPI comm. was not working well in previous stage of project.
At this point, I’m just trying to make i2c work with the code also attached !!

#include <Wire.h>

void setup()
{
 Wire.begin();
}
void loop()
{
 Wire.begin();
 Wire.beginTransmission(0x50);
//  TinyWireM.send(0x11);
 TinyWireM.send(0x99);  
 int Byte1 = TinyWireM.endTransmission();
 delay(1000);
}

Error received

Could not write build preferences file
java.io.FileNotFoundException: C:\Users\USER\AppData\Local\Temp\build2950559338258549534.tmp\test-i2c.cpp (The system cannot find the path specified)

  • at java.io.FileOutputStream.open0(Native Method)*
  • at java.io.FileOutputStream.open(FileOutputStream.java:270)*
  • at java.io.FileOutputStream.(FileOutputStream.java:213)*
  • at java.io.FileOutputStream.(FileOutputStream.java:162)*
  • at processing.app.debug.Compiler.preprocess(Compiler.java:1217)*
  • at processing.app.debug.Compiler.preprocess(Compiler.java:1177)*
  • at processing.app.debug.Compiler.compile(Compiler.java:354)*
  • at processing.app.debug.Compiler.build(Compiler.java:116)*
  • at processing.app.Sketch.build(Sketch.java:1162)*
  • at processing.app.Sketch.exportApplet(Sketch.java:1180)*
  • at processing.app.Sketch.exportApplet(Sketch.java:1166)*
  • at processing.app.Editor$DefaultExportHandler.run(Editor.java:2487)*
  • at java.lang.Thread.run(Thread.java:745)*
    java.lang.NullPointerException
  • at processing.app.debug.Compiler.adviseDuplicateLibraries(Compiler.java:442)*
  • at processing.app.debug.Compiler.build(Compiler.java:123)*
  • at processing.app.Sketch.build(Sketch.java:1162)*
  • at processing.app.Sketch.exportApplet(Sketch.java:1180)*
  • at processing.app.Sketch.exportApplet(Sketch.java:1166)*
  • at processing.app.Editor$DefaultExportHandler.run(Editor.java:2487)*
  • at java.lang.Thread.run(Thread.java:745)*

This is really frustrating for me as I’m stuck for past week. Let me know what do you think of error. Thank you for your response @DrAzzy.

Regardless of any other issues, the code you have posted still has several calls to TinyWire so that won't work.

What a small bug can do to the code. I was changing the library again and again to test and that's where I left my mistake. Thanks for pointing it out and now its all sort out

You’re welcome, thanks for letting us know that the problem was solved.