Many beginners face problems when they try to use UART on ESP32. Errors in wiring, wrong baud rate, and confusing code can make the process harder than it should be. This often leads to wasted hours without working results. The good news is, learning ESP32 UART with clear examples can save you time and help you build working projects fast. In this tutorial, we explain UART ESP32 basics, ESP32 UART examples, and ESP-IDF UART code step by step.
What is UART in ESP32?
UART stands for Universal Asynchronous Receiver and Transmitter. It is one of the oldest and most reliable communication protocols. The ESP32 chip comes with three hardware UART controllers:
- UART0 – Used by default for programming and debugging.
- UART1 – Free for user applications.
- UART2 – Free for user applications.
Each UART can work up to 5 Mbps, though most projects use standard baud rates like 9600, 115200, or 1 Mbps.
Why Use UART on ESP32?
- Simple serial communication between ESP32 and PC.
- Connect ESP32 with sensors, GPS modules, or GSM modules.
- Debug code with serial print statements.
- Send and receive data between two ESP32 boards.
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)
But the ESP32 allows pin remapping, which means you can assign TX and RX to almost any GPIO using the IO_MUX feature.
Getting Started with ESP32 UART
Hardware Setup
- ESP32 development board (ESP32-WROOM or ESP32-WROVER).
- USB cable for programming.
- Jumper wires.
- Optional: another UART device (Arduino, GPS module, or USB to TTL adapter).
Software Setup
- Install ESP-IDF (official SDK for ESP32).
- Install Python if not already present.
- Install a terminal program (like PuTTY or Arduino Serial Monitor).
ESP32 UART Example with ESP-IDF
Let’s write a basic UART communication program using ESP-IDF.
Code Example: Sending Data over UART
#include "driver/uart.h"
#include "string.h"
#define UART_PORT UART_NUM_1
#define BUF_SIZE (1024)
void app_main(void) {
const int uart_num = UART_PORT;
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_num, &uart_config);
uart_set_pin(uart_num, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, BUF_SIZE, 0, 0, NULL, 0);
const char *test_str = "ESP32 UART Example\n";
while (1) {
uart_write_bytes(uart_num, test_str, strlen(test_str));
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
This program configures UART1 on pins GPIO17 (TX) and GPIO16 (RX), sets the baud rate to 115200, and sends a message every second.
ESP32 UART Example: Receiving Data
Receiving data is just as important as sending it.
uint8_t data[BUF_SIZE];
int length = 0;
ESP_ERROR_CHECK(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, (const char *) data, read_len); // echo back
}
This example reads incoming UART data and echoes it back to the sender.
Common Errors in UART ESP32
- Wrong Baud Rate – Always match sender and receiver baud rates.
- Pin Mapping Issue – Check if TX and RX pins are set correctly.
- Cross Wiring – TX of ESP32 must connect to RX of device, and RX to TX.
- USB Drivers – Make sure CP210x or CH340 drivers are installed for ESP32 board.
ESP32 UART with Arduino IDE
If you prefer Arduino IDE over ESP-IDF, you can still use UART easily. Example:
void setup() {
Serial.begin(115200); // UART0
Serial1.begin(9600, SERIAL_8N1, 16, 17); // UART1 on GPIO16,17
}
void loop() {
if (Serial1.available()) {
char c = Serial1.read();
Serial.write(c);
}
}
This sets up UART1 on GPIO16 and GPIO17 with baud rate 9600.
Practical Uses of ESP32 UART
- Debugging – Use UART0 for serial monitor.
- Communication with GPS Module – Many GPS modules send NMEA data over UART.
- GSM and LTE Modules – Send AT commands to modules like SIM800L.
- Two ESP32 Boards – Create simple peer-to-peer communication.
- Data Logging – Send sensor data to PC for analysis.
Pros and Cons of ESP32 UART
Pros
- Supported by hardware, so reliable.
- Flexible pin mapping.
- High baud rates up to 5 Mbps.
Cons
- UART0 often used for programming, leaving fewer free UARTs.
- Care needed for level shifting when connecting with 5V devices.
FAQ on ESP32 UART
What is UART in ESP32?
It is a serial communication protocol available on ESP32 with three hardware UARTs.
Which UART does ESP32 use by default?
UART0 is used for programming and debugging.
Can I use all three UARTs on ESP32?
Yes, but UART0 is shared with USB programming.
What baud rates does ESP32 UART support?
Up to 5 Mbps, though 9600 and 115200 are most common.
How do I remap UART pins on ESP32?
Use the uart_set_pin()
function in ESP-IDF.
Can I use UART in Arduino IDE?
Yes, by using Serial, Serial1, and Serial2.
Why is my ESP32 UART not working?
Check baud rate, pin mapping, wiring, and drivers.
Does ESP32 UART support DMA?
Yes, ESP-IDF UART driver supports DMA for faster transfers.
Can I use UART for debugging?
Yes, UART0 is the default debug interface.
Will ESP32 UART work with GPS or GSM modules?
Yes, both modules use standard UART communication.
Conclusion
Learning ESP32 UART is simple when explained step by step. With clear wiring, correct baud rate, and tested examples, you can send and receive data with ease. Whether you use ESP-IDF UART code or Arduino IDE, the concepts remain the same. Start with a basic ESP32 UART example, test communication, and then expand into projects with GPS, GSM, or multiple boards.
If you found this helpful, check more tutorials on ControllersTech and start building your own ESP32 projects today.