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.

30/Post a Comment/Comments

  1. Thanks for sharing.
    What about voltages?
    If I remember well, Arduino is working at 5V, while ESP32 is working at 3.3V.
    I think you are risking to burn your ESP32.
    Wouldn't be better to use a bi-directional voltage level shifter module?
    This, for example: https://www.sparkfun.com/products/12009

    Thanks
    Daniele

    ReplyDelete
    Replies
    1. Yes, to exchange data between arduino and esp32, we should use voltage level shifter as esp32 works at 3.3v and arduino at 5volts. But in this cause the direct setup works fine. You can simply make a voltage level shifter by yourself using resistors. Thanks, and stay connected.

      Delete
  2. es grande maravilloso genial diferentes comunicaciones de datos, la comunicación de datos en serie es un protocolo de comunicación muy popular. La comunicación de datos entre ESP32

    ReplyDelete
    Replies
    1. Thanks for your comment. The serial communication is really easy and popular.

      Delete
  3. There are so many sites, books, and other options for learning programming that it can be overwhelming for even the most determined learners. JetBrains Academy removes this issue by providing a holistic approach to learning with a full hands-on programming experience. It allows you to study theory and immediately put it into practice as you create applications step by step. Whether you want to learn Java, Python, Kotlin, or frontend development, you’ll get the advantage of:

    Numerous projects that can strengthen your GitHub profile.
    Personalized study plan to keep you on track and focused on your goal.
    Integration with JetBrains IDEs to learn programming in a professional development environment.
    Community of 330,000+ learners that are always there for you whenever you are stuck or need motivation.

    ReplyDelete
  4. 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

    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.

    ReplyDelete
  5. Both the Arduino and the esp32 supports serial communication i.e. UART(Universal Asynchronous Reception and Transmission)

    we use two pins i.e. Tx and Rx for the serial communication.

    There are Tx and Rx pins in both the micro-controller boards and if you are using the pre defined pins then this mode of communication is called Hardware serial communication. You need to connect the Tx of Arduino to the Rx of ESP32 and Rx of Arduino to the Tx of ESP32.

    After that you can use serial communication to make these device to talk to each other. You can use Serial.println(); to send the command and Serial.readstringuntil(’\r’); to read.

    You can also use Digital GPIOs pins for serial communication, in that case you need to define 2 Digital pins as the Tx and Rx in both the micro-controllers. This mode is called software serial mode of communication, there are different libraries but the simplest one is SoftwareSerial.h. you can use this one for software serial communication.

    then you need to define the object then it will work same as the hardware serial communication and that too with same functions

    ReplyDelete
  6. What if I want to send gas sensor data from arduino uno to ESP32? So far my code in arduino is:

    Serial.print("LPG: ");
    Serial.print(ppm);
    Serial.print( "ppm" );
    Serial.print("\n");

    how would I get the ppm value in my ESP32 instead of the string?

    ReplyDelete
    Replies
    1. You have to listen to the serial input in esp32 side as char. Then you have to convert the received message to desire format. Thanks.

      Delete
    2. listen to the serial input in esp32 side. Just serial read the incoming data in the RX.

      Delete
  7. Hi sir , this is was a very helpful video . I have a small doubt to ask , that is can I do serial communication between arduino and esp32 cam module . If yes can you help me out regarding the code to write on both the boards. Like, if I push an analog button connected to Arduino an led connected to ESP32-CAM should glow
    .

    ReplyDelete
    Replies
    1. As shown in the example of this post, you can easily do serial communication between Arduino and ESP32 cam module. The process is same. Just receive the incoming commands in ESP side. You can also use a library called SerialTransfer. I didn't tried this library yet, but I guess it will be helpful. Here is the git link.
      https://github.com/PowerBroker2/SerialTransfer
      Thanks and stay connected.

      Delete
  8. How to send data ESP32 to Arduino?

    ReplyDelete
    Replies
    1. Thanks for your comment. To send data from esp32 to Arduino, you have to do the same thing. Just use UART communication and print something on serial port. The connection is the same.

      Delete
  9. Gracias por el aporte, Una consulta estoy intentando que el esp32 envié datos al arduino pero no logra llegar datos al arduino, se debe hacer alguna configuración?

    ReplyDelete
    Replies
    1. The both way communication should work. Please check tx rx connection and try again. Thanks

      Delete
  10. Thank for sharing this blog
    I tried you codes and did the direct connect between the ESP32 DevkitC V4 and arduino mega, but i didn't received any data. is that compulsory in my case to use a level shifter to acheive a UART between ESP32 DevkitC V4 and arduino.
    Please let me know...

    ReplyDelete
    Replies
    1. You know, we are communicating between two different logic-level controllers. It's good practice to use a voltage shifter between Arduino Tx to ESP32 RX. A voltage divider can be a simple solution. A 10K resistor between ESP32’s Rx and Arduino’s TX, and 20K between ESP32’s Rx and GND will work fine.
      In my case, the direct connection worked, but it is not the proper way. Thanks.

      Delete
  11. Can serial communication between an Arduino and ESP32 be used for more than just exchanging data? If so, what else can it be used for?

    ReplyDelete
    Replies
    1. Yes, serial communication between an Arduino and ESP32 can be used for more than just exchanging data. Some other uses include:

      Controlling the ESP32 from the Arduino: The Arduino can send commands to the ESP32 to control its behavior.

      Debugging: Serial communication can be used for debugging purposes, allowing you to send messages between the two boards and see what is going on.

      Firmware updates: The ESP32 can be updated over the air (OTA) using serial communication.

      Data logging: The ESP32 can collect data from various sensors and send it to the Arduino for storage or processing.

      Remote control: The Arduino and ESP32 can communicate with each other over a network using a serial-to-WiFi module, allowing for remote control of devices.

      These are just a few examples of how serial communication between an Arduino and ESP32 can be used for more than just exchanging data.

      Delete
    2. Yes, serial communication between an Arduino and ESP32 can be used for more than just exchanging data. Some other uses include:

      Controlling the ESP32 from the Arduino: The Arduino can send commands to the ESP32 to control its behavior.

      Debugging: Serial communication can be used for debugging purposes, allowing you to send messages between the two boards and see what is happening.

      Firmware updates: Using serial communication, the ESP32 can be updated over the air (OTA).

      Data logging: The ESP32 can collect data from various sensors and send it to the Arduino for storage or processing.

      Remote control: The Arduino and ESP32 can communicate with each other over a network using a serial-to-WiFi module, allowing for remote control of devices.

      These are just a few examples of how serial communication between an Arduino and ESP32 can be used for more than just exchanging data.

      Delete
  12. Hello Sir/Mam
    I am facing a problem while uploading the code in Nodemcu, like 'Serial2' port is not declared in this scope did you mean serial1

    ReplyDelete
    Replies
    1. In which board you are trying to upload the code?

      Delete
  13. I lost whole day but could not succeed in sending AT command using SoftwareSerial, I have ESP32 Dev Unit
    SoftwareSerial lora(16, 17);
    const int NETWORKID_LORA=5; //enter Lora Network ID
    const int ADDRESS_LORA_NODE_GATEWAY=1;

    lora.begin(115200); // default baud rate of module is 115200
    delay(1000); // wait for LoRa module to be ready
    lora.print((String)”AT+ADDRESS=” + ADDRESS_LORA_NODE_GATEWAY + “\r\n”);
    delay(200);
    lora.print((String)”AT+NETWORKID=” + NETWORKID_LORA + “\r\n”);
    delay(200);
    Please help

    ReplyDelete
  14. hi ! i'm facing a problem while uploading the code in Arduino IDE, error says : 'Serial2' port is not declared in this scope. i'm trying to upload on an ESP32c3 dev module (working with an arduino méga, the goal of my project is to create a web page or an app where my esp32c3 will bring the values of some sensors)
    thank you !

    ReplyDelete
    Replies
    1. You have to check that if there any hardware serial available named serial2. Please check the datasheet of your esp32 board.

      Delete
  15. komunikasi data yang sangat beragam dan hebat, komunikasi data secara seri merupakan protokol komunikasi yang sangat populer.terimakasih sudah memberikan kita pengetahuan yang sangat bagus

    ReplyDelete
  16. Compilation error: 'Serial2' was not declared in this scope; did you mean 'Serial1'?

    ReplyDelete
    Replies
    1. which board you are using?
      you have multiple serial communication port in your controller.

      Delete

Post a Comment