NodeMCU V3 (ESP8266) – I2C Connection to Arduino Uno

In this article we will take a closer look at connecting NODEMCU V3 to Arduino UNO via I2C, since this method of connecting two controllers is the simplest and does not require additional logic level matching modules.
nodemcu-v3-connecting-to-arduino-via-i2c

In this article we will take a closer look at connecting NODEMCU V3 to Arduino UNO via I2C, since this method of connecting two controllers is the simplest and does not require additional logic level matching modules.

I2C is a serial asymmetric bus for communication between integrated circuits inside electronic devices. Uses two bidirectional communication lines (SDA and SCL).

The bus consists of two conductors, and one microcontroller is enough to control the interface. Surprisingly, this simplicity allows for disabling chips in the process. Special built-in filter is able to cope with bursts, ensuring the safety of the processed information.

The bus can operate in two modes: Master and Slave. In the first mode, the device performs the role of a transmitter, in the second – the data receiver. In this example, we will consider connecting NodeMCU as a master, and Arduino Uno as a slave, that is, a receiving device.

An example of connecting two devices to each other is shown in the diagram below:


Sketch programming is based on the popular library Wire.h, included in the standard Arduino IDE library package. This library allows devices to interact on I2C / TWI.

Let’s pass the test “Hello” from the Master to the Slave via I2C, and after receiving the data from the slave, we will transfer the “Hello” back.

The Wire library uses a 7-bit device addressing. Addresses from 0 to 7 are not used, as they are reserved, so the first address that can be used is 8. We will use 2 addresses to connect devices: 8 and 13.

SKETCH FOR NODEMCU

#include

void setup() {
Serial.begin(9600); 
Wire.begin(D1, D2); 
}

void loop() {
Wire.beginTransmission(8); 
Wire.write(“Hello Arduino”); 
Wire.endTransmission(); 

Wire.requestFrom(8, 13); 
while(Wire.available()){
char c = Wire.read();
Serial.print(c);
}
Serial.println();
delay(1000);
}

SKETCH FOR ARDUINO UNO

#include

void setup() {
Wire.begin(8); 
Wire.onReceive(receiveEvent); 
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop() {
delay(100);
}

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

void requestEvent() {
Wire.write(“Hello NodeMCU”);
}

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest