NodeMCU I2C with Arduino IDE

NodeMCU based ESP8266 has an I2C (Inter-Integrated Circuit) feature. It is used to communicate with I2C enabled devices such as LCD, OLED displays, EEPROM, RTC, magnetometer, accelerometer, etc.

172.8k views2 min readUpdated Aug 8, 2023
NodeMCU I2C with Arduino IDE

Introduction to I2C

I2C (Inter-Integrated Circuit) is a serial bus interface connection protocol. It is also called as TWI (two-wire interface) since it uses only two wires for communication. Those two wires are SDA (serial data) and SCL (serial clock).

I2C is an acknowledgment-based communication protocol i.e. transmitter checks for an acknowledgment from the receiver after transmitting data to know whether data is received by the receiver successfully.

I2C works in two modes namely,

  • Master mode
  • Slave mode

SDA (serial data) wire is used for data exchange between master and slave devices. SCL (serial clock) is used for the synchronous clock in between master and slave devices.

The master device initiates communication with a slave device. The master device requires a slave device address to initiate a conversation with a slave device. The slave device responds to the master device when it is addressed by a master device.

NodeMCU has I2C functionality support on its GPIO pins. Due to internal functionality on ESP-12E, we cannot use all its GPIOs for I2C functionality. So, do tests before using any GPIO for I2C applications.

 

Example

Let’s write an Arduino sketch for NodeMCU as an I2C master device and Arduino sketch for Arduino Uno as an I2C slave device. The master device sends a hello string to the slave device and the slave device will send a hello string in response to the master device.

Here, we are using

Master Device: NodeMCU

Slave Device: Arduino Uno

Slave Device Address: 8

The interfacing diagram is shown in the below figure

This Diagram Shows NodeMCU Arduino interface using I2C Protocol
NodeMCU Arduino I2C interface

 

NodeMCU I2C Code using Arduino IDE (Master Device)

#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() {
 Wire.beginTransmission(8); /* begin with device address 8 */
 Wire.write("Hello Arduino");  /* sends hello string */
 Wire.endTransmission();    /* stop transmitting */

 Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
 while(Wire.available()){
    char c = Wire.read();
  Serial.print(c);
 }
 Serial.println();
 delay(1000);
}

 

Arduino Uno I2C Code (Slave Device)

#include <Wire.h>

void setup() {
 Wire.begin(8);                /* join i2c bus with address 8 */
 Wire.onReceive(receiveEvent); /* register receive event */
 Wire.onRequest(requestEvent); /* register request event */
 Serial.begin(9600);           /* start serial for debug */
}

void loop() {
 delay(100);
}

// function that executes whenever data is received from master
void receiveEvent(int howMany) {
 while (0 <Wire.available()) {
    char c = Wire.read();      /* receive byte as a character */
    Serial.print(c);           /* print the character */
  }
 Serial.println();             /* to newline */
}

// function that executes whenever data is requested from master
void requestEvent() {
 Wire.write("Hello NodeMCU");  /*send string on request */
}

 

Output Window

Output window of the serial monitor at Slave device (Arduino Uno)

Arduino Slave output window

Output window of the serial monitor at Master device (NodeMCU)

Arduino output window of NodeMCU I2C master

Components Used

Powered byMouser Electronics

Downloads

Comments36

