I2C connection between two boards (Beginner)

I want to connect my esp32 c3 supermini (slave) to esp8266 (master). Slave should display received data on OLED screen, while master should send some data (like string). I connected SDA and SLA pins and ground pins correctly. But when I run test script, it doesn't work: display is black, in serial port (master at 115200 baud) I see message being sent: ..., but there was nothing on slave side. One more question: do I need to use some resistors to stabilize voltage on SDA and SLA pins? Or ESP boards already have them?

Sketch - Master's

#include <Wire.h>
#define SLAVE_ADDR 0x08  // АдрСс ESP32-C3

void setup() {
  Wire.begin();  // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ I2C ΠΊΠ°ΠΊ мастСр
  Serial.begin(115200);
}

void loop() {
  int number = 10;  // Число для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ
  Serial.print("Sending: ");
  Serial.println(number);

  // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° числа ΠΏΠΎ I2C
  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write((uint8_t)number);  // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° числа ΠΊΠ°ΠΊ Π±Π°ΠΉΡ‚Π°
  Wire.endTransmission();

  delay(2000);  // ΠŸΠ°ΡƒΠ·Π° ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠ°ΠΌΠΈ
}

Sketch - Slave's

//Slave
#include <Wire.h>
#include <U8g2lib.h>  // Π‘ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° U8g2 для OLED

#define SLAVE_ADDR 0x08  // АдрСс ESP32-C3
#define OLED_SDA 8       // Пин SDA для OLED
#define OLED_SCL 9       // Пин SCL для OLED

// Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ OLED (для SSD1306 128x64)
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /*reset=*/ U8X8_PIN_NONE, OLED_SCL, OLED_SDA);

int receivedNumber = 0;  // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ для хранСния ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½ΠΎΠ³ΠΎ числа

void setup() {
  Serial.begin(115200);
  
  // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ I2C (slave)
  Wire.begin(SLAVE_ADDR);
  Wire.onReceive(receiveEvent);

  // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ OLED
  u8g2.begin();
  u8g2.setFont(u8g2_font_6x10_tf);  // Π’Ρ‹Π±ΠΎΡ€ ΡˆΡ€ΠΈΡ„Ρ‚Π°
  u8g2.setCursor(0, 15);            // ΠΠ°Ρ‡Π°Π»ΡŒΠ½Π°Ρ позиция тСкста
  u8g2.clearBuffer();
  u8g2.print("Waiting...");
  u8g2.sendBuffer();
}

void loop() {
  // Основной Ρ†ΠΈΠΊΠ» пуст, всС дСйствия Π² receiveEvent
}

// ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΡ€ΠΈΠ΅ΠΌΠ° Π΄Π°Π½Π½Ρ‹Ρ… ΠΏΠΎ I2C
void receiveEvent(int bytesReceived) {
  if (Wire.available()) {
    receivedNumber = Wire.read();  // Π§Ρ‚Π΅Π½ΠΈΠ΅ числа ΠΊΠ°ΠΊ Π±Π°ΠΉΡ‚Π°
    Serial.print("Received: ");
    Serial.println(receivedNumber);

    // ОбновлСниС OLED
    u8g2.clearBuffer();
    u8g2.setCursor(0, 15);  // ΠŸΠΎΠ·ΠΈΡ†ΠΈΡ тСкста (x, y)
    u8g2.print("Number: ");
    u8g2.print(receivedNumber);  // Π’Ρ‹Π²ΠΎΠ΄ числа Π½Π° экран
    u8g2.sendBuffer();
  }
}

Or how else can these boards be connected?

Wiring:


Serial monitors
Master's:

Slave's:

You can't: both are set to be masters.

For the slave, you need to use another set of pins to communicate on the I2C bus and you need to bit bang the communications.

1 Like

I connected
SDA (esp32 c3 sm) - SDA (esp8266)
SCL (esp32 c3 sm) - SCL (esp8266)
GRD (esp32 c3 sm) - GRD (esp8266)

Wire.begin(); //To set master's board
Wire.begin(ADDR);// To set slave's board

