0
votes

I am 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 the 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 as 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 as Slave

#include <Wire.h>
const byte add = 0x08;
int byte1;
void setup()
{
  Wire.begin(add);               
  Wire.onReceive(receiveEvent); 
  Serial.begin(9600);         
}

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

void receiveEvent(int bytes)
{
  byte1 = Wire.read(); 
}

But I am not able to get the output on serial monitor of arduino. What am i doing wrong here?

1

1 Answers

0
votes

I have used Atiny85 as a slave using TinyWireS lib (https://github.com/nadavmatalon/TinyWireS) some time back and it worked fine. Below were the pin configurations

ATtiny85 pin 5 with Arduino Uno A4 and ATtiny85 pin 7 with Arduino Uno A5

Below are my codes

Atiny.

#include "TinyWireS.h"                      

 const byte SLAVE_ADDR = 100;
 const byte NUM_BYTES = 4;

 volatile byte data = { 0, 1, 2, 3 };

 void setup() {
    TinyWireS.begin(SLAVE_ADDR);
    TinyWireS.onRequest(requestISR);
}

 void loop() {}

 void requestISR() {
    for (byte i=0; i<NUM_BYTES; i++) {
        TinyWireS.write(data[i]);
        data[i] += 1;
    }
}

Uno.

#include <Wire.h>

 const byte SLAVE_ADDR = 100;

 const byte NUM_BYTES = 4;

 byte data[NUM_BYTES] = { 0 };

 byte bytesReceived = 0;

 unsigned long timeNow = millis();

 void setup() {
    Serial.begin(9600);
    Wire.begin();
    Serial.print(F("\n\nSerial is Open\n\n"));
}

 void loop() {
    if (millis() - timeNow >= 750) {                                        // trigger every 750mS
        Wire.requestFrom(SLAVE_ADDR, NUM_BYTES);                            // request bytes from slave
        bytesReceived = Wire.available();                                   // count how many bytes received
        if (bytesReceived == NUM_BYTES) {                                   // if received correct number of bytes...
            for (byte i=0; i<NUM_BYTES; i++) data[i] = Wire.read();         // read and store each byte
            printData();                                                    // print the received data
        } else {                                                            // if received wrong number of bytes...
            Serial.print(F("\nRequested "));                                // print message with how many bytes received
            Serial.print(NUM_BYTES);
            Serial.print(F(" bytes, but got "));
            Serial.print(bytesReceived);
            Serial.print(F(" bytes\n"));
        }
        timeNow = millis();                                                 // mark preset time for next trigger
    }
}

 void printData() {
    Serial.print(F("\n"));
    for (byte i=0; i<NUM_BYTES; i++) {
          Serial.print(F("Byte["));
          Serial.print(i);
          Serial.print(F("]: "));
          Serial.print(data[i]);
          Serial.print(F("\t"));
    }
    Serial.print(F("\n"));    
}