master slave communication between two attiny 85 IC

2.8k Views Asked by At

Is it possible to communicate between two ATtiny85? I can use my Arduino to communicate with ATtiny85 by using Arduino Uno as the master and ATtiny85 as a slave. But I want to use one ATtiny85 as the master and one as a slave. Is this possible ?

I am not able to understand the examples given in TinyWireM library. I want a simple master and slave code for communication. For example, master should ask for 1 integer value and slave should reply.


My slave code :

#define I2C_SLAVE_ADDRESS 0x14 // Address of the slave
#include <TinyWireS.h>

int i=0;
void setup()
{
    TinyWireS.begin(I2C_SLAVE_ADDRESS); // join i2c network
    TinyWireS.onRequest(requestEvent);
}
void loop()
{
    TinyWireS_stop_check();
}
void requestEvent()
{
    if(i==1000)
    {
        TinyWireS.send(1);
        i=0;
    }
    else
        i++;
}

My master code

#include <TinyWireM.h> 
#define DS1621_ADDR   0x14

void setup() 
{
   TinyWireM.begin();
   pinMode(4, OUTPUT);

}
void loop() 
{
    TinyWireM.requestFrom(DS1621_ADDR,4); // Request 1 byte from slave
    int tempC = TinyWireM.receive(); 
    if(tempC)
    {
      digitalWrite(4, HIGH);   
      delay(1000);              // wait for a second
      digitalWrite(4, LOW);    
      delay(1000);              // wait for a second
    }
    if(tempC>1)
    {
      digitalWrite(4, HIGH);  
      delay(1000);              // wait for a second
      digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);              // wait for a second
    }
 }

I tried the above code but still I cannot see the LED blinking. But if I keep slave code as it is and use the following master code on an Arduino then everything works fine.

Arduino Uno code as master.

#include <Wire.h>
float i1=-1, i2=-1;

void setup()
{
 Wire.begin(); // join i2c bus (address optional for master)
 Serial.begin(9600); // start serial for output
}

void loop()
{
     Wire.requestFrom(4, 1); // request 1 byte from slave  address 4
     while(Wire.available()) // slave may send less than requested
     {
        i1 = Wire.read(); // receive a byte as character
        Serial.println(i1); // print the character 
     }
}

connection is and connections are SDA to SCL pins

pin 5 of master attiny85 - pin 7 of slave attiny85 pin 7 of slave attiny85 - pin 5 of master attiny85

I also tried by no cross connecting them. example and connections are SDA to SDA pins

pin 5 of master attiny85 - pin 5 of slave attiny85 pin 7 of slave attiny85 - pin 7 of master attiny85

but still no success.

2

There are 2 best solutions below

2
On

Yes, it is possible to use 1 ATtiny85 as a master, and another as a slave. The TinyWireM and TinyWireS libraries are both well-written and easy to use.

Handling a request and sending back bytes is as simple as setting the onRequest slave read event handler to a function of your choice that sends the correct data back. There are examples of this in the TinyWireS library.

0
On

Did you use the pull-up resistor for both, the SDA and SCL? They are important for the I2C protocol.

FYI: The logic '0' on the either bus is set by device driving actual '0' on the pin. On the other hand, the logic '1' is set on the bus by putting the device pin in the high impedance, in case on the ATtiny, it means setting pin into INPUT direction. When the both, the master and slave set pins to hiZ, the pull-up resistor pulls the voltage on the bus to value representing logic '1'. This solution allows avoiding contention on bi-directional bus (one device driving '1' and second driving '0') than can lead to short-circuit and damaging devices. So, if you don't use the pull-up resistor, the bus will be left floating whenever logic '1' is driven and it will lead to protocol errors.