I watched a tutorial in yt, and they just connected A4, A5 and Ground pins on two Arduino uno boards respectively.

You mean: I have to change I2C pins on slave's side?

Sorry, If I ask very stupid questions and waste your time :'/

1. Connect Master's (ESP8266) SDA (D4) and SCL (D3) lines with correspondig SDA (GPIO-4, Fig-1) and SCL (GPIO-5, Fig-1) lines of Slave (ESP32-C3). (I am not sure or forgotten if you need pull-up resistors for the I2C Bus.) You need to include this line: Wire.begin(D4, D3) in the setup() function of Master.


Figure-1:

2. Try by uplaoding the the following sketches without OLED, which are slightly modified versions of yours:
Master Sketch:

#include <Wire.h>
#define SLAVE_ADDR 0x08  // АдрСс ESP32-C3

void setup() 
{
  Wire.begin(D4, D3);   // SDA = D4 (GPIO/DPin-2), SCL = D3 (GPIO-0) 
  Serial.begin(115200);
}

void loop() 
{
  byte number = 10;  // Число для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ
  Serial.print("Sending: ");
  Serial.println(number);

  // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° числа ΠΏΠΎ I2C
  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write(number);  // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° числа ΠΊΠ°ΠΊ Π±Π°ΠΉΡ‚Π°
  Wire.endTransmission();

  delay(2000);  // ΠŸΠ°ΡƒΠ·Π° ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠ°ΠΌΠΈ
}

Slave Sketch:

//Slave
#include <Wire.h>

#define SLAVE_ADDR 0x08  // АдрСс ESP32-C3
#define OLED_SDA 8       // Пин SDA для OLED
#define OLED_SCL 9       // Пин SCL для OLED
volatile bool flag = false;

byte receivedNumber = 0;  // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ для хранСния ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½ΠΎΠ³ΠΎ числа

void setup() 
{
  Serial.begin(115200);
  
  // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ I2C (slave)
  Wire.begin(SLAVE_ADDR);
  Wire.onReceive(receiveEvent);
}

void loop() 
{
    if(flag == true) 
    {
        Serial.println(receivedNumber, DEC);
        flag = false;
    }
}

// ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΡ€ΠΈΠ΅ΠΌΠ° Π΄Π°Π½Π½Ρ‹Ρ… ΠΏΠΎ I2C
void receiveEvent(int bytesReceived) 
{
    receivedNumber = Wire.read();
     flag = true;
}

3. Check that Slave's Serial Monitor shows 10 at 2-sec interval.
4. If Step-2 is not correct, trouble shoot by deecting the pesence of the Slave.
5. If Step-2 is correct, connect OLED with Slave to display the receiced data.

1 Like

No.
You wiring and code look to be correct.
However you need pullup resistors.
Connect a10K resistor from SCL to 3.3V and from SDA to 3.3V

As you already know, your wiring is correct

SDA (esp32 c3 sm) - SDA (esp8266)
SCL (esp32 c3 sm) - SCL (esp8266)
GRD (esp32 c3 sm) - GRD (esp8266)

That is totally wrong

1 Like

Hi! welcome to the Forum.

Take a look in the ESP-Now protocol. You can connect these 2 boards wirelessly.

1 Like

I really appreciate your answer. I2C communication finally worked! I also added 2 pull up resistors 10k each.

These are the correct pin names for my Super Mini and ESP8266:

Everything was fine until I added u8g2.begin(); And I spent almost 1 day to find out the reasons.

  1. ESP8266 - Master;
  2. ESP32-C3 Supermini - Slave;
  3. OLED - Slave;

I tried to achieve control of one slave device (ESP32-C3 Supermini) by another slave device (OLED), but it is not possible cuz only Master can communicate with Slaves.

Masters are designed only to transmit and request data to slaves.
Slaves are designed only to receive and respond to the master.

Nothing about communication between the slave devices.
One slave device could not be master to another slave device.

Please, post your sketches.

I decided to use UART communication instead of I2C. I did not change anything in my sketch, so I used the previous published one

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