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.

11/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
  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
  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

Post a Comment