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.

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

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)

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

Components Used
Powered by
Downloads
Comments36
Join the discussion — share a question or tip.
- NA
hello i2c is not a full duplex , then how come the both master and slave are getting the outputs ?
- VL
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???
- NI
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
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.
- DO
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 -> NodeMCU 8266 2. transmit a number 1 or 2 or 3 or 4 from NodeMCU8566 -> 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
hi, can it be used for arduino mega? thanks
- TO
hello please help me, i have a problem in case i want to sent float data how can it possible. Thank
- LI
can i replace arduino uno with arduino due ? the wire connection still the same ?
- GI
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
First picture that apears when searcing for "nodemcu arduino i2c" in google and the I2C pins are mixed. You own me a 1l beer.
- MU
slave address can be in range of 0x7F. You are free to decide
- IO
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?
- NG
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!
- OD
is there space in this ??? void receiveEvent(inthowMany) or like this void receiveEvent(int howMany)
- OD
hi plz help me this error show when combile the code uno error: 'inthowMany' was not declared in this scope void receiveEvent(inthowMany) {
- MU
you have selected nodemcu while compiling
- RArahulsahubest2011· edited
This error is showing for arduino compiling error: variable or field 'receiveEvent' declared void
- AM
Replying to @lokeshc
sir, why there is nothing display on the serial monitor? already select Arduino board when compiling with arduino.
- TE
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
- DA
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
@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.

