I2C communication between nodeMCU and arduino uno

Hello there, i am having problem in sending message from arduino uno to NODEMCU (i.e. MASTER=uno and SLAVE=nodemcu) through I2C communication protocol . The problem is that despite making right connections and writing the following code the wire.available function in nodemcu is continuously returning 0 . Due to this the code is not working .

MASTER CODE FOR ARDUINO ---->>>

#include <Wire.h>
void setup() {
// Start the I2C Bus as Master
Serial.begin(9600);
Wire.begin();
}
int x=0;
void loop() {

Wire.beginTransmission( 8 );
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
delay(500);
}

SLAVE CODE FOR NODEMCU ---->>>>>

#include <Wire.h>
void setup() {
Serial.begin(9600); /* begin serial for debug /
Wire.begin(D1, D2);/
join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}

void loop() {
Serial.print(Wire.available());
while(Wire.available()){
int c = Wire.read();
Serial.print(c);
}
Serial.println();
delay(1000);
}

Do i need to write any argument in the wire.begin() function in the UNO code ? I have assumed the nodemcu adress to be “8” and thus have used “8” as argument in the wire.transmission function in the UNO code . I have tried writing “8” as argument in the wire.begin function as well but found no change . The main problem i have is that the wire.available() function in nodemcu code is returning 0 , and thats why nodemcu is not able to take any message from arduino uno . Please help as soon as possible.

The master should not have an address in the begin(). The slave has to have a address. See the NodeMCU docs for the begin() syntax.

Do you need level shifters between the 5V Uno and the 3.3V NodeMCU? Do you have pullup resistors on the SDA and SCL pins?

groundFungus:
The master should not have an address in the begin(). The slave has to have a address. See the NodeMCU docs for the begin() syntax.

Do you need level shifters between the 5V Uno and the 3.3V NodeMCU? Do you have pullup resistors on the SDA and SCL pins?

No, i didnt use any level shifters and pullup resisters because i have read at many places on the internet that the nodemcu pins are 5v tolerant and there is no problem in directly connecting it .

The main problem i have is that the wire.available() function in nodemcu code is returning 0 , and thats why nodemcu is not able to take any message from arduino uno .

I don’t know about the NodeMCU, that is why I asked about level shifters. The pullup resistors are not optional.

Did this ever get solved? I am having the same issue...

bkirkby:
Did this ever get solved? I am having the same issue...

Here is a solution using NodeMCU1.0-12E Module (I2C Master) and Arduino DUE (I2C Slave). The Master sends the data byte 0x23; the slave receives it and shows on its Serial Monitor. The Master makes a request to the Slave for 1-byte data; in response, the Slave sends 0x45 which is received by the Master and is displayed on Serial Monitor. (The Wire.h Library curretly supports NodeMCU only as Master.)

1. The connection diagram between NodeMCU and DUE using I2C Bus

2. The Sketches
(1) Master Sketch

#include<Wire.h>

void setup()
{
  Wire.begin(4, 5);
  Serial.begin(115200);
  
  Wire.beginTransmission(0x08);
  Wire.write(0x23);
  byte busStatus = Wire.endTransmission();
  Serial.println(busStatus);

  Wire.requestFrom(0x08, 1);
  byte x = Wire.read();
}

void loop()
{

}

(2) Slave Sketch

#include<Wire.h>
bool flag1 = LOW;
byte x;

void setup()
{
  Serial.begin(115200);
  Wire.begin(0x08);
  pinMode(13, OUTPUT);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
}

void loop()
{
  if (flag1 == HIGH)
  {

    Serial.println(x, HEX);
    flag1 = LOW;
  }
  digitalWrite(13, !digitalRead(13));
  delay(1000);
}

void receiveEvent(int howmany)
{
  x = Wire.read();
  flag1 = HIGH;
  //Serial.print(x, HEX);
}

void sendEvent(int howmany)
{
  Wire.write(0x45);
}

3. Upload Master sketch into NodeMCU. Bring in the Serial Monitor-1 (SM1)

4. Upload Slave sketch into DUE.

5. Reset the DUE; observe that the built-in LED (L) blinks. Bring in the Serial Monitor-2 (SM2).

6. Reset the NodeMCU; observe that 23 has papered on SM2.

BTW: I am not sure if the NodeMCU's pins are 5V tolerant or not; however, I replaced the DUE with UNO in the above setup; uploaded the sketches in the NodeMCU and UNO. The I2C Bus is found working and exchanging data.

Can I2C handle pictures?

Did anyone ever found out the problem? That is precisely my problem...

Rohla:
Did anyone ever found out the problem? That is precisely my problem…

I dont see anywhere where the int x is changing. it is set to 0 when declared but never altered. so when the master sends out x it is always sending out 0.

MASTER CODE FOR ARDUINO ---->>>

#include <Wire.h>
void setup() {
// Start the I2C Bus as Master
Serial.begin(9600);
Wire.begin();
}
int x=0; <<<<<----Right here x is set to 0
void loop() {

Wire.beginTransmission( 8 );
Wire.write(x); // sends x <<<<<------ x is sent so 0 is sent
Wire.endTransmission(); // stop transmitting
delay(500);
}

<<<<<------- X is never ++, +1, or anything

SLAVE CODE FOR NODEMCU ---->>>>>

#include <Wire.h>
void setup() {
Serial.begin(9600); /* begin serial for debug /
Wire.begin(D1, D2);/
join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}

void loop() {
Serial.print(Wire.available());
while(Wire.available()){ <<<<<<-------- X is received as 0
int c = Wire.read(); <<<<<<-------- C is declared as 0
Serial.print(c); <<<<<<-------- 0 is printed on monitor
}
Serial.println();
delay(1000);
}

unless im missing something

Hello guys, I achieved send data from NodeMCU(master) and Arduino nano(slave) and vice versa with this code:

(1)Node MCU(master):

// nodeMCU
#include <Wire.h>
String recibido;

void setup(){
  Wire.begin(4, 5);
  Serial.begin(115200);
}

void loop(){
  Wire.beginTransmission(8);//0x08 = 8;
  Wire.write("mcu to uno \n");
  Wire.endTransmission();

  Wire.requestFrom(8, 10);//0x08 = 8;

  String recibido;
  while (0 < Wire.available()){
    char c = Wire.read();
    recibido += c;
  }
  Serial.println(recibido);
  delay(3000);
}

(2)Arduino UNO (slave):

// arduino UNO
#include <Wire.h>

void setup() {
  Wire.begin(8);  //0x08 =8
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
  Serial.begin(9600);
}

void loop() {
  delay(1000);

}

void receiveEvent(int howMany){
  String recibido;
  while (0 < Wire.available()) {
    char c = Wire.read();
    recibido += c;
  }
  Serial.print(recibido);
}

void sendEvent(int howmany){
  Wire.write("uno to mcu");
}

I attached the images of the schema. I hope it helps.

Chalius:
Hello guys, I achieved send data from NodeMCU(master) and Arduino nano(slave) and vice versa with this

Can you send data from UNO as I2C Master and Node as I2C Slave? If, post your tested sketches please.

Check out this website:

ElectroTechnics:
Check out this website:

In the example you have referred, the Node is Master and UNO is Slave; I have asked for an example where the case is opposite – the UNO is Master and Node is Slave.