UART Communication on STM32
Learn to send debug messages from STM32 to your PC via UART
What you'll learn
- Configuring UART in STM32CubeIDE
- Using HAL_UART_Transmit()
- Viewing output on serial monitor
- Understanding baud rate
Components needed
- STM32F103C8T6 Blue PillUSART1 TX on PA9 and RX on PA10
- USB to TTL converter (CP2102 or CH340)A 3.3 V logic-compatible serial adapter
- Jumper wiresCross TX/RX and connect the grounds
Circuit diagram
Circuit diagram placeholder
Blue Pill PA9 (USART1_TX) → USB-to-TTL RX, adapter TX → PA10 (USART1_RX), and GND ↔ GND. Use 3.3 V-compatible logic, never ±RS-232 voltage levels.
Diagram coming soonCode
Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.
#include "main.h"#include "string.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_USART1_UART_Init(void);
int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init();
char msg[] = "Hello World from STM32!\r\n";
while (1) { HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); HAL_Delay(1000); }}
static void MX_USART1_UART_Init(void){ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; HAL_UART_Init(&huart1);}HAL_UART_Transmit() sends the message through USART1, and HAL_Delay(1000) creates a one-second interval. Both ends must use the same baud rate and frame format; STM32CubeIDE generates the GPIO and clock initialization.
Simulate It
Run the circuit in your browser when a Wokwi simulation is connected to this lesson.
Wokwi simulation placeholder
Add the public Wokwi embed URL for this lab to launch the interactive simulator here.
wokwiEmbedUrl: "https://wokwi.com/projects/..."Expected Output
Open the serial monitor at 115200 baud → “Hello World from STM32!” prints every 1 second.
Expected output image placeholder
Expected serial monitor output showing Hello World from STM32 every second
Diagram coming soon07 · Challenge Task
Take this lab further
Send a counter value via UART — print “Count: 1”, “Count: 2”, “Count: 3”... every second