Attiny1614
// #include < SoftwareSerial.h>
#include <Wire.h>
// // Software serial rx,tx pins of Attiny84
// const byte rxPin = 0;
// const byte txPin = 1;
//declaring mySerial as SoftwareSerial
// SoftwareSerial mySerial(rxPin, txPin);
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// 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
}
Xiao ESP32 C6
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
// Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}
Attiny1614
#include <Wire.h>
volatile byte receivedData = 0;
volatile byte dataToSend = 100; // Example data to send
void setup() {
Wire.swap(1);
// Wire.pins(8, 9);
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register receive event
Wire.onRequest(requestEvent); // register request event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
void receiveEvent(int howMany) {
while (1 < Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
int x = Wire.read();
receivedData = x;
Serial.print("Received: ");
Serial.println(x);
// You can update dataToSend based on received data if you want
dataToSend = receivedData + 10; // for example
}
void requestEvent() {
Wire.write(dataToSend);
Serial.print("Sent: ");
Serial.println(dataToSend);
}
Xiao ESP32 C6
#include <Wire.h>
byte x = 0;
void setup() {
Serial.begin(115200);
Wire.begin(); // I2C Master
}
void loop() {
// Write data to slave
Wire.beginTransmission(8); // address of slave
Wire.write(x);
Wire.endTransmission();
Serial.print("Sent: ");
Serial.println(x);
delay(100);
// Now request 1 byte from slave
Wire.requestFrom(8, 1);
if (Wire.available()) {
byte received = Wire.read();
Serial.print("Received from slave: ");
Serial.println(received);
}
x++;
delay(1000);
}