How to Exchange Data between Arduino and ESP32 using Serial Communication?

Serial communication

Serial communication is a process of data transmission sequentially through a communication channel or bus. In serial communication, the data is sent one bit at a time. The serial transmission can be performed using only a single wire and four wires. Serial communication is also known as UART or USART communication.

Serial communication is also known as UART or USART communication
Serial Communication

Arduino Serial Pins:

In every Arduino board, there is at least one serial pin available. The pins are named TX and RX pins. TX stands for transmitting; RX stands for receive. 

In Arduino Uno, there a one TX and one RX pin available. The digital pins 0 and 1 are used as serial pins. 

In Arduino Mega, there are additional three serial pins. That means there are four serial communication pins available in Arduino Mega.

ESP32 Serial pins:

ESP32 boards also come with TX and RX pins. Depending on the model, the number of serial pins may vary.

Communication between Arduino and ESP32:

To establish serial communication between Arduino and ESP32, we can cross-connect TX and RX pins of Arduino with ESP32. There should be common ground. So, we will connect Arduino GND Pin with ESP32 GND Pin

Serial Communication between an ESP32 and an Arduino
Serial Communication between an ESP32 and an Arduino

To exchange data between ESP32 and Arduino, the baud rate should be the same in both programs.

Exchange Data between Arduino and ESP32 using Serial Communication
Exchange Data between Arduino and ESP32 using Serial Communication

In this tutorial, we will use Arduino UNO and ESP32 dev modules. We will use Arduino d1 as TX pin and d0 as RX. In ESP32, GPIO 16 as the RX pin, and GPIO 17 is TX pin, marked as the  RX1 and  TX1 pin in the ESP32 board.

Also read:

Arduino and ESP32 Serial Communication
Arduino and ESP32 Serial Communication

Arduino Code for URT communication:

// this sample code provided by www.programmingboss.com
void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println("Hello Boss");
  delay(1500);
}code-box

ESP32 Code for URT communication:

// this sample code provided by www.programmingboss.com
#define RXp2 16
#define TXp2 17
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
    Serial.println("Message Received: ");
    Serial.println(Serial2.readString());
}code-box

Video Instruction and demonstration

Summary

Serial data communication is a very popular communication protocol among different data communication. Data communication between ESP32 and Arduino can be done by serial communication. Most microcontrollers can transmit and receive data between them using UART communication.