Slave ESP 32 reset when communicating between 2 esps using i2c

Hi. I am working on communicating between 2 esp32 boards using i2c and pins 18 as SDA and Pin 19 as SCL.

However, everything works well with Master ESP but slave ESP resets.

Here are the codes:

Master ESP:


#include <Wire.h>

#define I2C_SLAVE_ADDR 0x08  // Slave I2C address

void setup() {
  Wire.begin(18, 19); // Initialize I2C with SDA on pin 18 and SCL on pin 19
  Serial.begin(115200);
}

void loop() {
  Wire.beginTransmission(I2C_SLAVE_ADDR); // Start I2C transmission to the slave
  Wire.write("Hello from master");        // Send a message to the slave
  Wire.endTransmission();                 // End the transmission
  
  Serial.println("Data sent to slave");
  
  delay(1000);  // Wait 1 second before sending data again
}

Slave Code:

#include <Wire.h>

#define I2C_SLAVE_ADDR 0x08  // Slave I2C address

void receiveEvent(int bytes) {
  while (Wire.available()) {
    char c = Wire.read();  // Read each byte sent by the master
    Serial.print(c);       // Print the received character
  }
  Serial.println();         // Print a new line after the message
delay(1000);

}

void setup() {
  Wire.begin(I2C_SLAVE_ADDR, 18, 19);  // Initialize I2C as a slave with SDA on pin 18 and SCL on pin 19
  Wire.onReceive(receiveEvent);        // Register event to handle incoming data
  
  Serial.begin(115200);
  Serial.println("I2C Slave Ready");
}

void loop() {
  // Nothing to do in the main loop, data is received in the receiveEvent callback
}

I have connected SCL-SCL, SDA- SDA and common ground between two esps.

Error I am getting:

"
rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:1

load:0x3fff0030,len:1184

load:0x40078000,len:13260

"

Requesting you to help as this is crucial for my current project

Are you using Arduino Nano ESP32 boards or are you using other ESP32 boards?

Take the delay() and printing out of receiveEvent().

Not familiar with the ESP32, but on most Arduino's the onReceive function is executed as an interrupt handler, which will render delay() inoperable, as well as printing taking far too much time.

I am using ESP32 Devkit (38 pins)

It still resets even after removing delay

Topic moved. The Arduino Nano ESP32 section of the forum is only for, you can guess it, the Arduino Nano ESP32, not for other ESP32 boards.

1 Like

Instead of printing to Serial inside the receiveEvent() function, copy the data to a buffer, set a flag to indicate that data has been received, and do the printing in loop().

Would probably be a good idea to move Serial.begin() before Wire.begin(), if an I2C message is received in the brief time between Wire.begin() and Serial.begin() that could be problematic.

Getting the same error.

However If i use the default i2c pins (21 and 22) instead of pins 18 and 19 as sda, scl, It works well.

The error goes away if I modify the Wire.begin(I2C_SLAVE_ADDR, 18, 19); line. to

Wire.begin(18,19); 
  Wire.begin(I2C_SLAVE_ADDR);

but no data is received.

is Wire.begin(I2C_SLAVE_ADDR, 18, 19); correct? I even tried Wire.begin(18,19,I2C_SLAVE_ADDR); but getting the same error.

Try Wire.begin(I2C_SLAVE_ADDR, 18, 19, 100000), with the last number being the I2C bus speed (100KHz I think is the default). Using only three parameters, the library is expecting only the pin numbers and the bus speed, not the address.

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