Lab 02
Beginner
STM32

UART Communication on STM32

Learn to send debug messages from STM32 to your PC via UART

MCU:STM32F103C8T6 (Blue Pill)
Official reference documentation
01

What you'll learn

  • Configuring UART in STM32CubeIDE
  • Using HAL_UART_Transmit()
  • Viewing output on serial monitor
  • Understanding baud rate
02

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
03

Circuit diagram

04

Code

Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.

main.c
#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.

05

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/..."
06

Expected Output

Open the serial monitor at 115200 baud → “Hello World from STM32!” prints every 1 second.

07 · Challenge Task

Take this lab further

Send a counter value via UART — print “Count: 1”, “Count: 2”, “Count: 3”... every second

Up next · Lab 03

Button Input — Toggle LED on STM32

Next Lab
Back to all labs