Many beginners struggle when setting up UART on ESP32. Wrong wiring, mismatched baud rates, or confusing code often lead to wasted hours. If you are facing the same issue, this guide will help. In this post, we explain UART on ESP32 with practical examples using both ESP-IDF and Arduino IDE. By the end, you will know how to configure, send, and receive data correctly with ESP32 UART.

What is UART in ESP32?

UART (Universal Asynchronous Receiver/Transmitter) is a simple serial protocol that works without a clock signal. The ESP32 supports three hardware UARTs:

  • UART0 – Used for programming and debugging.

  • UART1 – Available for custom applications.

  • UART2 – Available for custom applications.

Each UART can run up to 5 Mbps, though most projects use 9600 or 115200 baud.


ESP32 UART Pinout

By default, ESP32 UART pins are mapped as:

  • UART0: TX0 (GPIO1), RX0 (GPIO3)

  • UART1: TX1 (GPIO10), RX1 (GPIO9)

  • UART2: TX2 (GPIO17), RX2 (GPIO16)

The ESP32 allows you to remap UART pins using the IO_MUX feature, giving more flexibility.


Why Use UART on ESP32?

  • Connect GPS, GSM, and Bluetooth modules.

  • Debug applications with print logs.

  • Exchange data between two ESP32 boards.

  • Interface with legacy devices using serial communication.


ESP32 UART Setup

Hardware Required

  • ESP32 development board

  • Jumper wires

  • USB cable

  • Optional: UART-based module (GPS, GSM, Arduino, or USB-to-TTL adapter)

Software Required

  • ESP-IDF (for advanced users)

  • Arduino IDE (for beginners)

  • Terminal tool like PuTTY or Serial Monitor


ESP32 UART Example with ESP-IDF

Sending Data




#include"driver/uart.h"#include"string.h"#define UART_PORT UART_NUM_1 #define BUF_SIZE 1024 voidapp_main(void) { uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE }; uart_param_config(UART_PORT, &uart_config); uart_set_pin(UART_PORT, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); uart_driver_install(UART_PORT, BUF_SIZE, 0, 0, NULL, 0); constchar *msg = "Hello from ESP32 UART\n"; while (1) { uart_write_bytes(UART_PORT, msg, strlen(msg)); vTaskDelay(1000 / portTICK_PERIOD_MS); } }

This example configures UART1 on GPIO17 (TX) and GPIO16 (RX) and sends text every second.

Receiving Data




uint8_t data[BUF_SIZE]; int length = 0; uart_get_buffered_data_len(UART_PORT, (size_t*)&length); int read_len = uart_read_bytes(UART_PORT, data, length, 100 / portTICK_PERIOD_MS); if (read_len > 0) { uart_write_bytes(UART_PORT, (constchar*) data, read_len); // echo }

This code reads UART data and echoes it back.


ESP32 UART Example with Arduino IDE




voidsetup() { Serial.begin(115200); // UART0 Serial1.begin(9600, SERIAL_8N1, 16, 17); // UART1 on GPIO16,17 } voidloop() { if (Serial1.available()) { char c = Serial1.read(); Serial.write(c); // echo to Serial Monitor } }

Here, Serial1 is configured on GPIO16 (RX) and GPIO17 (TX).


Common Errors in UART ESP32

  • Wrong baud rate between devices.

  • Incorrect TX/RX pin mapping.

  • Forgetting that TX must connect to RX and RX to TX.

  • Missing USB drivers (CP210x or CH340).


Applications of UART on ESP32

  1. Debugging with serial print.

  2. Communication with GSM/GPS modules.

  3. Peer-to-peer data between ESP32 boards.

  4. Logging sensor data to PC.

  5. Connecting old devices that support UART.


Pros and Cons of ESP32 UART

Pros

  • Reliable communication.

  • Flexible pin remapping.

  • High baud rate support.

Cons

  • UART0 is shared with programming.

  • Needs care when working with 5V modules.


FAQ on UART ESP32

What is UART used for in ESP32?
For serial communication with devices like PCs, GPS, and GSM modules.

How many UARTs does ESP32 have?
Three: UART0, UART1, and UART2.

Can I remap ESP32 UART pins?
Yes, any GPIO can be mapped to UART TX/RX.

Which baud rates are supported?
Up to 5 Mbps, with 9600 and 115200 being common.

Can I use UART with Arduino IDE?
Yes, through Serial, Serial1, and Serial2.

Why is my ESP32 UART not working?
Check wiring, baud rate, and pin assignments.

Does ESP32 support UART DMA?
Yes, for efficient data transfer in ESP-IDF.

Can two ESP32 boards communicate over UART?
Yes, by cross-connecting TX and RX pins.

Will UART interfere with programming?
UART0 is used for programming, so avoid reassigning its pins.

Is UART better than I2C or SPI?
It depends on the use case. UART is simple but point-to-point.


Conclusion

UART on ESP32 makes serial communication easy and reliable. Whether you use ESP-IDF or Arduino IDE, the process is straightforward when you configure baud rate, pins, and wiring correctly. With examples for sending and receiving data, you can start building projects with GPS, GSM, or two ESP32 boards today.

For more tutorials, check ControllersTech and keep learning step by step.