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 |
||
---|---|---|
NodeMCU NodeMCUNodeMCU |
X 1 | |
ESP12F ESP12E |
X 1 |