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 perform using only a single wire and not more than 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 used as serial pins. 

In Arduino Mega, there are additional three serial pins. That means there is a total of 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 a 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 are going to use Arduino UNO and ESP32 dev module. We are going to use Arduino d1 as TX pin and d0 as RX. In ESP32, GPIO 16 as RX pin and GPIO 17 as TX pin which is marked as RX1 and TX1 pin in ESP32 board.

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

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

0/Post a Comment/Comments