{"id":162999,"date":"2024-10-24T11:46:20","date_gmt":"2024-10-24T11:46:20","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=162999"},"modified":"2025-03-24T11:45:53","modified_gmt":"2025-03-24T11:45:53","slug":"esp32-uart-communication-serial-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-uart-communication-serial-arduino\/","title":{"rendered":"ESP32 UART Communication (Serial): Set Pins,  Interfaces, Send and Receive Data (Arduino IDE)"},"content":{"rendered":"\n<p>This is a simple guide about UART serial communication protocol with the ESP32 using Arduino IDE. We&#8217;ll take a look at the basics of UART, default and custom UART pins, basic functions, and communication between boards.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 UART Communication Serial Set Pins,  Interfaces, Send and Receive Data\" class=\"wp-image-163062\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>Throughout this guide, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#intro-uart\" title=\"\">Introducing ESP32 UART Communication Protocol<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#uart-interface\" title=\"\">UART Interface<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-uart-peripherals\" title=\"\">ESP32 UART Peripherals<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-default-uart-pins\" title=\"\">ESP32 Default UART Pins<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#uart0-serial-monitor\" title=\"\">UART0 and the Serial Monitor<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-custom-uart-pins\" title=\"\">ESP32 Setting Custom UART Pins<\/a><\/li>\n\n\n\n<li><a href=\"#uart-other-devices\" title=\"\">UART Communication with Other Devices<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#serial-gps-module\" title=\"\">Serial Communication with a GPS Module<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-serial-communication-between-board\" title=\"\">ESP32 Serial Communication Between Boards (Sender and Receiver)<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>This tutorial focuses on programming the ESP32 using the Arduino core. Before proceeding, you should have the ESP32 Arduino core installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intro-uart\">Introducing ESP32 UART Communication Protocol<\/h2>\n\n\n\n<p>UART (Universal Asynchronous Receiver-Transmitter) is a serial communication protocol that allows two devices to communicate. <\/p>\n\n\n\n<p>Unlike SPI or I2C, which are synchronous, UART is asynchronous, meaning it does not use a clock signal to synchronize the data transmission between devices. However, both devices must agree on the baud rate (speed of transmission).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"746\" height=\"286\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/UART-communication.png?resize=746%2C286&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"UART communication with ESP32 explained\" class=\"wp-image-163047\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/UART-communication.png?w=746&amp;quality=100&amp;strip=all&amp;ssl=1 746w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/UART-communication.png?resize=300%2C115&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 746px) 100vw, 746px\" \/><\/figure><\/div>\n\n\n<p>In UART communication, data is transferred serially, bit by bit (hence the term <strong>serial<\/strong>), at a pre-defined baud rate (bits per second). UART uses a single data line for transmitting (TX) and one for receiving (RX).<\/p>\n\n\n\n<p>UART ports allow us to communicate with other devices, such as other microcontroller boards (an Arduino, an ESP8266, another ESP32 board, or others), the computer, sensors, <a href=\"https:\/\/randomnerdtutorials.com\/esp32-neo-6m-gps-module-arduino\/\" title=\"\">GPS<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/bluetooth-module-hc-06\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Bluetooth modules<\/a>, some types of displays, and more.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"uart-interface\">UART Interface<\/h3>\n\n\n\n<p>For UART communication, you need the following lines:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TX (Transmit)<\/strong>: Sends data.<\/li>\n\n\n\n<li><strong>RX (Receive)<\/strong>: Receives data.<\/li>\n\n\n\n<li><strong>GND<\/strong>: Common ground<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"esp32-uart-peripherals\">ESP32 UART Peripherals<\/h3>\n\n\n\n<p>The ESP32 supports up to three UART interfaces: <strong>UART0<\/strong>, <strong>UART1<\/strong>, and <strong>UART2<\/strong>, depending on the ESP32 board model you\u2019re using.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UART0<\/strong> is usually reserved for communication with the serial monitor during upload and debugging. However, you can also use it for communication with other devices after uploading the code if the Serial Monitor is not needed.<\/li>\n\n\n\n<li><strong>UART1<\/strong> and <strong>UART2<\/strong>: available to communicate with external devices.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"esp32-default-uart-pins\">ESP32 Default UART Pins<\/h3>\n\n\n\n<p>Like I2C and SPI, these UART pins can be mapped to any GPIO pin on the ESP32. However, they have a default pin assignment on most board models.<\/p>\n\n\n\n<p>For most ESP32 boards the UART pin assignment is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>UART Port<\/strong><\/td><td><strong>TX<\/strong><\/td><td><strong>RX<\/strong><\/td><td><strong>Remarks<\/strong><\/td><\/tr><tr><td><strong>UART0<\/strong><\/td><td>GPIO 1<\/td><td>GPIO 3<\/td><td>Used for Serial Monitor and uploading code; Can be assigned to other GPIOs;<\/td><\/tr><tr><td><strong>UART1<\/strong><\/td><td>GPIO 10<\/td><td>GPIO 9<\/td><td><span style=\"text-decoration: underline;\">Must <\/span>be assigned to other GPIOs<\/td><\/tr><tr><td><strong>UART2<\/strong><\/td><td>GPIO 17<\/td><td>GPIO 16<\/td><td>Can be assigned to other GPIOs<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>About UART1 (GPIO 9 and GPIO10)<\/strong> \u2013 these GPIOs are connected to the ESP32 SPI flash memory, so you can\u2019t use them like that. To use UART1 to communicate with other devices, you must define different pins using the <span class=\"rnthl rntliteral\">HardwareSerial<\/span> library.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/esp32-s3-devkitc-pinout-guide\/\" title=\"\">If you&#8217;re using an ESP32-S3<\/a>, the assignment is completely different. The following table shows the default UART0, UART1, and UART2 RX and TX pins for the <strong>ESP32-S3<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>UART Port<\/strong><\/td><td><strong>TX<\/strong><\/td><td><strong>RX<\/strong><\/td><td><strong>Remarks<\/strong><\/td><\/tr><tr><td><strong>UART0<\/strong><\/td><td>GPIO 43<\/td><td>GPIO 44<\/td><td>Cannot be changed<\/td><\/tr><tr><td><strong>UART1<\/strong><\/td><td>GPIO 17<\/td><td>GPIO 18<\/td><td>Can be assigned to other GPIOs<\/td><\/tr><tr><td><strong>UART2<\/strong><\/td><td>&#8212;<\/td><td>&#8212;<\/td><td>Assign any pins of your choice<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Note<\/strong>: Depending on the board, the default UART pins might be different. Always check the pinout for your specific board. You can also reassign UART pins in code if your board doesn&#8217;t have them pre-assigned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"uart0-serial-monitor\">UART0 and the Serial Monitor<\/h2>\n\n\n\n<p>In most IDEs like the<strong> <\/strong>Arduino IDE or PlatformIO, the Serial Monitor interacts with the ESP32 over a USB connection. This is internally mapped to UART0 on the ESP32, so when you open the Serial Monitor, you are using UART communication to send and receive messages.<\/p>\n\n\n\n<p><strong>Note:<\/strong> you can use UART0 to communicate with other devices after uploading code to the board if you don&#8217;t use the Serial Monitor.<\/p>\n\n\n\n<p>Here&#8217;s a basic example that shows a two-way communication between the ESP32 and the Serial Monitor (your computer).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp32-uart-communication-serial-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\nString receivedMessage = &quot;&quot;;  \/\/ Variable to store the complete message\n\nvoid setup() {\n  \/\/ Start the Serial Monitor at a baud rate of 115200\n  Serial.begin(115200);\n  \n  \/\/ Print an initial message to the Serial Monitor\n  Serial.println(&quot;ESP32 is ready. Please enter a message:&quot;);\n}\n\nvoid loop() {\n  \/\/ Check if data is available in the Serial buffer\n  while (Serial.available()) {\n    char incomingChar = Serial.read();  \/\/ Read each character from the buffer\n    \n    if (incomingChar == '\\n') {  \/\/ Check if the user pressed Enter (new line character)\n      \/\/ Print the message\n      Serial.print(&quot;You sent: &quot;);\n      Serial.println(receivedMessage);\n      \n      \/\/ Clear the message buffer for the next input\n      receivedMessage = &quot;&quot;;\n    } else {\n      \/\/ Append the character to the message string\n      receivedMessage += incomingChar;\n    }\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/UART\/UART0_Serial_Monitor.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>When you use <span class=\"rnthl rntliteral\">Serial.begin(115200)<\/span> you are initializing a serial communication using UART0 at a 115200 baud rate.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>To write data to the Serial Monitor, you use the <span class=\"rnthl rntliteral\">print()<\/span> or <span class=\"rnthl rntliteral\">println()<\/span> methods on the <span class=\"rnthl rntliteral\">Serial<\/span> instance.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.println(\"ESP32 is ready. Please enter a message:\");<\/code><\/pre>\n\n\n\n<p>You can also send data from the Serial Monitor to the ESP32. To read that data, you can use the <span class=\"rnthl rntliteral\">read()<\/span> method. To check if there are any available bytes to read, you can use the <span class=\"rnthl rntliteral\">available()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (Serial.available()) {\n    char incomingData = Serial.read();   \/\/ Read the incoming data from Serial Monitor<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>Upload the code to your ESP32. After uploading, open the Serial Monitor at a baud rate of 115200. Then, press the ESP32 RST button to start running the program.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"41\" height=\"39\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/serial-monitor-logo-arduino-ide-2.png?resize=41%2C39&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Uploading code to ESP32 Arduino IDE\" class=\"wp-image-148549\"\/><\/figure><\/div>\n\n\n<p>It should print a message &#8220;<span class=\"rnthl rntliteral\">ESP32 is ready. Please enter a message.<\/span>&#8220;<\/p>\n\n\n\n<p>There&#8217;s a field on the Serial Monitor where you can write data to send to the ESP32 (highlighted in yellow). Write something on that field and press enter to send it to the ESP32. It will be printed back.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"388\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Testing-the-Serial-Monitor-UART-communication.png?resize=666%2C388&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Testing the Serial Monitor UART communication\" class=\"wp-image-163027\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Testing-the-Serial-Monitor-UART-communication.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Testing-the-Serial-Monitor-UART-communication.png?resize=300%2C175&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp32-custom-uart-pins\">ESP32 Setting Custom UART Pins<\/h2>\n\n\n\n<p>Setting custom UART pins is quite simple. As we&#8217;ve seen previously, the ESP32 has three UART ports you can use: UART0, UART 1 and UART 2.<\/p>\n\n\n\n<p>To use any of those ports, you just need to create an <span class=\"rnthl rntliteral\">HardwareSerial<\/span> instance on the desired UART port. For example, the following line creates a serial instance called <span class=\"rnthl rntliteral\">mySerial<\/span> using UART2.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>HardwareSerial mySerial(2);<\/code><\/pre>\n\n\n\n<p>Then, you can use any pins of your choice. You just need to pass them to the <span class=\"rnthl rntliteral\">begin()<\/span> method like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mySerial.begin(GPS_BAUD, SERIAL_8N1, RX_GPIO, TX_GPIO);<\/code><\/pre>\n\n\n\n<p>In which <span class=\"rnthl rntliteral\">RX_GPIO<\/span> and <span class=\"rnthl rntliteral\">TX_GPIO<\/span> are the UART pins of your choice.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"uart-other-devices\">UART Communication with Other Devices<\/h2>\n\n\n\n<p>Now, let&#8217;s take a look at how to use UART to communicate with other devices. We&#8217;ll see an example to communicate with a GPS module, and an example to communicate with another ESP32.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"serial-gps-module\">Serial Communication with a GPS Module<\/h3>\n\n\n\n<p>To start a UART communication in your ESP32 code, you need to specify the UART port, baud rate, and pin mapping. Here&#8217;s an example of initializing UART communication with a GPS module\u2014this can be applied to other modules or devices.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp32-neo-6m-gps-module-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\n \/\/ Define the RX and TX pins for Serial 2\n#define RXD2 16\n#define TXD2 17\n\n#define GPS_BAUD 9600\n\n\/\/ Create an instance of the HardwareSerial class for Serial 2\nHardwareSerial gpsSerial(2);\n\nvoid setup(){\n  \/\/ Serial Monitor\n  Serial.begin(115200);\n  \n  \/\/ Start Serial 2 with the defined RX and TX pins and a baud rate of 9600\n  gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RXD2, TXD2);\n  Serial.println(&quot;Serial 2 started at 9600 baud rate&quot;);\n}\n\nvoid loop(){\n  while (gpsSerial.available() &gt; 0){\n    \/\/ get the byte data from the GPS\n    char gpsData = gpsSerial.read();\n    Serial.print(gpsData);\n  }\n  delay(1000);\n  Serial.println(&quot;-------------------------------&quot;);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_NEO_6M_GPS_Basic_Serial.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This sketch assumes you are using <span class=\"rnthl rntcyellow\">GPIO 16<\/span> and <span class=\"rnthl rntclgray\">GPIO 17<\/span> as RX and TX serial pins to establish serial communication with the GPS module. If you&#8217;re using other pins you should edit that on the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Define the RX and TX pins for Serial 2\n#define RXD2 16\n#define TXD2 17<\/code><\/pre>\n\n\n\n<p>Then, we define the module baud rate on the following line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define GPS_BAUD 9600<\/code><\/pre>\n\n\n\n<p>We create an instance of the <span class=\"rnthl rntliteral\">HardwareSerial<\/span> to use UART 2 called <span class=\"rnthl rntliteral\">gpsSerial<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Create an instance of the HardwareSerial class for Serial 2\nHardwareSerial gpsSerial(2);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we initiate the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Serial Monitor\nSerial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Next, we initialize a serial communication with the GPS module using the <span class=\"rnthl rntliteral\">begin()<\/span> method on the <span class=\"rnthl rntliteral\">gpsSerial()<\/span> instance.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Start Serial 2 with the defined RX and TX pins and a baud rate of 9600\ngpsSerial.begin(GPS_BAUD, SERIAL_8N1, RXD2, TXD2);\nSerial.println(\"Serial 2 started at 9600 baud rate\");<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">begin()<\/span> method accepts as arguments the baud rate, communication frame format (data, parity and stop bits, <span class=\"rnthl rntliteral\">SERIAL_8N1<\/span> is the default), and RX and TX pins.<\/p>\n\n\n\n<p><span class=\"rnthl rntliteral\">SERIAL_8N1<\/span> means 8 data bits, No parity, 1 stop bit, which is the most commonly used. You can <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/communication\/serial\/begin\/\" target=\"_blank\" rel=\"noopener\" title=\"\">check all the options here<\/a>.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, the code checks if there is GPS data on the serial port with the <span class=\"rnthl rntliteral\">available()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop(){\n  while (gpsSerial.available() &gt; 0){<\/code><\/pre>\n\n\n\n<p>When data is available, we read it using the <span class=\"rnthl rntliteral\">read()<\/span> method and then print it in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop(){<br>  while (gpsSerial.available() &gt; 0){<br>    \/\/ get the byte data from the GPS<br>    char gpsData = gpsSerial.read();<br>    Serial.print(gpsData);<br>  }<br>  delay(1000);<br>  Serial.println(\"-------------------------------\");<br>}<\/code><\/pre>\n\n\n\n<p>For the complete instructions to test this code, make sure to take a look at our GPS Module tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-neo-6m-gps-module-arduino\/\" title=\"\">ESP32 with NEO-6M GPS Module (Arduino IDE)<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp32-serial-communication-between-board\">ESP32 Serial Communication Between Boards (Sender and Receiver)<\/h2>\n\n\n\n<p>If you want to communicate with another ESP32, it is not much different. You basically initialize a serial communication on the desired GPIOs and then use the functions to read and send data via serial.<\/p>\n\n\n\n<p>To show you how this works, we&#8217;ll send data via Serial from one ESP32 board to the other. The <em>ESP32 sender<\/em> will continuously send a message with a counter over UART. The <em>ESP32 receiver<\/em> board receiver will receive the number from the other board.<\/p>\n\n\n\n<p>To test the example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>you need two ESP32 boards;<\/li>\n\n\n\n<li>one board will be the sender and the other will be the receiver<\/li>\n\n\n\n<li>we&#8217;ll use UART1 and we&#8217;ll assign TX to GPIO 19 and RX to GPIO 21 (you can use any other pins)\u2014we&#8217;re not using the default pins to show you how it&#8217;s done;<\/li>\n\n\n\n<li>you need to connect the RX from one board to the TX of the other and connect the GNDs together<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>ESP32 #1<\/strong> (Sender)<\/td><td><strong>ESP32 #2<\/strong> (Receiver)<\/td><\/tr><tr><td>TXD1 (GPIO 19)<\/td><td>RXD1 (GPIO 21)<\/td><\/tr><tr><td>RXD1 (GPIO 21)<\/td><td>TXD1 (GPIO 19)<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1112\" height=\"771\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-communication.png?resize=1112%2C771&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 UART communication between boards\" class=\"wp-image-163046\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-communication.png?w=1112&amp;quality=100&amp;strip=all&amp;ssl=1 1112w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-communication.png?resize=300%2C208&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-communication.png?resize=1024%2C710&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-communication.png?resize=768%2C532&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1112px) 100vw, 1112px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">ESP32 Sender Code<\/h3>\n\n\n\n<p>Here&#8217;s the code for the ESP32 sender. Upload it to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp32-uart-communication-serial-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\n\/\/ Define TX and RX pins for UART (change if needed)\n#define TXD1 19\n#define RXD1 21\n\n\/\/ Use Serial1 for UART communication\nHardwareSerial mySerial(1);\n\nint counter = 0;\n\nvoid setup() {\n  Serial.begin(115200);\n  mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1);  \/\/ UART setup\n  \n  Serial.println(&quot;ESP32 UART Transmitter&quot;);\n}\n\nvoid loop() {\n  \n  \/\/ Send message over UART\n  mySerial.println(String(counter));\n  \n  Serial.println(&quot;Sent: &quot; + String(counter));\n  \n  \/\/ increment the counter\n  counter++;\n  \n  delay(1000); \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/UART\/UART_Sender.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How Does the Code Work?<\/h4>\n\n\n\n<p>Let&#8217;s take a look at how the code works.<\/p>\n\n\n\n<p>Start by defining the TX and RX pins you want to use. We&#8217;re using GPIO19 and GPIO21, but you can use any other pins.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define TXD1 19\n#define RXD1 21<\/code><\/pre>\n\n\n\n<p>We&#8217;re using UART1, but for this example we could also have chosen UART2. To use UART1, we start by creating an <span class=\"rnthl rntliteral\">HardwareSerial<\/span> instance called <span class=\"rnthl rntliteral\">mySerial<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ UART1\nHardwareSerial mySerial(1);<\/code><\/pre>\n\n\n\n<p>Then, we create a variable to hold the number we want to send to the other board called <span class=\"rnthl rntliteral\">counter<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int counter = 0;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we initialize the serial communication on UART1 by calling the <span class=\"rnthl rntliteral\">begin()<\/span> method on the <span class=\"rnthl rntliteral\">mySerial<\/span> object.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1);  \/\/ UART setup<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we send the counter via serial by using the <span class=\"rnthl rntliteral\">println()<\/span> method on our <span class=\"rnthl rntliteral\">mySerial<\/span> object. This is what will send the number to the other board.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mySerial.println(String(counter));<\/code><\/pre>\n\n\n\n<p>Then, we increment the counter on each loop.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ increment the counter\ncounter++;<\/code><\/pre>\n\n\n\n<p>A new message is sent every second, but you can change the delay time if needed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(1000); <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Uploading the Code<\/h4>\n\n\n\n<p>Upload the code to the sender board. After uploading open the serial monitor at a baud rate of 115200. You&#8217;ll see that it will start sending data via UART.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"415\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-transmitter.png?resize=666%2C415&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 UART Sender Demonstration\" class=\"wp-image-163048\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-transmitter.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-transmitter.png?resize=300%2C187&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<p>Now, let&#8217;s prepare the receiver to receive the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP32 Receiver Code<\/h3>\n\n\n\n<p>Here&#8217;s the code for the ESP32 receiver. Upload it to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp32-uart-communication-serial-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\n\/\/ Define TX and RX pins for UART (change if needed)\n#define TXD1 19\n#define RXD1 21\n\n\/\/ Use Serial1 for UART communication\nHardwareSerial mySerial(2);\n\nvoid setup() {\n  Serial.begin(115200);\n  mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1);  \/\/ UART setup\n  \n  Serial.println(&quot;ESP32 UART Receiver&quot;);\n}\n\nvoid loop() {\n  \/\/ Check if data is available to read\n  if (mySerial.available()) {\n    \/\/ Read data and display it\n    String message = mySerial.readStringUntil('\\n');\n    Serial.println(&quot;Received: &quot; + message);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/UART\/UART_Receiver.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How Does the Code Work?<\/h4>\n\n\n\n<p>Setting up the serial connection is the same as we&#8217;ve seen in the previous example. The <span class=\"rnthl rntliteral\">loop()<\/span> is different. In this case, we&#8217;ll listen for incoming data.<\/p>\n\n\n\n<p>Then, to receive data from the other board, we start by checking if there are any available bytes to read.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Check if data is available to read\nif (mySerial.available()) {<\/code><\/pre>\n\n\n\n<p>Then, we read the incoming data and saved it in the message variable using the <span class=\"rnthl rntliteral\">readStringUntil()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String message = mySerial.readStringUntil('\\n');<\/code><\/pre>\n\n\n\n<p>This method reads data from the UART serial buffer as a String, continuing to read characters until it encounters the newline character &#8216;<span class=\"rnthl rntliteral\">\\n<\/span>&#8216; . <\/p>\n\n\n\n<p>The &#8216;<span class=\"rnthl rntliteral\">\\n<\/span>&#8216; is typically used to mark the end of a message or line. When it encounters this character in the incoming data stream, it stops reading and returns the string up to that point.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Upload the Code<\/h4>\n\n\n\n<p>Upload the previous code to the receiver board. Open a Serial Monitor connection with this new board. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-communication-between-two-boards.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 communication via UART\" class=\"wp-image-163052\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-communication-between-two-boards.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-communication-between-two-boards.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The ESP32 sender will continuously send a message with a counter over UART. The ESP32 board receiver will receive the number from the other board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"444\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-receiver.png?resize=666%2C444&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 UART receiver demonstration\" class=\"wp-image-163049\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-receiver.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-receiver.png?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<p>Now you have one ESP32 board sending data to the other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This tutorial was a getting started guide to UART communication with the ESP32. We&#8217;ve seen how UART works, how to use the ESP32 UART ports on the desired pins, and how to send data from one ESP32 to the other.<\/p>\n\n\n\n<p>We hope you found this guide useful.<\/p>\n\n\n\n<p>We have guides for other communication protocols with the ESP32 that you may find useful.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\">ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-spi-communication-arduino\/\">ESP32 SPI Communication: Set Pins, Multiple SPI Bus Interfaces, and Peripherals (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-wireless-communication-protocols\/\">ESP32 Wireless Communication Protocols<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple guide about UART serial communication protocol with the ESP32 using Arduino IDE. We&#8217;ll take a look at the basics of UART, default and custom UART pins, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 UART Communication (Serial): Set Pins,  Interfaces, Send and Receive Data (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-uart-communication-serial-arduino\/#more-162999\" aria-label=\"Read more about ESP32 UART Communication (Serial): Set Pins,  Interfaces, Send and Receive Data (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":163062,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-162999","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-UART-Communication-protocol.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/162999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=162999"}],"version-history":[{"count":17,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/162999\/revisions"}],"predecessor-version":[{"id":168252,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/162999\/revisions\/168252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/163062"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=162999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=162999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=162999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}