ESP32 UART Programming: Easy Examples in ESP-IDF
Learn ESP32 UART programming step by step with ESP-IDF. Simple code examples for sending and receiving data over serial.

Many beginners run into problems while setting up UART communication on ESP32. Wrong pin mapping, mismatched baud rate, and confusing SDK calls can waste hours. If you want to save time, the best way is to learn ESP32 UART programming with ESP-IDF examples. In this guide, we will explain UART basics, wiring, and working code so you can send and receive serial data without issues.

What is UART in ESP32?

UART (Universal Asynchronous Receiver and Transmitter) is one of the simplest communication protocols. ESP32 has three hardware UARTs:

  • UART0 – used for flashing firmware and debugging by default.

  • UART1 – available for user applications.

  • UART2 – available for user applications.

Each UART supports baud rates up to 5 Mbps.

ESP32 UART Pin Mapping

Default pin mapping is:

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

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

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

But ESP32 lets you remap UART pins to almost any GPIO using uart_set_pin().

ESP32 UART Programming with ESP-IDF

To work with UART in ESP-IDF, you need to:

  1. Configure UART parameters.

  2. Assign TX and RX pins.

  3. Install a driver.

  4. Write and read data.

Example 1: Sending Data with UART

This example shows how to send text over UART1.

 
#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 = "ESP32 UART Programming Example\n"; while (1) { uart_write_bytes(UART_PORT, msg, strlen(msg)); vTaskDelay(1000 / portTICK_PERIOD_MS); } }

This program sends a message every second over GPIO17 (TX) and GPIO16 (RX).

Example 2: Receiving Data with UART

Now let’s read data sent to ESP32 through UART.

 
#define BUF_SIZE (1024) uint8_t data[BUF_SIZE]; int len = 0; ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_PORT, (size_t*)&len)); int read_len = uart_read_bytes(UART_PORT, data, len, 100 / portTICK_PERIOD_MS); if (read_len > 0) { uart_write_bytes(UART_PORT, (const char *) data, read_len); // echo }

This reads data from UART1 and echoes it back to the sender.

ESP32 UART Projects

Here are some practical uses:

  • GPS Module → read NMEA data over UART.

  • GSM Module → send AT commands.

  • PC Logging → send sensor data to PC.

  • Two ESP32 Boards → connect with UART2 for board-to-board communication.

Conclusion

ESP32 UART programming with ESP-IDF is easy once you set up pins, baud rate, and drivers correctly. With just a few lines of code, you can send and receive data. Whether you connect a GPS, GSM, or another ESP32 board, UART makes communication simple.

 

If you are starting your ESP32 journey, try these examples today. For more embedded tutorials, check out ControllersTech.


disclaimer

Comments

https://newyorktimesnow.com/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!