Getting Started with STM32F103C8T6 Blue Pill: GPIO LED Blink Tutorial

Moving from 8-bit microcontrollers like the Arduino Uno to 32-bit ARM Cortex-M chips opens the door to faster clock speeds, hardware DMA, advanced timers, and richer peripheral sets. One of the easiest and most affordable boards to start with is the STM32F103C8T6, widely known as the Blue Pill. Powered by a 72 MHz ARM Cortex-M3 core, this compact development board packs 64 KB of Flash memory, 20 KB of SRAM, and dozens of general-purpose input/output (GPIO) pins into a breadboard-friendly layout.

Illustration

In this tutorial, you will learn how to set up an STM32 project in STM32CubeIDE, wire an ST-Link V2 programmer to the Blue Pill, and write a clean C program using ST's Hardware Abstraction Layer (HAL) to blink both the onboard LED on PC13 and an external LED on PA5.

How STM32 GPIO Output Works

Unlike basic AVR boards where every peripheral port is powered on by default, STM32 microcontrollers keep peripheral clocks disabled at startup to save power. Before a GPIO pin can drive a voltage high or low, the firmware must enable the clock for that specific port and configure its electrical mode.

  1. The system clock tree configures the High-Speed External (HSE) 8 MHz crystal oscillator and multiplies it via the Phase-Locked Loop (PLL) to run the CPU at 72 MHz.
  2. The Reset and Clock Control (RCC) peripheral enables the APB2 bus clock for GPIO Port A and Port C.
  3. The GPIO initialization routine sets pin PC13 and pin PA5 as push-pull digital outputs with no internal pull-up or pull-down resistors.
  4. Inside the main while (1) loop, HAL_GPIO_TogglePin() flips the output data register bit between 0 (0 V) and 1 (3.3 V), while HAL_Delay() pauses execution using the 1 ms SysTick hardware timer.

LED Current (mA) = (Supply Voltage - LED Forward Voltage) / Resistor Value (Ohms)

With a 3.3 V GPIO output, a standard red LED (2.0 V forward drop), and a 220 Ohm current-limiting resistor, the pin sources roughly 5.9 mA, well within the 25 mA per-pin limit of the STM32F103C8T6.

Hardware and Settings

Gather the components listed below to wire and flash your Blue Pill board.

ItemChoiceReason
Microcontroller BoardSTM32F103C8T6 Blue Pill72 MHz 32-bit ARM Cortex-M3 development board
Programmer / DebuggerST-Link V2 USB DonglePrograms and debugs the STM32 over the 2-wire SWD interface
Indicator LED5 mm Red or Green LEDVisualizes digital output switching on pin PA5
Current-Limiting Resistor220 Ohm (1/4 W)Protects the GPIO pin and LED from excessive current
Prototyping GearBreadboard and female-to-male jumper wiresConnects the ST-Link V2 header and external LED cleanly

Wiring Connections

Connect the ST-Link V2 programmer to the 4-pin Serial Wire Debug (SWD) header at the end of the Blue Pill, and wire the external LED on the breadboard.

ST-Link V2 Programmer to STM32 Blue Pill

PinSTM32 Blue Pill PinFunction
3.3V3.3V (on 4-pin SWD header)Supplies regulated 3.3 V power to the board
GNDGNDCommon ground reference
SWDIODIO (PA13)Serial Wire Debug bidirectional data line
SWCLKDCLK (PA14)Serial Wire Debug clock line

External LED to STM32 Blue Pill

PinSTM32 Blue Pill PinFunction
LED Anode (+) via 220 Ohm resistorPA5Driven high (3.3 V) to turn on the external LED
LED Cathode (-)GNDGround return path

Pin labels printed on the metal shell of generic ST-Link V2 clones occasionally differ from the actual header pinout, so always double-check the markings directly on the plastic shroud or PCB before plugging in the USB dongle.

Complete Code

Complete STM32 HAL C code (`main.c`) for STM32CubeIDE to blink the onboard LED on PC13 and an external LED on PA5 in alternating order.
#include "main.h"

void SystemClock_Config(void);
static void MX_GPIO_Init(void);

int main(void) {
  /* Reset of all peripherals, initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock to 72 MHz */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();

  /* Set initial states: PC13 is active-low on Blue Pill, PA5 is active-high */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

  while (1) {
    /* Toggle onboard LED (PC13) and external LED (PA5) every 500 ms */
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
    HAL_Delay(500);
  }
}

