I2C Communication Between Two esp8266 Wemos D1 mini

Hi all,

I am trying to make 2 Wemos D1 mini cards communicate bidirectionally via I2C, bu i can`t figure why is not working.

On both boards I have LEDs on pin D3 and a push button on pin D6.
The D1 (SCL) is connected directly to D1 of the other board, and D2 (SDA) to D2.
PB and LED tested and working.

The Master:

#include<Wire.h>
const int pbPin = D6;
const int ledPin = D3;

int slaveCh = 8;
int MasterRX = 0;
int MasterTX = 0;

void I2Cscan() {
  Serial.println("\n------I2C Scan Start------");
  byte error, address;
  int nDevices;
  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
  
    if (error == 0){
      Serial.print("I2C device found at address 0x");
      if (address<16) Serial.print("0");
      Serial.println(address,HEX);
     
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknown error at address 0x");
      if (address<16) Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0) Serial.println("No I2C devices found\n");
  else Serial.println("------I2C Scan end------\n");
}

void setup() { 
  delay(2000);
  Serial.begin(115200);                    
  Wire.begin();                            
  Wire.status();          
  I2Cscan();
  
  pinMode(ledPin, OUTPUT);
  pinMode(pbPin, INPUT_PULLUP);
}

void loop(){
    Wire.requestFrom(slaveCh,2);         // Slave Address, Q.ty of byte (int=2)
    MasterRX = Wire.read();    
    Serial.println("RX: "+String(MasterRX));
    digitalWrite(ledPin,MasterRX);
    
    MasterTX = digitalRead(pbPin);                       
    Wire.beginTransmission(slaveCh);     // start transmit 
    Wire.write(MasterTX);              
    Wire.endTransmission();              // stop transmitting
    Serial.println("TX: "+String(MasterTX));
    
    delay(500);                                     
}     

The Slave:

#include<Wire.h>                       
const int pbPin = D6;
const int ledPin = D3;

int slaveCh = 8;
int SlaveRX = 0;
int SlaveTX = 0;

void setup() {
  delay(2000);
  Serial.begin(115200);                   
  Wire.begin(8);                       //Slave Address 
  Wire.onReceive(RX_Event);           //Function call when Slave receives value from master
  Wire.onRequest(RQ_Event);           //Function call when Master request value from Slave
  Wire.status();          


  pinMode(D3, OUTPUT);
  pinMode(D6, INPUT_PULLUP);
} 

void RX_Event (int howMany){       //This Function is called when Slave receives value from master
   SlaveRX = Wire.read();        
   Serial.println("RX: "+String(SlaveRX)); 
}

void RQ_Event(){                    //This Function is called when Master wants value from slave
  SlaveTX = digitalRead(D6);  
  Wire.write(SlaveTX);            
  Serial.println("TX: "+String(SlaveTX));   
}

void loop(void){
  delay(100);
}

So after the upload, the master can see that there is a slave connected to the address 8, but don't receive any data (doesn't change the RX value when i push the button).

23:58:04.138 -> ------I2C Scan Start------
23:58:04.138 -> I2C device found at address 0x08
23:58:04.138 -> ------I2C Scan end------
23:58:04.138 ->
23:58:04.138 -> RX: 255
23:58:04.138 -> TX: 1
23:58:04.652 -> RX: 255
23:58:04.652 -> TX: 1
23:58:05.166 -> RX: 255
23:58:05.166 -> TX: 1
23:58:05.681 -> RX: 255
23:58:05.681 -> TX: 1

The slave also don`t receive or transmit any data, so there is no data on the serial.

Can anyone help me?

1 Like

Pull-up resistors may help.

Yes, but I don't want to :woozy_face:
The I2C bus is not the best solution to communicate between processors.

  1. Why do you need two ESP8266 boards ?

As soon as the Slave does Wire.begin(8); then the Master should be able to detect it with the I2C Scanner. That works, so there is something working.

  1. Do you know if the ESP8266 in Slave mode works ? Others have a lot of trouble with it. Some have to lower the clock speed to 20kHz. I don't know the latest status. Can you find a recent example of someone who has it working ?
  2. Did you also connect the GNDs ?
  3. Do you have pullup resistors ?

Things that do work:

  1. Replace the two ESP8266 boards with a single ESP32 board.
  2. Use two ESP32 boards with Serial communication: RX, TX and GND.
  3. Wireless via a router, or ESP-NOW.

the esp8266 Wire library slave mode doesn't work

Yes, but I don't want to :woozy_face:

Thank you!:rofl:

1.Why do you need two ESP8266 boards ?

because for the project I have in mind I have a Wemos D1 mini that connects to other Wemos D1 mini via ESP-NOW, and to control it from the mobile phone it has to connect to the Blynk server, but apparently it is not possible to use the 2 communications on a single controller and therefore the need to separate them and connect them by wire.

  1. Do you know if the ESP8266 in Slave mode works ? Others have a lot of trouble with it. Some have to lower the clock speed to 20kHz. I don't know the latest status. Can you find a recent example of someone who has it working ?

Unfortunately i didn't find any example with ESP8266, and still don't know if it is possible.

3.Did you also connect the GNDs ?
4.Do you have pullup resistors ?

Yes and Yes, 1k ohm, sorry I forgot to write it.

5.Replace the two ESP8266 boards with a single ESP32 board.

Hmm do you know if ESP32 can support Blynk and ESP-NOW at the same time?

6.Use two ESP32 boards with Serial communication: RX, TX and GND.
7.Wireless via a router, or ESP-NOW

Two ESP32 becomes too expensive for me :laughing:

1 Like

Juraj wrote:

the esp8266 Wire library slave mode doesn't work

So you have to find an other solution.

If you want to continue with the ESP8266, you could run SoftwareSerial on both. They have a spare hardware TX signal, then you need SoftwareSerial on both for RX.
Some Arduino boards have a ESP32 for Wifi, and a normal processor for the sketch. The communication is with serial AT commands I think. This is such a board.
It is normal to have a serial communication to a ESP for just the Wifi.

As far as I know, the combination of Wifi / Bluetooth / ESP-NOW is one way or the other a problem for both the ESP8266 and the ESP32.

Ok, thank you very much to both!
I will try the SoftwareSerial way!

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