1

I'm trying to use the TinyWire library for I2C communication between a master ATTiny85 and a slave ATTiny85. Either master or slave as the sender is fine, but I have not been able to get a basic transmission working. I am using the Arduino IDE to program the ATTinys. The code I am testing should turn on the LED if the communication is a success. Please advise me. Thanks!

//Master Receiver
#include <TinyWire.h>
const int ledPin = 3;

void setup() {
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  TinyWire.begin();
  
}

void loop() {
  
  TinyWire.requestFrom(10, 1);
  bool receive = TinyWire.read();
  if (receive == 1) {
    digitalWrite(ledPin, LOW);
  }
  
}
//Slave Sender
#include <TinyWire.h>

void setup() {
  
  TinyWire.begin(10);
  TinyWire.onRequest(onRequestI2C);

}

void loop() {

}

void onRequestI2C() {
  
  TinyWire.send(1);
  
}

Also, here is the wiring: enter image description here

Also posted here.

  • For one thing your slave program should declare `onRequestI2C` before using it in the `onRequest` function. I'm kind of surprised that the program builds. – Some programmer dude Jun 29 '21 at 18:58
  • Thanks for the quick response! Could you please elaborate? The example for the slave sender in the TinyWire library is pretty much identical, with no other functions present. How do I "declare" ```onRequestI2C```? – anonymouseuser Jun 29 '21 at 19:17

2 Answers2

1

This isn't really an answer for the problem, but I've found another library that does what I want to achieve. It's called Manchester, and you can download it here.

This is the code I used for a basic test. The transmitter ATTiny85 collects the button information and sends it to the receiver ATTiny85, which turns on the LED based on if the button is pressed or not.

//Transmitter
#include <Manchester.h>
#define TX_PIN 0
const int buttonPin = 3;

void setup() {
  man.setupTransmit(TX_PIN, MAN_1200);
  pinMode(buttonPin, INPUT);
}

void loop() {
  bool button = digitalRead(buttonPin);
  man.transmit(button);
  delay(200);
}
//Receiver
#include <Manchester.h>
#define RX_PIN 1
const int ledPin = 3;

void setup() {
  man.setupReceive(RX_PIN, MAN_1200);
  man.beginReceive();
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
}

void loop() {
  if (man.receiveComplete()) {
    bool button = man.getMessage();
    man.beginReceive();
    if (button == 1) {
      digitalWrite(ledPin, LOW);
    } else {
      digitalWrite(ledPin, HIGH);
    }
  }
}

The wiring I did looked like this: enter image description here

0

For some reason, I can use Master Send and Slave Receive with this wiring, but still not Slave Send and Master Receive. With some extra programming, you should be able to make the master send a "request" and the slave receiver to switch to master mode to send back the requested data. Not efficient, but it works. (Master on the left, Slave on the right)

My code:

//Master Send
#include <TinyWire.h>
const int buttonPin = 3;

void setup() {
  TinyWire.begin();
  pinMode(buttonPin, INPUT);
}

void loop() {
  bool button = digitalRead(buttonPin);
  TinyWire.beginTransmission(10);
  TinyWire.send(button);
  TinyWire.endTransmission();
  delay(200);
}
//Slave Receive
#include <TinyWire.h>
const int ledPin = 3;

void setup() {
  TinyWire.begin(10);
  TinyWire.onReceive(receiveEvent);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
}

void loop() {
  delay(100);
}

void receiveEvent(int howMany) {
  while (TinyWire.available()) {
    int receive = TinyWire.read();
    if (receive == 1) {
      digitalWrite(ledPin, LOW);
    }else{
      digitalWrite(ledPin, HIGH);
    }
  }
}

enter image description here