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?
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
jim-p
February 2, 2025, 8:59am
6
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)
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.
ESP8266 - Master;
ESP32-C3 Supermini - Slave;
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
system
Closed
August 6, 2025, 12:53pm
12
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.