{"id":721,"date":"2022-08-24T08:49:56","date_gmt":"2022-08-24T08:49:56","guid":{"rendered":"https:\/\/esp32tutorials.com\/?p=721"},"modified":"2022-08-24T08:49:59","modified_gmt":"2022-08-24T08:49:59","slug":"esp32-uart-tutorial-esp-idf","status":"publish","type":"post","link":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/","title":{"rendered":"ESP32 UART Communication using ESP-IDF"},"content":{"rendered":"\n<p>In this ESP32 ESP-IDF tutorial, we will look at UART communication ports of ESP32. Firstly, we will discuss some important UART driver library provided in ESP-IDF and then demonstrate example projects to transmit and receive data over UART. Through the example project we will learn how configure the UART settings and install UART driver to read\/write using the UART interface. The serial port (UART) are also used to debug and program ESP32 using a USB port.<\/p>\n\n\n\n<p>Before we move ahead, make sure you have the latest version of VS Code installed on your system with the ESP-IDF extension configured.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/esp32tutorials.com\/install-esp32-esp-idf-windows-integrate-vs-code\/\">Install ESP32 ESP-IDF on Windows<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/install-esp32-esp-idf-linux-ubuntu\/\">Install ESP32 ESP-IDF on Linux Ubuntu<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">UART Introduction<\/h2>\n\n\n\n<p>UART communication also known as Universal Asynchronous Receive Transmit or Serial communication is one of the simplest and commonly used communication protocols used between two devices. Data transfer between the two devices is via transmission and receiving lines which are connected with the two devices. Data is transferred in the form of bits from one device to another, bit by bit. One of the key advantages of using this communication protocol is that the two devices which are communicating need not be at the same operating frequency. However, it is necessary to se the same baud rate for both the devices in order for the instructions to be understood by them.<\/p>\n\n\n\n<p>The ESP32 development consists of three UART ports commonly referred as UART0, UART1, and UART2. These three serial interfaces are hardware supported and work at 3.3V TTL level. Each of them exposes 4 pins: RX, TX, RTS and CTS. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp-idf-pwm-library\">ESP32 UART ESP-IDF APIs<\/h2>\n\n\n\n<p>Now let us discuss the UART driver library and its functions provided by ESP-IDF to establish communication between UART devices such as ESP32 and USB-TTL converter. <\/p>\n\n\n\n<p>ESP-IDF provides driver\/uart.h library that is required for UART communication. Let\u2019s show you the steps using the API provided by this library to successfully setup UART communication.<\/p>\n\n\n\n<p>This is the header file that is required to include the UART driver:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">#include \"driver\/uart.h\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Set Communication Parameters<\/h3>\n\n\n\n<p>The first step is to setup the UART communication parameters. These include the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Baud Rate<\/li><li>Data Bits<\/li><li>Parity Control<\/li><li>Number of stop Bits<\/li><li>Hardware Flow Control Mode<\/li><li>Communication Mode<\/li><\/ul>\n\n\n\n<p>There are generally two methods to set these communication methods.<\/p>\n\n\n\n<p>In the first method which is more commonly used, we configure all the parameters in a single step. This is done by first creating a structure <strong>uart_config_t<\/strong> that contains all the definitions of the required parameters. And then calling the function <strong>uart_param_config()<\/strong> which takes in the UART port and the above structure as parameters inside it. This will configure the UART parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">const uart_port_t uart_num = UART_NUM_2;\nuart_config_t uart_config = {\n    .baud_rate = 115200,\n    .data_bits = UART_DATA_8_BITS,\n    .parity = UART_PARITY_DISABLE,\n    .stop_bits = UART_STOP_BITS_1,\n    .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,\n    .rx_flow_ctrl_thresh = 122,\n};\n\/\/ Configure UART parameters\nESP_ERROR_CHECK(uart_param_config(uart_num, &amp;uart_config));<\/code><\/pre>\n\n\n\n<p>The second method involves calling separate functions to configure each of the parameters. The table below shows the parameters and their corresponding configuration functions.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Baud Rate<\/td><td>uart_set_baudrate()<\/td><\/tr><tr><td>Number of Transmitted Bits<\/td><td>uart_set_word_length() selected out of uart_word_length_t<\/td><\/tr><tr><td>Parity Control<\/td><td>uart_set_parity() selected out of uart_parity_t<\/td><\/tr><tr><td>Number of Stop Bits<\/td><td>uart_set_stop_bits() selected out of uart_stop_bits_t<\/td><\/tr><tr><td>Hardware Flow Control Mode<\/td><td>uart_set_hw_flow_ctrl() selected out of uart_hw_flowcontrol_t<\/td><\/tr><tr><td>Communication Mode<\/td><td>uart_set_mode() selected out of uart_mode_t<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Set Communication Pins<\/h3>\n\n\n\n<p>In this step, we assign the physical pins that will allow UART communication to establish between two devices by connecting them. <\/p>\n\n\n\n<p>The <strong>uart_set_pin()<\/strong> function is called to set the TX, RX, RTS and CTS pins which we pass as parameters inside it. This function takes in five parameters. The first parameter is the UART port number. The second parameter is the GPIO pin that we want to set up as the TX pin. The third parameter is the GPIO pin that we want to set up as the RX pin. Likewise, the fourth parameter is the GPIO pin that we want to set up as the RTS pin. Finally, the last parameter is the GPIO pin that we want to set up as the CTS pin. The driver will route the GPIO pins that we specify to the respective TX, RX, RTS and CTS signals.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)<\/code><\/pre>\n\n\n\n<p>In the following function, we are setting GPIO4 as TX, GPIO5 as RX, GPIO18 as RTS and GPIO19 as CTS, for UART2.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 4, 5, 18, 19));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install Driver<\/h3>\n\n\n\n<p>Next, we assign the resources of ESP32 for the UART driver. This stage marks the completion of the UART configuration. We use the function, <strong>uart_driver_install()<\/strong> to assign the necessary internal resources for the UART driver. It takes in five parameters. The first parameter is the UART port number. The rest of the parameters are listed below in order:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>rx_buffer_size (Size of RX ring buffer)<\/li><li>tx_buffer_size (Size of TX ring buffer)<\/li><li>queue_size (UART event queue size)<\/li><li>uart_queue (UART event queue handle)<\/li><li>intr_alloc_flags (Flags to allocate an interrupt)<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t *uart_queue, int intr_alloc_flags)<\/code><\/pre>\n\n\n\n<p>First we setup UART buffered IO with event queue as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">const int uart_buffer_size = (1024 * 2);\nQueueHandle_t uart_queue;<\/code><\/pre>\n\n\n\n<p>Next, we install the driver by using the event queue:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, \\\n                                        uart_buffer_size, 10, &amp;uart_queue, 0));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Send and Receive Data<\/h3>\n\n\n\n<p>This stage denotes the start of the UART operation. The UART controller has a finite state machine commonly known as FSM that controls the UART communication. The FSM takes care of all the process whereby the application just reads and writes data to a specific buffer. Lets look at each of these steps. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Transmission<\/h4>\n\n\n\n<p>To send data serially, we first write the data to the TX FIFO buffer. The FSM then serializes this data and sends it out. We use the function, <strong>uart_write_bytes()<\/strong> to write the data to the TX ring buffer first. When there is available space in the TX FIFO buffer, this data is shifted from the ring buffer to the FIFO buffer. This process occurs behind the scene through an interrupt service routine.<\/p>\n\n\n\n<p>The uart_write_bytes() takes in three parameters. the first parameter is the UART port number. The second parameter is the address of the data buffer and the third parameter is the length of the data to be sent. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)<\/code><\/pre>\n\n\n\n<p>Below you can view the code which shows how to use of this function to transmit the message &#8216;Welcome to esp32tutorials.&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">char* message = \"Welcome to esp32tutorials\\n\";\nuart_write_bytes(uart_num, (const char*)message, strlen(message));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reception<\/h4>\n\n\n\n<p>Likewise, to receive the data serially, the FSM first processes this incoming data and parallelizes it. It then writes the data to the RX FIFO buffer. Finally, this data is read from the RX FIFO buffer. To red the data already present in the RX FIFO buffer, we use the function <strong>uart_read_bytes()<\/strong>. This function takes in four parameters. The first parameter is the UART port number. The second parameter is the pointer to the buffer. The third parameter is the length of the data and the last parameter is the count in RTOS ticks.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">int uart_read_bytes(uart_port_t uart_num, void *buf, uint32_t length, TickType_t ticks_to_wait)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp-idf-i2c-example\"><strong>ESP-IDF UART Echo Example<\/strong> ESP-IDF<\/h2>\n\n\n\n<p>In this section, we will build and test a project using the UART driver described in the previously. We will use the UART Echo example provided by ESP-IDF. Through this example we will show you how to use UART interfaces. For demonstration, the data received on the configured UART will be echoed back to the sender.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Example Project<\/h3>\n\n\n\n<p>Open your VS Code and head over to&nbsp;<strong>View &gt; Command Palette<\/strong>. Type&nbsp;<strong>ESP-IDF: New Project<\/strong>&nbsp;in the search bar and press enter.<\/p>\n\n\n\n<p>Specify the project name and directory. We have named our project &#8216;ESP_IDF_UART_ECHO.\u2019 For the ESP-IDF board, we have chosen the custom board option. For ESP-IDF target, we have chosen ESP32 module. Click \u2018Choose Template\u2019 button to proceed forward.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"636\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?resize=1024%2C636&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project 1\" class=\"wp-image-751\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?resize=1024%2C636&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?resize=300%2C186&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?resize=768%2C477&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?w=1234&amp;ssl=1 1234w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>In the Extension, select ESP-IDF option:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1347\" height=\"549\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=1024%2C417&amp;ssl=1\" alt=\"ESP-IDF in VS Code New Project 2\" class=\"wp-image-78\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?w=1347&amp;ssl=1 1347w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=300%2C122&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=1024%2C417&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=768%2C313&amp;ssl=1 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n<\/div>\n\n\n<p>We will click the \u2018<strong>softAP<\/strong>\u2019 under the uart tab. Now click \u2018<strong>Create project using template uart_echo<\/strong>.&#8217;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"383\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-2.jpg?resize=1024%2C383&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project 2\" class=\"wp-image-752\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-2.jpg?resize=1024%2C383&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-2.jpg?resize=300%2C112&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-2.jpg?resize=768%2C287&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-2.jpg?w=1318&amp;ssl=1 1318w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>You will get a notification that the project has been created. To open the project in a new window, click \u2018Yes.\u2019<\/p>\n\n\n\n<p>This opens our ESP_IDF_UART_ECHO project that we created, inside the EXPLORER tab. There are several folders inside our project folder. This is the same for every project which you will create through ESP-IDF Explorer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP-IDF SDK Configuration Editor<\/h3>\n\n\n\n<p>Lets first head over to the <strong>menuconfig<\/strong>. Click the icon shown below. It opens the ESP-IDF SDK Configuration Editor. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-1.jpg?resize=1024%2C640&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project Menuconfig 1\" class=\"wp-image-754\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-1.jpg?resize=1024%2C640&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-1.jpg?resize=300%2C188&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-1.jpg?resize=768%2C480&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-1.jpg?w=1187&amp;ssl=1 1187w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>Scroll down and open the <strong>Echo Example Configuration<\/strong> for our project. Here we can set the UART configuration parameter according to our needs. This includes the UART port number, UART communication speed (baud rate), UART RX pin, UART TX pin and the stack size for the task. Here you can view that by default, ESP-IDF is using UART port number 2 (UART2), baud rate of 115200, RX pin as GPIO5, TX pin as GPIO4 and task stack size as 2048. You can alter these parameters and then click the Save button found at the top. We will leave the configuration settings as default.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-2.jpg?resize=1024%2C585&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project Me\" class=\"wp-image-756\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-2.jpg?resize=1024%2C585&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-2.jpg?resize=300%2C171&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-2.jpg?resize=768%2C439&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Menuconfig-2.jpg?w=1141&amp;ssl=1 1141w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>Lets head over to the main.c file. The main folder contains the source code meaning the main.c file will be found here.<\/p>\n\n\n\n<p>Now go to&nbsp;<strong>main &gt; uart_echo_example_main.c&nbsp;<\/strong>and open it. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"567\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-main.c-file.jpg?resize=1024%2C567&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project main.c file\" class=\"wp-image-761\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-main.c-file.jpg?resize=1024%2C567&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-main.c-file.jpg?resize=300%2C166&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-main.c-file.jpg?resize=768%2C426&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-main.c-file.jpg?w=1301&amp;ssl=1 1301w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>The following code opens up which is shown below. This example code will demonstrate UART Echo. It will echo the received message on UART2 back to the sender.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP-IDF Code: ESP32 UART Echo<\/h3>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">\/* UART Echo Example\n\n   This example code is in the Public Domain (or CC0 licensed, at your option.)\n\n   Unless required by applicable law or agreed to in writing, this\n   software is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR\n   CONDITIONS OF ANY KIND, either express or implied.\n*\/\n#include &lt;stdio.h&gt;\n#include \"freertos\/FreeRTOS.h\"\n#include \"freertos\/task.h\"\n#include \"driver\/uart.h\"\n#include \"driver\/gpio.h\"\n#include \"sdkconfig.h\"\n\n\/**\n * This is an example which echos any data it receives on configured UART back to the sender,\n * with hardware flow control turned off. It does not use UART driver event queue.\n *\n * - Port: configured UART\n * - Receive (Rx) buffer: on\n * - Transmit (Tx) buffer: off\n * - Flow control: off\n * - Event queue: off\n * - Pin assignment: see defines below (See Kconfig)\n *\/\n\n#define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)\n#define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)\n#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)\n#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)\n\n#define ECHO_UART_PORT_NUM      (CONFIG_EXAMPLE_UART_PORT_NUM)\n#define ECHO_UART_BAUD_RATE     (CONFIG_EXAMPLE_UART_BAUD_RATE)\n#define ECHO_TASK_STACK_SIZE    (CONFIG_EXAMPLE_TASK_STACK_SIZE)\n\n#define BUF_SIZE (1024)\n\nstatic void echo_task(void *arg)\n{\n    \/* Configure parameters of an UART driver,\n     * communication pins and install the driver *\/\n    uart_config_t uart_config = {\n        .baud_rate = ECHO_UART_BAUD_RATE,\n        .data_bits = UART_DATA_8_BITS,\n        .parity    = UART_PARITY_DISABLE,\n        .stop_bits = UART_STOP_BITS_1,\n        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,\n        .source_clk = UART_SCLK_APB,\n    };\n    int intr_alloc_flags = 0;\n\n#if CONFIG_UART_ISR_IN_IRAM\n    intr_alloc_flags = ESP_INTR_FLAG_IRAM;\n#endif\n\n    ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));\n    ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &amp;uart_config));\n    ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));\n\n    \/\/ Configure a temporary buffer for the incoming data\n    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);\n\n    while (1) {\n        \/\/ Read data from the UART\n        int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 \/ portTICK_RATE_MS);\n        \/\/ Write data back to the UART\n        uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);\n    }\n}\n\nvoid app_main(void)\n{\n    xTaskCreate(echo_task, \"uart_echo_task\", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works?<\/h3>\n\n\n\n<p>Firstly, we will start by including the necessary libraries that includes the FreeRTOS libraries to generate delays and create tasks, driver\/gpio.h as we have to assign signals of a UART peripheral to ESP32 GPIO pins and driver\/uart.h for the UART communication.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">#include &lt;stdio.h&gt;\n#include \"freertos\/FreeRTOS.h\"\n#include \"freertos\/task.h\"\n#include \"driver\/uart.h\"\n#include \"driver\/gpio.h\"\n#include \"sdkconfig.h\"<\/code><\/pre>\n\n\n\n<p>Then we have defined several variables including the RX, TX, RTS and CTS pins, baud rate, UAT port number and task stack size. These variables will access the values that we set in the menuconfig previously so the values are already defined.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">#define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)\n#define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)\n#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)\n#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)\n\n#define ECHO_UART_PORT_NUM      (CONFIG_EXAMPLE_UART_PORT_NUM)\n#define ECHO_UART_BAUD_RATE     (CONFIG_EXAMPLE_UART_BAUD_RATE)\n#define ECHO_TASK_STACK_SIZE    (CONFIG_EXAMPLE_TASK_STACK_SIZE)<\/code><\/pre>\n\n\n\n<p>Moreover, we will define the Buffer size in this code. It is 1024 bytes or approximately 1 Kilobyte.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">#define BUF_SIZE (1024)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">echo_task()<\/h4>\n\n\n\n<p>Next we have the function echo_task(). It consists of a <strong>uart_config_t <\/strong>structure that contains all the necessary parameters for UART communication. It will be later on passed as a parameter inside the <strong>uart_param_confg()<\/strong> function, when we configure the UART communication parameters. The configuration used is 8 data bits, no parity bit and 1 stop bit (8-N-1) with the baud rate of 115200. Moreover, the UART source clock is set to APB.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">static void echo_task(void *arg)\n{\n    uart_config_t uart_config = {\n        .baud_rate = ECHO_UART_BAUD_RATE,\n        .data_bits = UART_DATA_8_BITS,\n        .parity    = UART_PARITY_DISABLE,\n        .stop_bits = UART_STOP_BITS_1,\n        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,\n        .source_clk = UART_SCLK_APB,\n    };<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Install Driver<\/h4>\n\n\n\n<p>Next, we call the uart_driver_install() function to install the UART driver. It takes in six parameters as described below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>uart_port_t<\/strong>: This is the UART port number that we set up in menuconfig. We are using the default settings hence it is UART2 in our case.<\/li><li><strong>int rx_buffer_size<\/strong>: This is the size of the buffer which we defined as 1024 bytes multiplied by 2. <\/li><li><strong>int tx_buffer_size<\/strong>: This is the size of the TX buffer which is set to 0 in our case. This means that until all of the data is sent out, the driver will not use this buffer.<\/li><li><strong>int queue_size<\/strong>: This is the UART event queue size which is set to 0 as no queues are being used.<\/li><li><strong>QueueHandle_t *uart_queue<\/strong>: This is the UART event queue handle which is NULL in our case due to presence of no queues in this example.<\/li><li><strong>int intr_alloc_flags<\/strong>: This is the flag that is assigned to an interrupt. As we are not using interrupt in this example, hence it is set as NULL.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure UART Parameters<\/h4>\n\n\n\n<p>Next, we call the uart_param_config() function, to set the UART configuration parameters. It takes in two parameters. The first parameter is the UART port number and the second parameter is the UART parameter settings that we setup in the echo_task() function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &amp;uart_config));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Set Communication Pins<\/h4>\n\n\n\n<p>To set the UART communication (TX, RX, RTS and CTS) pins, we will use the uart_set_pin() function and specify the GPIOs as parameters inside it. In our case, the driver will allocate signals of TX, RX, RTS and CTS to the corresponding GPIO pins that we set respectively. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));<\/code><\/pre>\n\n\n\n<p>We will create a temporary buffer for the arriving data by allocating dynamic memory.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">uint8_t *data = (uint8_t *) malloc(BUF_SIZE);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Running UART Communication<\/h4>\n\n\n\n<p>Inside the while loop we will read data from UART2 by calling the uart_read_bytes() function and save it in the int variable &#8216;len.&#8217; Then we will write this data (len) back to the UART using uart_write_bytes().<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">while (1) {\n        int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 \/ portTICK_RATE_MS);\n        uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);\n    }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">app_main()<\/h4>\n\n\n\n<p>Inside the main() function, we will create the task. To create a task, use the function&nbsp;<strong>xTaskCreate()<\/strong>. This function takes in several arguments.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The first argument is the name of the function. In our case, we have set it to echo_task.<\/li><li>The second argument is the name of the task for descriptive purposes. In our case we set the second argument as \u201cuart_echo_task.\u201d<\/li><li>The third argument specifies the stack size of the task. This indicates the amount of memory we want to reserve for the particular task. We have set it to \u20182048\u2019 which was the default value.<\/li><li>The fourth argument is the parameter. It is a value that is passed as the parameter to the created task. This can be used to specify a variable that is being used in the main function and whose value needs to be added to the task. In our case, we will set this argument as NULL which indicates that we are not using this property.<\/li><li>The fifth argument is the priority of the task. We have set it to \u201910.\u2019<\/li><li>The last argument is the handle which is used to change the function of the task eg. suspend, delete, resume, get or set a config of task. This works as a pointer therefore the ampersand symbol is used with it. It is optional so we can also set it to NULL. In our case we have set it to NULL.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">void app_main(void)\n{\n    xTaskCreate(echo_task, \"uart_echo_task\", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compiling the Sketch<\/h3>\n\n\n\n<p>To flash your chip, type the following command in the serial terminal. Remember to replace the COM port with the one through which your board is connected.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">idf.py -p COMX flash monitor<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"644\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-flash-chip.jpg?resize=1024%2C644&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project flash chip\" class=\"wp-image-781\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-flash-chip.jpg?resize=1024%2C644&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-flash-chip.jpg?resize=300%2C189&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-flash-chip.jpg?resize=768%2C483&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-flash-chip.jpg?w=1198&amp;ssl=1 1198w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>After the code flashes successfully, connect your ESP32 with a USB to TTL converter. Use the following connections to connect both the devices together.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>USB-TTL converter<\/td><td>ESP32<\/td><\/tr><tr><td>3.3V<\/td><td>3.3V<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>RX<\/td><td>GPIO4 (set up as TX)<\/td><\/tr><tr><td>TX<\/td><td>GPIO5 (set up as RX)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After connecting the two devices together, plug the USB to TTL converter in your system. Now open the <strong>Device Manager &gt; Ports<\/strong> and check the COM port to which it is connected. Note that it will be a different COM port than the one through which the ESP32 board is currently connected with. In our case the ESP32 is connected with COM5 and the USB to TTL converter is connected with COM14.<\/p>\n\n\n\n<p>After finding out the port, you will have to open a serial terminal. We are using Putty. Set the connection type as serial, specify the COM port number and the baud rate. Then click Open.<\/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=\"640\" height=\"583\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Serial-Terminal-1.jpg?resize=640%2C583&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project Serial Terminal 1\" class=\"wp-image-783\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Serial-Terminal-1.jpg?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Serial-Terminal-1.jpg?resize=300%2C273&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n\n\n<p>The serial terminal will open for COM14. Now type any character from your keyboard and you will be able to view it in the terminal. If you disconnect TX or RX pin, the characters will stop printing. This suggests that the echo is coming from the ESP32 board. Here we sent the message &#8216;UART Echo Testing&#8217;. <\/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=\"653\" height=\"291\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Serial-Terminal-2.jpg?resize=653%2C291&#038;ssl=1\" alt=\"ESP32 UART Echo ESP-IDF Project Serial Terminal 2\" class=\"wp-image-784\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Serial-Terminal-2.jpg?w=653&amp;ssl=1 653w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-Serial-Terminal-2.jpg?resize=300%2C134&amp;ssl=1 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"esp-idf-i2c-example\"><strong>ESP-IDF UART Asynchronous Rx Tx Tasks Example<\/strong> ESP-IDF<\/h2>\n\n\n\n<p>In this section, we will build and test another example project <strong>uart_async_rxtxtasks<\/strong> provided by ESP-IDF for UART. Through this example we will demonstrate two asynchronous tasks that use the same UART interface for the communication to take place. This example creates two FreeRTOS where the first task transmits a message &#8216;Hello world&#8217; to the computer through UART after every 2 seconds. Whereas the second task reads the received data from UART and prints it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Example Project<\/h3>\n\n\n\n<p>Open your VS Code and head over to&nbsp;<strong>View &gt; Command Palette<\/strong>. Type&nbsp;<strong>ESP-IDF: New Project<\/strong>&nbsp;in the search bar and press enter.<\/p>\n\n\n\n<p>Specify the project name and directory. We have named our project &#8216;ESP_IDF_UART_RxTxTasks.\u2019 For the ESP-IDF board, we have chosen the custom board option. For ESP-IDF target, we have chosen ESP32 module. Click \u2018Choose Template\u2019 button to proceed forward.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"636\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-1.jpg?resize=1024%2C636&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project 1\" class=\"wp-image-791\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-1.jpg?resize=1024%2C636&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-1.jpg?resize=300%2C186&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-1.jpg?resize=768%2C477&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-1.jpg?w=1238&amp;ssl=1 1238w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>In the Extension, select ESP-IDF option:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1347\" height=\"549\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=1024%2C417&amp;ssl=1\" alt=\"ESP-IDF in VS Code New Project 2\" class=\"wp-image-78\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?w=1347&amp;ssl=1 1347w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=300%2C122&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=1024%2C417&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/06\/ESP-IDF-in-VS-Code-New-Project-2.jpg?resize=768%2C313&amp;ssl=1 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n<\/div>\n\n\n<p>We will click the \u2018<strong>uart_async_rxtxtasks<\/strong>\u2019 under the uart tab. Now click \u2018<strong>Create project using template uart_async_rxtxtasks<\/strong>.&#8217;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"372\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-2.jpg?resize=1024%2C372&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project 2\" class=\"wp-image-793\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-2.jpg?resize=1024%2C372&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-2.jpg?resize=300%2C109&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-2.jpg?resize=768%2C279&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-2.jpg?w=1350&amp;ssl=1 1350w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>You will get a notification that the project has been created. To open the project in a new window, click \u2018Yes.\u2019<\/p>\n\n\n\n<p>This opens our ESP_IDF_UART_RxTxTasks project that we created, inside the EXPLORER tab. Now go to&nbsp;<strong>main &gt; uart_async_rxtxtasks_main.c&nbsp;<\/strong>and open it. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"684\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-3.jpg?resize=1024%2C684&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project 3\" class=\"wp-image-797\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-3.jpg?resize=1024%2C684&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-3.jpg?resize=300%2C200&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-3.jpg?resize=768%2C513&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-3.jpg?w=1129&amp;ssl=1 1129w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>The following code opens up which is shown below. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP-IDF Code: ESP32 UART Async Rx Tx Tasks<\/h3>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">\/* UART asynchronous example, that uses separate RX and TX tasks\n\n   This example code is in the Public Domain (or CC0 licensed, at your option.)\n\n   Unless required by applicable law or agreed to in writing, this\n   software is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR\n   CONDITIONS OF ANY KIND, either express or implied.\n*\/\n#include \"freertos\/FreeRTOS.h\"\n#include \"freertos\/task.h\"\n#include \"esp_system.h\"\n#include \"esp_log.h\"\n#include \"driver\/uart.h\"\n#include \"string.h\"\n#include \"driver\/gpio.h\"\n\nstatic const int RX_BUF_SIZE = 1024;\n\n#define TXD_PIN (GPIO_NUM_4)\n#define RXD_PIN (GPIO_NUM_5)\n\nvoid init(void) {\n    const uart_config_t uart_config = {\n        .baud_rate = 115200,\n        .data_bits = UART_DATA_8_BITS,\n        .parity = UART_PARITY_DISABLE,\n        .stop_bits = UART_STOP_BITS_1,\n        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,\n        .source_clk = UART_SCLK_APB,\n    };\n    \/\/ We won't use a buffer for sending data.\n    uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);\n    uart_param_config(UART_NUM_1, &amp;uart_config);\n    uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);\n}\n\nint sendData(const char* logName, const char* data)\n{\n    const int len = strlen(data);\n    const int txBytes = uart_write_bytes(UART_NUM_1, data, len);\n    ESP_LOGI(logName, \"Wrote %d bytes\", txBytes);\n    return txBytes;\n}\n\nstatic void tx_task(void *arg)\n{\n    static const char *TX_TASK_TAG = \"TX_TASK\";\n    esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);\n    while (1) {\n        sendData(TX_TASK_TAG, \"Hello world\");\n        vTaskDelay(2000 \/ portTICK_PERIOD_MS);\n    }\n}\n\nstatic void rx_task(void *arg)\n{\n    static const char *RX_TASK_TAG = \"RX_TASK\";\n    esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);\n    uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1);\n    while (1) {\n        const int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 1000 \/ portTICK_RATE_MS);\n        if (rxBytes &gt; 0) {\n            data[rxBytes] = 0;\n            ESP_LOGI(RX_TASK_TAG, \"Read %d bytes: '%s'\", rxBytes, data);\n            ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);\n        }\n    }\n    free(data);\n}\n\nvoid app_main(void)\n{\n    init();\n    xTaskCreate(rx_task, \"uart_rx_task\", 1024*2, NULL, configMAX_PRIORITIES, NULL);\n    xTaskCreate(tx_task, \"uart_tx_task\", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compiling the Sketch<\/h3>\n\n\n\n<p>To flash your chip, type the following command in the serial terminal. Remember to replace the COM port with the one through which your board is connected.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code class=\"\">idf.py -p COMX flash monitor<\/code><\/pre>\n\n\n\n<p>After the code flashes successfully, connect your ESP32 with a USB to TTL converter. Use the following connections to connect both the devices together.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>USB-TTL converter<\/td><td>ESP32<\/td><\/tr><tr><td>3.3V<\/td><td>3.3V<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>RX<\/td><td>GPIO4 (set up as TX)<\/td><\/tr><tr><td>TX<\/td><td>GPIO5 (set up as RX)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After connecting the two devices together, plug the USB to TTL converter in your system. Now open the <strong>Device Manager &gt; Ports<\/strong> and check the COM port to which it is connected. Note that it will be a different COM port than the one through which the ESP32 board is currently connected with. <strong>In our case, the ESP32 is connected with COM5 and the USB to TTL converter is connected with COM14.<\/strong><\/p>\n\n\n\n<p>After finding out the port, you will have to open a serial terminal. We are using RealTerm: Serial\/TCP Terminal. You can download this program from this link (<a href=\"https:\/\/sourceforge.net\/projects\/realterm\/\">https:\/\/sourceforge.net\/projects\/realterm\/<\/a>). <\/p>\n\n\n\n<p>Open COM14 and set the baud rate to 115200 and then click open.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"918\" height=\"560\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal.jpg?resize=918%2C560&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project Serial Terminal\" class=\"wp-image-803\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal.jpg?w=918&amp;ssl=1 918w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal.jpg?resize=300%2C183&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal.jpg?resize=768%2C468&amp;ssl=1 768w\" sizes=\"auto, (max-width: 918px) 100vw, 918px\" \/><\/figure>\n\n\n\n<p>The ESP-IDF terminal keeps logging the message &#8216;TX_TASK: Wrote 11 bytes&#8217; which suggests that ESP32 is sending the message via UART after every 2 seconds. The COM14 terminal through which the USB-TTL converter is attached receives the message &#8216;Hello world&#8217; after every 2 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"485\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-1.jpg?resize=1024%2C485&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project Serial Terminal 1\" class=\"wp-image-801\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-1.jpg?resize=1024%2C485&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-1.jpg?resize=300%2C142&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-1.jpg?resize=768%2C364&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-1.jpg?w=1295&amp;ssl=1 1295w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>Now let us send a message from the computer to ESP32. Here we are sending the message &#8216;Sending data to esp32&#8217; from this terminal to ESP32.<\/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=\"962\" height=\"658\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-2.jpg?resize=962%2C658&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project Serial Terminal 2\" class=\"wp-image-805\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-2.jpg?w=962&amp;ssl=1 962w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-2.jpg?resize=300%2C205&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-2.jpg?resize=768%2C525&amp;ssl=1 768w\" sizes=\"auto, (max-width: 962px) 100vw, 962px\" \/><\/figure>\n<\/div>\n\n\n<p>The ESP32 receives the message and logs it in the ESP-IDF terminal as shown below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"377\" src=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-3-1.jpg?resize=1024%2C377&#038;ssl=1\" alt=\"ESP32 UART Async RX TX Tasks ESP-IDF Project Serial Terminal 3\" class=\"wp-image-807\" srcset=\"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-3-1.jpg?resize=1024%2C377&amp;ssl=1 1024w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-3-1.jpg?resize=300%2C110&amp;ssl=1 300w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-3-1.jpg?resize=768%2C283&amp;ssl=1 768w, https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Async-RX-TX-Tasks-ESP-IDF-Project-Serial-Terminal-3-1.jpg?w=1030&amp;ssl=1 1030w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>You may also like to read: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/esp32tutorials.com\/esp32-gpio-esp-idf-led-blinking-example\/\">ESP32 GPIO with ESP-IDF with LED Blinking example<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/esp32-push-button-esp-idf-digital-input\/\">ESP32 Push Button with ESP-IDF (Digital Input)<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/esp32-adc-esp-idf\/\">ESP32 ADC with ESP-IDF Measure Analog Inputs<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/esp32-pwm-esp-idf-led-brightness-control\/\">ESP32 PWM ESP-IDF LED Brightness Control Example<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/i2c-lcd-esp32-esp-idf\/\">I2C LCD with ESP32 using ESP-IDF<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/esp32-esp-idf-connect-wifi-station-mode-example\/\">ESP32 ESP-IDF Connect with WiFi \u2013 Station Mode Example<\/a><\/li><li><a href=\"https:\/\/esp32tutorials.com\/esp32-access-point-ap-esp-idf\/\">ESP32 Set an Access Point (AP) using ESP-IDF<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this ESP32 ESP-IDF tutorial, we will look at UART communication ports of ESP32. Firstly, we will discuss some important UART driver library provided in ESP-IDF and then demonstrate example &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 UART Communication using ESP-IDF\" class=\"read-more button\" href=\"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#more-721\" aria-label=\"Read more about ESP32 UART Communication using ESP-IDF\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"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":[9],"tags":[4,3,7,18],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-esp32-esp-idf-tutorials","tag-esp-idf","tag-esp32","tag-tutorial","tag-uart-communication"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ESP32 UART Communication using ESP-IDF<\/title>\n<meta name=\"description\" content=\"ESP32 UART Communications tutorial using ESP-IDF to transmit and receive data serially over UART ports of ESP32, how to use library to configure ports\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ESP32 UART Communication using ESP-IDF\" \/>\n<meta property=\"og:description\" content=\"ESP32 UART Communications tutorial using ESP-IDF to transmit and receive data serially over UART ports of ESP32, how to use library to configure ports\" \/>\n<meta property=\"og:url\" content=\"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/\" \/>\n<meta property=\"og:site_name\" content=\"ESP32 ESP-IDF\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-24T08:49:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-24T08:49:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1-1024x636.jpg\" \/>\n<meta name=\"author\" content=\"Bilal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bilal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"21 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/\"},\"author\":{\"name\":\"Bilal\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/#\\\/schema\\\/person\\\/d648bc7f340c02d211193ac9efc2e3a5\"},\"headline\":\"ESP32 UART Communication using ESP-IDF\",\"datePublished\":\"2022-08-24T08:49:56+00:00\",\"dateModified\":\"2022-08-24T08:49:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/\"},\"wordCount\":3110,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/#\\\/schema\\\/person\\\/d648bc7f340c02d211193ac9efc2e3a5\"},\"image\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/esp32tutorials.com\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/ESP32-UART-Echo-ESP-IDF-Project-1-1024x636.jpg\",\"keywords\":[\"esp-idf\",\"esp32\",\"tutorial\",\"uart communication\"],\"articleSection\":[\"ESP32 Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/\",\"url\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/\",\"name\":\"ESP32 UART Communication using ESP-IDF\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/esp32tutorials.com\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/ESP32-UART-Echo-ESP-IDF-Project-1-1024x636.jpg\",\"datePublished\":\"2022-08-24T08:49:56+00:00\",\"dateModified\":\"2022-08-24T08:49:59+00:00\",\"description\":\"ESP32 UART Communications tutorial using ESP-IDF to transmit and receive data serially over UART ports of ESP32, how to use library to configure ports\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/esp32tutorials.com\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?fit=1234%2C766&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/esp32tutorials.com\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?fit=1234%2C766&ssl=1\",\"width\":1234,\"height\":766,\"caption\":\"ESP32 UART Echo ESP-IDF Project 1\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/esp32-uart-tutorial-esp-idf\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/esp32tutorials.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ESP32 UART Communication using ESP-IDF\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/#website\",\"url\":\"https:\\\/\\\/esp32tutorials.com\\\/\",\"name\":\"ESP32 ESP-IDF\",\"description\":\"ESP32 ESP IDF Tutorials and Projects\",\"publisher\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/#\\\/schema\\\/person\\\/d648bc7f340c02d211193ac9efc2e3a5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/esp32tutorials.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/#\\\/schema\\\/person\\\/d648bc7f340c02d211193ac9efc2e3a5\",\"name\":\"Bilal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013\",\"url\":\"https:\\\/\\\/esp32tutorials.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013\",\"contentUrl\":\"https:\\\/\\\/esp32tutorials.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013\",\"caption\":\"Bilal\"},\"logo\":{\"@id\":\"https:\\\/\\\/esp32tutorials.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013\"},\"description\":\"We are a team of experienced Embedded Software Developers with skills in developing Embedded Linux, RTOS, and IoT products from scratch to deployment with a demonstrated history of working in the embedded software industry. Contact us for your projects: admin@esp32tutorials.com\",\"sameAs\":[\"http:\\\/\\\/esp32tutorials.com\"],\"url\":\"https:\\\/\\\/esp32tutorials.com\\\/author\\\/esp32tutorialslab\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ESP32 UART Communication using ESP-IDF","description":"ESP32 UART Communications tutorial using ESP-IDF to transmit and receive data serially over UART ports of ESP32, how to use library to configure ports","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/","og_locale":"en_US","og_type":"article","og_title":"ESP32 UART Communication using ESP-IDF","og_description":"ESP32 UART Communications tutorial using ESP-IDF to transmit and receive data serially over UART ports of ESP32, how to use library to configure ports","og_url":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/","og_site_name":"ESP32 ESP-IDF","article_published_time":"2022-08-24T08:49:56+00:00","article_modified_time":"2022-08-24T08:49:59+00:00","og_image":[{"url":"https:\/\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1-1024x636.jpg","type":"","width":"","height":""}],"author":"Bilal","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Bilal","Est. reading time":"21 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#article","isPartOf":{"@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/"},"author":{"name":"Bilal","@id":"https:\/\/esp32tutorials.com\/#\/schema\/person\/d648bc7f340c02d211193ac9efc2e3a5"},"headline":"ESP32 UART Communication using ESP-IDF","datePublished":"2022-08-24T08:49:56+00:00","dateModified":"2022-08-24T08:49:59+00:00","mainEntityOfPage":{"@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/"},"wordCount":3110,"commentCount":6,"publisher":{"@id":"https:\/\/esp32tutorials.com\/#\/schema\/person\/d648bc7f340c02d211193ac9efc2e3a5"},"image":{"@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#primaryimage"},"thumbnailUrl":"https:\/\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1-1024x636.jpg","keywords":["esp-idf","esp32","tutorial","uart communication"],"articleSection":["ESP32 Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/","url":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/","name":"ESP32 UART Communication using ESP-IDF","isPartOf":{"@id":"https:\/\/esp32tutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#primaryimage"},"image":{"@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#primaryimage"},"thumbnailUrl":"https:\/\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1-1024x636.jpg","datePublished":"2022-08-24T08:49:56+00:00","dateModified":"2022-08-24T08:49:59+00:00","description":"ESP32 UART Communications tutorial using ESP-IDF to transmit and receive data serially over UART ports of ESP32, how to use library to configure ports","breadcrumb":{"@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#primaryimage","url":"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?fit=1234%2C766&ssl=1","contentUrl":"https:\/\/i0.wp.com\/esp32tutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-UART-Echo-ESP-IDF-Project-1.jpg?fit=1234%2C766&ssl=1","width":1234,"height":766,"caption":"ESP32 UART Echo ESP-IDF Project 1"},{"@type":"BreadcrumbList","@id":"https:\/\/esp32tutorials.com\/esp32-uart-tutorial-esp-idf\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/esp32tutorials.com\/"},{"@type":"ListItem","position":2,"name":"ESP32 UART Communication using ESP-IDF"}]},{"@type":"WebSite","@id":"https:\/\/esp32tutorials.com\/#website","url":"https:\/\/esp32tutorials.com\/","name":"ESP32 ESP-IDF","description":"ESP32 ESP IDF Tutorials and Projects","publisher":{"@id":"https:\/\/esp32tutorials.com\/#\/schema\/person\/d648bc7f340c02d211193ac9efc2e3a5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/esp32tutorials.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/esp32tutorials.com\/#\/schema\/person\/d648bc7f340c02d211193ac9efc2e3a5","name":"Bilal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/esp32tutorials.com\/wp-content\/litespeed\/avatar\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013","url":"https:\/\/esp32tutorials.com\/wp-content\/litespeed\/avatar\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013","contentUrl":"https:\/\/esp32tutorials.com\/wp-content\/litespeed\/avatar\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013","caption":"Bilal"},"logo":{"@id":"https:\/\/esp32tutorials.com\/wp-content\/litespeed\/avatar\/b623b0da24dec30042f1540f5437f129.jpg?ver=1776224013"},"description":"We are a team of experienced Embedded Software Developers with skills in developing Embedded Linux, RTOS, and IoT products from scratch to deployment with a demonstrated history of working in the embedded software industry. Contact us for your projects: admin@esp32tutorials.com","sameAs":["http:\/\/esp32tutorials.com"],"url":"https:\/\/esp32tutorials.com\/author\/esp32tutorialslab\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/posts\/721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":0,"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esp32tutorials.com\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}