Connecting two ESP32 with I2C Bus

Hello,
I am trying to connect two ESP32 with the I2C bus. The master reads a button and is supposed to send the state of the button to the slave, which will let a LED shine.

Master:

#include <Wire.h>

#define SDA 21
#define SCL 23

int state = 0;

void setup() {
  // put your setup code here, to run once:
  Wire.begin(SDA,SCL);
}

void loop() {
  // put your main code here, to run repeatedly:
  state = digitalRead(14);
  Wire.beginTransmission(2);
  Wire.write(state);
  Wire.endTransmission();
}

Slave:

#include <Wire.h>
int state = 0;

void setup() {
  // put your setup code here, to run once:
  ledcSetup(0, 5000, 8);
  ledcAttachPin(19, 0);
  Wire.begin(2);
  Wire.onReceive(receiveEvent);
}

void receiveEvent(int b){
  state = Wire.read();
  if(state == LOW){
    ledcWrite(0, 255);
  }
  else{
    ledcWrite(0, 0);
  }
}

void loop() {
 
}

I did research and this is my current sketch, the master is compilable, at the slave I get the following error:
sketch\EK512_4.ino.cpp.o:(.literal._Z5setupv+0x10): undefined reference to TwoWire::onReceive(void (*)(int))' sketch\EK512_4.ino.cpp.o: In function setup()':
C:\Users\morit\Desktop\TGM\3\SYT\EK\5.1\EK512_4/EK512_4.ino:25: undefined reference to `TwoWire::onReceive(void (*)(int))'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.

Things I dont understand:
Can I just put any (imaginary) Adress in beginTransmission (at master), Wire.begin() (at slave)?
Why do I get this error trying to implement onReceive?
Are the specific diffrences - implementing I2C on a ESP?
What did I miss?

Pictures of circuit (bad):

Thanks for your time.

Welcome to the forum

The obvious question is why you are using two ESP32 boards

School

Do breadboard connections fix a compiler error?

I dont think that is has anything to do with the breadboard, i can implement the programm without I2C very easy and the LED and all is working.

No, I just noticed that the breadboard shown with using wires on the other side of the split and offered a suggestion to a possible issue. Sorry to had offended you. I'm out.

I am not offended.
if that's the impression, it's probably because of my bad English. I'm sorry

1 Like

Update: I2C works now with a ESP32 as master and Arduino Mega as slave, but im still interested in how the solution for ESP32 as both slave and master would look like...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.