Join the discussion — share a question or tip.

  • NA
    naveengundluri

    hello i2c is not a full duplex , then how come the both master and slave are getting the outputs ?

  • VL
    VladimirEfimenco

    Hi, thank you for tutorial. Is any way to not declare exact number that i expecting to get from slave? For example i want to get actual PWM values from some pins by request, that are dynamic. 5(pinNum);155(pwmValue) and i can't know number i expecting to get. It can be 5;0 or 5;255 etc.

  • SH

    hello, i try to I2C communication b/w Arduino and nodeMCU . NodeMCU does not work as a slave device properly, can you share block of nodeMCU as a slave and arduino as a master???

    • CO
      coreywirun

      This was mentioned in the first comment to this article. Have a look. https://github.com/esp8266/Arduino/issues/1330

    • SH

      Replying to @coreywirun

      ok that mean esp doesn't work as a slave mode.

  • NI
    ninadkulkarni

    Please note that NodeMCU ESP8266 has SCL SDA in the opposite order as the one you have shown. Please do correct it so that technologists don't end up losing their time. Keep up the great work !

  • ME
    menikhilv143

    hello i am struggling from last 1 months to how to interface arduino uno with node mcu and i had tried many examples for it. but, they are unsuccessful. but your example work thank you so much for this information. can you just help in, i am doing one project in that some sensors are collecting the data in arduino uno and that data i have to send to website using nodemcu. how can i do it by using node mcu and arduino uno interfacing. please help if you have any solution for it.

  • SH

    Hello sir I am using esp8266 to read data from mpu6050 i am able to do this. Pls help me as I want to send this data to cloud how it can be done.

    • MU
      muman613

      Look at the wifi library. the esp8266 has wifi capability and you should be able to connect it to your Wifi access point. From there you can either connect to a socket on a server or send an HTTP request to a web application.

  • DO
    donsissing

    Hi, This is exactly what I need to do, expect I want to : 1. transmit a number 1 or 2 or 3 or 4 from Uno -&gt; NodeMCU 8266 2. transmit a number 1 or 2 or 3 or 4 from NodeMCU8566 -&gt; Uno 3. All pins are currently use expect D13, RX, TX and A0 and 1 Gnd I have been struggling for over 3 weeks now and you example is the closest I have seen. PLEASE PLEASE can you help me, thanks.

  • LE
    lexieaepiphany

    hi, can it be used for arduino mega? thanks

  • TO
    topantop28

    hello please help me, i have a problem in case i want to sent float data how can it possible. Thank

    • LO
      lokeshc

      Use string library to communicate all type of data conversion.

  • LI
    limchuntatt

    can i replace arduino uno with arduino due ? the wire connection still the same ?

    • GI
      giorgirshengelia

      No, you'll need to connect pins other way. Uno has SDA at port A4 and SCL at A5, Due has two I2C interfaces SDA1 and SCL1 are near to the AREF pin and the additional one is on pins 20 and 21. https://www.arduino.cc/en/reference/wire

  • PO
    popgheorghe

    First picture that apears when searcing for "nodemcu arduino i2c" in google and the I2C pins are mixed. You own me a 1l beer.

  • AP
    aparnarm

    Why is the slave address 8

    • MU
      muhammedimdaad16

      slave address can be in range of 0x7F. You are free to decide

  • IO
    iotcarriot8266

    I have a question i want to send multiple sensor data from nodemcu to arduino uno but only one value is sends to slave why?

  • HA
    hasanagha

    Thans for nice demo. I can do the connection. The ESP receive correct string but arduino ricive garbage. What can be problem!? I use ESP-E12 and Arduino pro mini.

  • NG
    nguyentanduy69

    Can you help me, please! I'm beginer about ARDUINO so i have problem that is send data from arduino to ESP 8266. Example i have random number and i want to send that number from arduino to ESP but i don't know how to do that. Thank your help!

    • LO
      lokeshc

      Read the above given information carefully. Here, in above example string send from esp8266 instead of that you can send random string/no. from arduino to esp8266. Just make sure that you are using esp8266 as master and Arduino as slave.

  • OD
    odaytoball

    is there space in this ??? void receiveEvent(inthowMany) or like this void receiveEvent(int howMany)

    • LO
      lokeshc

      Yeah, you are right. Thanks for notifying.

    • OD
      odaytoball

      Replying to @lokeshc

      welcom your code help me alot thankkkks

  • OD
    odaytoball

    hi plz help me this error show when combile the code uno error: 'inthowMany' was not declared in this scope void receiveEvent(inthowMany) {

  • RA
    rahulsahubest2011· edited

    This error is showing for arduino compiling error: variable or field 'receiveEvent' declared void

    • LO
      lokeshc

      Hello, Select Arduino board for compiling with arduino and select nodemcu board for compiling with nodemcu. Then, it will work if not then let me know.

    • AM
      amirahazri

      Replying to @lokeshc

      sir, why there is nothing display on the serial monitor? already select Arduino board when compiling with arduino.

    • LO
      lokeshc

      Replying to @amirahazri

      Check your connections properly and read the above content properly. May be you have missed something while interfacing.

  • TE
    terc1971

    Hello, Please help me with one issue: Arduino Uno/Mega uses 5V while Nodemcu board uses 3.3V. Why you did not use a bi-directional level converter? Is there any chance to damage the Nodemcu without level converter? I am asking you because I tried to use the level converter and the boards did not find each other on I2C...but it works perfect without level converter. Thanks, Eugen

    • LO
      lokeshc

      @EUGEN TERCHILA: NodeMCU board (ESP8266 based module) has IO pins which are 5V tolerant mentioned in the official datasheet of ESP8266. So there is no need for a level converter to use I2C pins of NodeMCU board which is also 5V tolerant.

  • DA
    daniloespinoza96

    Hello, i am doing my thesis right now, and i use something similar to this, but instead of being the NodeMCU the master i would prefer the Arduino to be the master, what shouls i change to make it possibe? Thanks Beforehand, Sirius~

    • LO
      lokeshc

      @Danilo Espinoza: I did not try any I2C slave sample for NodeMCU. On their official git page https://github.com/esp8266/Arduino/issues/1330 the issue is mentioned about support for I2C slave mode in esp8266. But it can possible to try with software I2C implementation. I have not tried yet for software I2C but I will try for the same and share when it get done. If you get it done please do share with me. It will help me too.

    • VI
      VicenteRubioJuan

      Replying to @lokeshc

      Have you managed to do it? I have the same issue. Thanks