I've been working on a project with I2C protocol where an ATmega328 (Arduino) configured as slave, receives messages from an ATtiny (ATtiny85, ATTinyCore by SpenceKonde) configured as master, or from an ESP8266 (ESP-01, Arduino core) configured as master.
All using the standard wire library, powered with 3.3v (LM1117 regulator), SDA and SCL 4.7kohms pull-ups. The ESP pins used are RX and TX, with 330ohms pull-ups.
Individually, it works (ATtiny to ATmega; ESP to ATmega), but when i connect the three on the same bus, after the first message it stops working and the ATmega doesn't receive more messages.
Code (modified for readability)
ATmega slave:
#include <Wire.h>
const int ADDRESS = 56;
void receiveEvent() {
Serial.println(Wire.read());
}
void setup() {
Serial.begin(9600);
Wire.begin(MY_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
}
ESP master:
#include <Wire.h>
const int DISPLAY_ADDRESS = 56;
const int PACKET_SIZE = 1;
const int SDA_PIN = 1;
const int SCL_PIN = 3;
void setup(){
Wire.begin(SDA_PIN, SCL_PIN);
}
void loop() {
byte packet[PACKET_SIZE];
packet[0] = 'e';
Wire.beginTransmission(DISPLAY_ADDRESS);
Wire.write(packet, PACKET_SIZE);
Wire.endTransmission();
delay(2500;)
}
ATtiny master:
#include <Wire.h>
const int DISPLAY_ADDRESS = 56;
const int PACKET_SIZE = 1;
// SDA = 0; attiny85 pin 5
// SCL = 2; attiny85 pin 7
void setup() {
Wire.begin();
}
void loop() {
byte packet[PACKET_SIZE];
packet[0] = 'a';
Wire.beginTransmission(DISPLAY_ADDRESS);
Wire.write(packet, PACKET_SIZE);
Wire.endTransmission();
delay(1000);
}
After some research I found out that the wire library can only work with a single master, is this correct? And the esp can only work in master mode.
Am I doing something wrong, or is this simply not possible?
Are there any protocol alternatives for this design? A single device that only cares about receiving messages, and multiple other devices that only care about sending messages to the central.
Thank you for your time!