In this tutorial we will learn how to make serial communication between Arduino to ESP8266 & ESP8266 to Arduino. Serial communication is required when you want to transfer sensor data or any data from one device to another device, In our case it is ESP8266 NodeMCU and Arduino.  Moreover, we will transfer DHT22 Sensor data from Arduino to NodeMCU and NodeMCU to Arduino. Apart from this we will also see how to use software serial library on Arduino and Serial1 on ESP8266 NodeMCU, So that you can transfer data with another serial port, which we will be helpful. If you are doing lots of things on both Arduino and ESP8266 NodeMCU. So let’s get started.

Components Required:
  1. NodeMCU ESP8266 12E,
  2. Arduino,
  3. DHT22 sensor,
  4. 10K ohms Resistor / 1k & 2k ohms resistor for voltage divider,
  5. Breadboard,
  6. Jumper Wire.
Libraries Required:
  1. DHT22 Library,
  2. Adafruit Sensor, (If you haven’t installed yet)

How to add library in Arduino IDE.

Make sure you already setup ESP8266 for Arduino IDE. If you didn’t setup yet. You can read How to setup ESP8266 for first time in Arduino IDE.

As in this project I am transferring DHT sensor data from Arduino to NodeMCU and NodeMCU to Arduino. So I need this library for that sensor to work.

Software Serial library: This library is inbuilt in Arduino IDE. You don’t need to install it.

Voltage Divider for NodeMCU’s Serial Communication Rx Pin:

ESP8266 works on 3.3V and Arduino Tx pin supplies 5V, which can damage your ESP8266 NodeMCU. By using voltage divider you are reducing Arduino Tx 5V to 3.3V for Rx of ESP8266.

Unfortunately it didn’t work. It shows blank screen on serial monitor, if it is connected via voltage divider. when I connect direct Arduino’s Tx to NodeMCU’s Rx, it starts showing sensor data. I tried everything to make it work, checked voltage with multimeter. It was 3.3V that means voltage divider connection is correct. In addition to this I tried reducing baud rate but it also didn’t work. I don’t know why it didn’t work. But it should have worked.

Solution for ESP8266 Rx pin:

In my case voltage divider didn’t work and we know Arduino provide 5V if it is connected to USB or 5V power supply. But you can reduce Arduino’s power supply voltage, so it can provide little low voltage than 5V or near to 3.5 V.

1) Send Data from Arduino to ESP8266 NodeMCU:

We have connected DHT22 sensor on Arduino Uno. It will transfer sensor data to NodeMCU via serial communication.

Circuit for Sending data from Arduino to ESP8266 NodeMCU with Voltage Divider:

Send Data from Arduino to ESP8266 NodeMCU With Rx Voltage Divider Connection

Note: Before uploading the code, check you have selected board and port is correct. Remove Rx/Tx pins if connected.

If with voltage divider it doesn’t work. You can try with direct connection Tx/Rx direct connection. But there is risk of damaging your NodeMCU because Arduino provide 5V and NodeMCU needs 3.3V.

Circuit for Sending data from Arduino to ESP8266 NodeMCU Direct Tx & Rx:

Send Data from Arduino to ESP8266 NodeMCU With Direct Rx Tx Connection

Code for Arduino:

Select Arduino Board and Arduino Port before uploading the code.

#include "DHT.h"
#include <SoftwareSerial.h>
#define DHTPIN 2

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

SoftwareSerial espSerial(5, 6);
DHT dht(DHTPIN, DHTTYPE);
String str;

void setup(){
Serial.begin(115200);
espSerial.begin(115200);
dht.begin();
delay(2000);
}
void loop()
{
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
Serial.print("H: ");
Serial.print(h); 
Serial.print("% ");
Serial.print(" T: ");
Serial.print(t); 
Serial.println("C");
str =String("coming from arduino: ")+String("H= ")+String(h)+String("T= ")+String(t);
espSerial.println(str);
delay(1000);
}
Code for ESP8266:

Select NodeMCU 1.0 Board and ESP8266 Port before uploading the code.

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}

void loop() { // run over and over
if (Serial.available()) {
Serial.write(Serial.read());
}
}
Software Serial Library:

The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality. With the help of this library we can allow multiple serial port on arduino.

we are using digital pin 5 & 6 for serial communication.

Result:

Select Serial port from which you want to see data. As we have connected sensor on Arduino, so we can see data on ESP8266 via serial communication. Select ESP8266 port. Open serial monitor. Now you will be able to see data coming from Arduino.

Receiving Data from Arduino to NodeMCU

2) Send Data from ESP8266 NodeMCU to Arduino:

Now change the DHT22 sensor pins to ESP8266. It will transfer sensor data from NodeMCU to Arduino via serial communication.

Circuit for Sending Data from ESP8266 NodeMCU to Arduino:

Send Data from ESP8266 NodeMCU to Arduino Connection

Note: Before uploading the code, check you have selected board and port is correct.

Code for Arduino:

Select Arduino Board and Arduino Port before uploading the code.

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}

void loop() { // run over and over
if (Serial.available()) {
Serial.write(Serial.read());
}
}
Code for ESP8266:

Select NodeMCU 1.0 Board and ESP8266 Port before uploading the code.

#include "DHT.h"
#include <SoftwareSerial.h>
#define DHTPIN 4

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);
String str;

void setup(){
Serial.begin(115200);
Serial1.begin(115200);
dht.begin();
delay(2000);
}
void loop()
{
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
Serial.print("H: ");
Serial.print(h); 
Serial.print("% ");
Serial.print(" T: ");
Serial.print(t); 
Serial.println("C");
str =String("coming from ESP8266: ")+String("H= ")+String(h)+String("T= ")+String(t);
Serial1.println(str);
delay(1000);
}
Serial1 (Only Tx):

Serial1 uses UART1, TX pin is GPIO2. UART1 cannot be used to receive data because normally its RX pin is occupied for flash chip connection. To use Serial1, call Serial1.begin(baudrate).

After uploading program you will see blue led flashes due to data is getting sent on GPIO2(TX).

Result:

Select Serial port from which you want to see data. As we have connected sensor on ESP8266, so we can see data on Arduino via serial communication. Select Arduino port. Open serial monitor. Now you will be able to see data coming from ESP8266.

Receiving Data from NodeMCU to Arduino

Video:

If you like this kind of content please subscribe to my YouTube channel for more updates.