×

STM32 UART Guide: Serial Communication Made Simple

Many beginners face problems when using UART on STM32. The code seems confusing, the CubeIDE configuration feels overwhelming, and wiring mistakes make debugging harder. This frustration often keeps students stuck on simple tasks like printing text to a serial monitor. The good news is that once you understand the basics, STM32 UART communication becomes simple. In this guide, we will explain UART step by step, set it up in STM32CubeIDE, write example code, and test it on real hardware.

What is UART in STM32?

UART (Universal Asynchronous Receiver and Transmitter) is a hardware communication protocol. It allows two devices to exchange data serially without a clock line.

Key points about UART in STM32:

  • Works on two main pins: TX (Transmit) and RX (Receive).

  • Supports different baud rates (commonly 9600 or 115200).

  • Asynchronous, meaning no shared clock is required.

  • Widely used for debugging, logging, and connecting modules.

Every STM32 microcontroller has multiple UART/USART peripherals. For example, STM32F103 (Blue Pill) has three UART interfaces.

Why Learn STM32 UART?

UART is one of the most common interfaces in embedded systems. You will use it for:

  • Debugging code by printing values to the terminal.

  • Connecting modules like GPS, GSM, or Bluetooth.

  • Board-to-board communication between two STM32 devices.

  • Data logging from sensors to PC.

  • Bootloader programming in some STM32 boards.

If you master UART early, many future projects will become easier.

STM32 UART Pinout

Different STM32 boards have different pinouts. For example:

  • On STM32F103C8T6 (Blue Pill):

    • USART1 TX → PA9

    • USART1 RX → PA10

  • On STM32F407 Discovery:

    • USART2 TX → PA2

    • USART2 RX → PA3

Always check your datasheet or CubeIDE pinout tool to confirm pins.

Setting Up STM32 UART in STM32CubeIDE

Step 1: Create a New Project

  1. Open STM32CubeIDE.

  2. Select your STM32 chip or board.

  3. Create a new project.

Step 2: Configure UART

  1. In the Pinout & Configuration tab, click on USART2 (or any available UART).

  2. Set the mode as Asynchronous.

  3. CubeIDE automatically assigns TX and RX pins.

Step 3: Set Parameters

  • Baud Rate: 115200 (commonly used).

  • Word Length: 8 bits.

  • Stop Bits: 1.

  • Parity: None.

  • Hardware Flow Control: None.

Step 4: Generate Code

Click Generate Code. CubeIDE creates initialization code for UART.

STM32 UART Example 1: Transmit Data

In main.c, add this code:

#include "main.h"
#include "string.h"

char msg[] = "Hello from STM32 UART\r\n";

int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();

while (1) {
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
HAL_Delay(1000);
}
}

This sends a message every second through USART2. You can view it on a serial terminal like PuTTY.

Also read: How to Use UART in ESP32 

STM32 UART Example 2: Receive Data

Modify main.c to read data:

uint8_t rxData[10];

while (1) {
HAL_UART_Receive(&huart2, rxData, 5, HAL_MAX_DELAY);
HAL_UART_Transmit(&huart2, rxData, 5, HAL_MAX_DELAY); // Echo
}

This waits for 5 characters from UART and echoes them back.

STM32 UART Example 3: Interrupt Mode

Instead of blocking code, you can use interrupts.

uint8_t rxByte;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == USART2) {
HAL_UART_Transmit(&huart2, &rxByte, 1, HAL_MAX_DELAY);
HAL_UART_Receive_IT(&huart2, &rxByte, 1);
}
}

int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();

HAL_UART_Receive_IT(&huart2, &rxByte, 1);

while (1) { }
}

This uses interrupt-driven UART to receive and echo data.

STM32 UART Example 4: DMA Mode

For high-speed data, use DMA.

uint8_t buffer[100];

HAL_UART_Receive_DMA(&huart2, buffer, 100);

DMA reduces CPU load when handling large data.

Testing STM32 UART

  1. Flash the code to STM32 board.

  2. Connect TX and RX pins to a USB-to-TTL converter.

  3. Open a serial monitor on PC (115200 baud).

  4. Check if data is transmitted and received correctly.

Common Errors and Fixes

  • Wrong Baud Rate → Ensure both STM32 and PC have same baud.

  • Incorrect Pin Mapping → Use CubeIDE to verify pins.

  • No Response in Terminal → Check GND connection.

  • Garbage Characters → Wrong baud or crystal frequency mismatch.

Real-World Uses of STM32 UART

  • Send GPS data to STM32 via UART.

  • Connect STM32 to SIM800L GSM module for IoT.

  • Log sensor data to PC for debugging.

  • Exchange data between two STM32 boards.

  • Debug firmware using UART print statements.

Pros and Cons of STM32 UART

Pros

  • Easy to configure in CubeIDE.

  • Reliable for short-distance communication.

  • Supports interrupts and DMA.

Cons

  • Limited to point-to-point communication.

  • Not reliable for long distances without RS-485 converters.

FAQ on STM32 UART

What is UART in STM32?
It is a hardware module for serial communication using TX and RX pins.

How many UARTs are in STM32?
Depends on the series. STM32F103 has 3, STM32F4 can have up to 6.

Which baud rate is best for STM32 UART?
115200 is standard for debugging.

Can STM32 UART use DMA?
Yes, STM32 HAL supports UART with DMA for fast transfers.

Can I use UART with Arduino IDE on STM32?
Yes, STM32 boards can be programmed in Arduino IDE with UART functions.

Why am I seeing garbage characters in terminal?
This usually means baud rate mismatch.

Can I remap UART pins on STM32?
Yes, many STM32 series support alternate function remapping.

Is UART full-duplex on STM32?
Yes, most STM32 UARTs are full-duplex.

Does STM32 support RS-485 with UART?
Yes, by using external RS-485 driver ICs.

Can UART be used while debugging with SWD?
Yes, UART and SWD can be used independently.

Conclusion

This guide explained STM32 UART communication in simple steps. We covered setup in CubeIDE, transmitting and receiving data, using interrupts, and testing with real hardware. Once you learn UART, you can connect STM32 to modules like GPS, GSM, and Bluetooth, or use it for debugging and data logging.

If you want more examples and advanced STM32 tutorials, check ControllersTech for detailed guides and projects.