NodeMCU I2C with Arduino IDE
I2C (Inter-Integrated Circuit) is 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 acknowledgment based communication protocol i.e. transmitter checks for an acknowledgment from the receiver after transmitting data to know whether data is received by receiver successfully.
I2Cworks in two modes namely,
- Master mode
- Slave mode
SDA (serial data) wire is used for data exchange in between master and slave device. SCL (serial clock) is used for the synchronous clock in between master and slave device.
Master device initiates communication with a slave device. Master device requires slave device address to initiate conversation with a slave device. Slave device responds to 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.
Let’s write Arduino sketch for NodeMCU as I2C master device and Arduino sketch for Arduino Uno as I2C slave device. Master device sends hello string to slave device and slave device will send hello string in response to the master device.
Here, we are using
Master Device: NodeMCU
Slave Device: Arduino Uno
Slave Device Address: 8
Interfacing diagram is shown in below figure
Arduino Sketch for NodeMCU (Master I2C Device)
Arduino Sketch for Arduino Uno (Slave I2C Device)
Output window of serial monitor at Slave device (Arduino Uno)
Output window of serial monitor at Master device (NodeMCU)