Many beginners run into problems when they try to use UART on ESP32 with ESP-IDF. Wrong pins, wrong baud rate, or missing driver functions can make things harder than expected. The best way to fix this is by following a working ESP32 UART example using ESP-IDF. In this tutorial, we explain setup, code, and testing in simple steps.
ESP32 UART Overview
- ESP32 has 3 hardware UARTs (UART0, UART1, UART2).
- UART0 is used for programming and debugging.
- UART1 and UART2 are free for user applications.
- You can map TX and RX pins to almost any GPIO using
uart_set_pin()
.
Hardware Setup
- ESP32 Development Board (ESP-WROOM or ESP-WROVER).
- USB cable for programming.
- Optional: USB-to-TTL converter or another microcontroller for testing.
- Jumper wires.
For testing, connect TX of ESP32 to RX of another device, and RX of ESP32 to TX of the other device.
ESP32 UART Example Using ESP-IDF
Sending Data with UART
#include "driver/uart.h"
#include "string.h"
#define TXD_PIN (GPIO_NUM_17)
#define RXD_PIN (GPIO_NUM_16)
void app_main(void)
{
const uart_port_t uart_num = UART_NUM_1;
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, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, 1024, 0, 0, NULL, 0);
const char *test_str = "ESP32 UART Example Using ESP-IDF\n";
while (1) {
uart_write_bytes(uart_num, test_str, strlen(test_str));
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
This sends a message every second through UART1 on GPIO17 (TX) and GPIO16 (RX).
Receiving Data with UART
#define BUF_SIZE (1024)
uint8_t data[BUF_SIZE];
while (1) {
int length = 0;
uart_get_buffered_data_len(UART_NUM_1, (size_t*)&length);
int read_len = uart_read_bytes(UART_NUM_1, data, length, 100 / portTICK_PERIOD_MS);
if (read_len > 0) {
uart_write_bytes(UART_NUM_1, (const char *) data, read_len); // echo back
}
}
This code reads incoming UART data and sends it back, acting as an echo test.
Common Errors in ESP-IDF UART
- Wrong Baud Rate → Ensure sender and receiver match.
- Pin Mapping → Check that TX and RX are set correctly in
uart_set_pin()
. - Cross Wiring → Connect TX to RX, and RX to TX.
- Driver Not Installed → Always call
uart_driver_install()
before using UART.
Practical Use Cases
- Debugging with external UART terminal.
- Communication with GPS modules.
- Sending AT commands to GSM modules.
- Data exchange between two ESP32 boards.
FAQ
Which UART is used for programming ESP32?
UART0 is used by default for flashing and debugging.
Can I use UART1 and UART2 freely?
Yes, they are available for custom applications.
How do I change UART pins in ESP-IDF?
Use uart_set_pin()
with GPIO numbers.
What is the maximum baud rate for ESP32 UART?
Up to 5 Mbps is supported, but 115200 is most common.
Can ESP32 UART work with GPS or GSM modules?
Yes, both modules use UART for data exchange.
Conclusion
The ESP32 UART example using ESP-IDF makes it clear how to send and receive data. With proper configuration, you can use UART for debugging, external modules, or board-to-board communication. Start with the basic echo example and then build on top of it for your own projects.
If you want more examples with SPI, I2C, or Wi-Fi projects, check the other tutorials on ControllersTech.