void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /* Initializes the RCC Oscillators using the 8 MHz HSE crystal and 9x PLL */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    Error_Handler();
  }

  /* Initializes the CPU, AHB and APB buses clocks */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
                                RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
    Error_Handler();
  }
}

static void MX_GPIO_Init(void) {
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* Enable GPIO Ports C and A Clocks */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /* Configure PC13 (Onboard LED) as Output Push-Pull */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /* Configure PA5 (External LED) as Output Push-Pull */
  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void Error_Handler(void) {
  __disable_irq();
  while (1) {
  }
}

Uploading and Testing

  1. Open STM32CubeIDE and create a new project via File > New > STM32 Project.
  2. In the Target Selector search box, type STM32F103C8, select STM32F103C8Tx, click Next, name your project BluePill_Blink, and click Finish.
  3. When the .ioc Device Configuration Tool opens, set RCC High Speed Clock (HSE) to Crystal/Ceramic Resonator, set SYS Debug to Serial Wire, and configure PC13 and PA5 as GPIO_Output (or replace the generated Core/Src/main.c with the complete code above).
  4. Plug the ST-Link V2 into a USB port on your computer with the Blue Pill wired to 3.3V, GND, SWDIO, and SWCLK.
  5. Click the green Run button (or press Ctrl + F11), confirm that the debugger probe is set to ST-LINK (OpenOCD) or ST-LINK (ST-LINK GDB server), and click OK to flash the binary.
  6. Watch the Blue Pill: the onboard green or blue LED connected to PC13 and your external breadboard LED on PA5 will alternate on and off every 0.5 seconds.

Understanding Active-Low vs. Active-High Pins on STM32

When working with STM32 development boards, pay close attention to how onboard LEDs and buttons are wired electrically.

  • Onboard PC13 is active-low: On the Blue Pill schematic, the anode of the onboard LED connects to 3.3 V through a resistor and its cathode connects to PC13. Calling HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET) pulls the pin low (0 V) and turns the LED ON, while GPIO_PIN_SET turns it OFF.
  • External PA5 is active-high: Because our external LED cathode goes to GND, setting PA5 high (3.3 V) turns the LED ON.
  • Pin current budget: The PC13 pin on STM32F103 chips is supplied through the backup power domain and can only sink or source up to 3 mA, making it suitable for small SMD indicator LEDs but unsuitable for driving heavy loads directly.

Note that this tutorial covers bare-metal HAL GPIO output using blocking HAL_Delay() calls and does not configure hardware timer interrupts or UART serial logging.

Common Problems

  • STM32CubeIDE reports "No ST-LINK detected": Install the official STSW-LINK009 USB drivers on Windows or check that your USB cable supports data transfer rather than charging only.
  • Target is not responding or "Error: init mode failed": Verify that SWDIO and SWCLK are not swapped, ensure SYS > Debug is set to Serial Wire in your project configuration (otherwise PA13 and PA14 get disabled after the first upload), and hold the Blue Pill RESET button while clicking Run if the chip is locked up.
  • Program runs after flashing but stops after unplugging and re-powering: Check the yellow BOOT0 jumper on the Blue Pill. It must be placed on the 0 position (GND); if left on 1, the STM32 boots into the empty system bootloader instead of your Flash code.
  • Blue Pill board has a counterfeit or clone MCU (CS32F103 or CKS32F103): Many budget Blue Pill boards ship with compatible clone chips whose IDCODE differs slightly from genuine ST silicon. If ST-LINK GDB server rejects the chip ID, switch the debug probe setting in Run > Debug Configurations > Debugger to ST-LINK (OpenOCD).
  • External LED glows very dimly or not at all: Confirm the long leg (anode) of the LED connects toward PA5 through the 220 Ohm resistor and the short leg (cathode) goes to GND.

Summary

In this tutorial, you learned how to set up and program an STM32F103C8T6 Blue Pill microcontroller using STM32CubeIDE and an ST-Link V2 debugger. By configuring the 72 MHz PLL system clock, enabling the GPIO peripheral clocks, and toggling pins PC13 and PA5 with HAL_GPIO_TogglePin(), you built a working hardware test setup that demonstrates the difference between active-low and active-high outputs on 32-bit ARM Cortex-M microcontrollers.