When beginners try ESP32 UART communication, they often face wiring mistakes, wrong baud rates, or confusing code. These issues can stop projects from working and make learning harder. With the right guide, however, sending and receiving data with UART ESP32 becomes simple. In this blog, we explain UART basics, show working code examples for ESP32 UART using both ESP-IDF and Arduino IDE, and cover common mistakes to avoid.
What is UART in ESP32?
UART stands for Universal Asynchronous Receiver and Transmitter. It is a serial protocol used to send and receive data one bit at a time.
The ESP32 microcontroller has three hardware UART controllers:
-
UART0 – Used by default for programming and serial monitor.
-
UART1 – Free for user communication.
-
UART2 – Free for user communication.
Each UART supports custom baud rates, data bits, parity, and stop bits.
Why Use ESP32 UART?
-
Send data between ESP32 and PC.
-
Connect modules like GPS, GSM, or RFID readers.
-
Debug projects with serial output.
-
Connect two ESP32 boards together.
-
Share data with Arduino or Raspberry Pi.
ESP32 UART Pin Mapping
By default:
-
UART0: TX0 = GPIO1, RX0 = GPIO3
-
UART1: TX1 = GPIO10, RX1 = GPIO9
-
UART2: TX2 = GPIO17, RX2 = GPIO16
ESP32 allows remapping pins, so you can assign TX and RX to most GPIOs. This gives flexibility in hardware design.
ESP32 UART Communication with ESP-IDF
Sending Data Example
#include "driver/uart.h"
#include "string.h"
#define UART_PORT UART_NUM_1
#define BUF_SIZE (1024)
void app_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);
const char *msg = "Hello from ESP32 UARTn";
while (1) {
uart_write_bytes(UART_PORT, msg, strlen(msg));
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
This sends “Hello from ESP32 UART” every second through UART1 on pins GPIO17 (TX) and GPIO16 (RX).
Receiving Data Example
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 reads data from UART and echoes it back to the sender.
ESP32 UART Communication with Arduino IDE
If you prefer Arduino IDE, the code becomes very simple.
void setup() {
Serial.begin(115200); // UART0
Serial1.begin(9600, SERIAL_8N1, 16, 17); // UART1 on GPIO16 (RX), GPIO17 (TX)
}
void loop() {
if (Serial1.available()) {
char c = Serial1.read();
Serial.write(c); // echo to serial monitor
}
}
This sets up UART1 on GPIO16 and GPIO17 with baud rate 9600.
Common Errors in UART ESP32
-
Mismatched Baud Rate – Both sender and receiver must use the same baud rate.
-
Cross Wiring – Connect TX of ESP32 to RX of device, and RX to TX.
-
Pin Mapping – Check if the chosen pins support UART.
-
Shared UART0 – Remember UART0 is already used for programming and serial monitor.
-
Driver Issue – Ensure USB drivers (CP2102/CH340) are installed for ESP32.
Practical ESP32 UART Projects
-
Sending sensor data to PC.
-
GPS data logging with UART NMEA strings.
-
GSM module with AT commands.
-
Communication between ESP32 and Arduino.
-
Wireless bridge between two ESP32 boards.
Pros and Cons of ESP32 UART
Pros
-
Simple and reliable protocol.
-
Multiple UARTs available.
-
Flexible pin assignment.
-
High speed supported up to 5 Mbps.
Cons
-
Limited to point-to-point communication.
-
UART0 is busy during programming.
-
Requires level shifting for 5V devices.
FAQ on ESP32 UART Communication
What is ESP32 UART used for?
It is used for serial communication between ESP32 and other devices.
How many UARTs are on ESP32?
Three hardware UARTs are available: UART0, UART1, and UART2.
Which UART is used for programming?
UART0 is used for programming and debugging.
Can I remap UART pins on ESP32?
Yes, ESP32 allows flexible pin mapping using software.
What is the maximum baud rate of ESP32 UART?
It can support up to 5 Mbps.
Can I use ESP32 UART in Arduino IDE?
Yes, you can use Serial, Serial1, and Serial2.
Why is my ESP32 UART not working?
Check baud rate, wiring, and pin mapping.
Does ESP32 UART support DMA?
Yes, ESP-IDF UART driver supports DMA transfers.
Can two ESP32 boards talk over UART?
Yes, connect TX of one to RX of the other.
Can I use UART for debugging?
Yes, UART0 is commonly used with serial monitor.
Conclusion
ESP32 UART communication is easy to set up and very powerful once you know the basics. By using the right baud rate, correct wiring, and tested code examples, you can connect ESP32 with almost any device. Whether you prefer ESP-IDF or Arduino IDE, the method is simple and reliable. Start with a basic ESP32 UART example and expand your projects to GPS, GSM, or multi-board communication.
For more ESP32 guides and real examples, check other tutorials on ControllersTech and start building your own projects today.


