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.

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.
- The system clock tree configures the High-Speed External (
HSE)8 MHzcrystal oscillator and multiplies it via the Phase-Locked Loop (PLL) to run the CPU at72 MHz. - The Reset and Clock Control (
RCC) peripheral enables theAPB2bus clock forGPIOPort A and Port C. - The
GPIOinitialization routine sets pinPC13and pinPA5as push-pull digital outputs with no internal pull-up or pull-down resistors. - Inside the main
while (1)loop,HAL_GPIO_TogglePin()flips the output data register bit between0(0 V) and1(3.3 V), whileHAL_Delay()pauses execution using the1 msSysTickhardware 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.
| Item | Choice | Reason |
|---|---|---|
| Microcontroller Board | STM32F103C8T6 Blue Pill | 72 MHz 32-bit ARM Cortex-M3 development board |
| Programmer / Debugger | ST-Link V2 USB Dongle | Programs and debugs the STM32 over the 2-wire SWD interface |
| Indicator LED | 5 mm Red or Green LED | Visualizes digital output switching on pin PA5 |
| Current-Limiting Resistor | 220 Ohm (1/4 W) | Protects the GPIO pin and LED from excessive current |
| Prototyping Gear | Breadboard and female-to-male jumper wires | Connects 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
| Pin | STM32 Blue Pill Pin | Function |
|---|---|---|
3.3V | 3.3V (on 4-pin SWD header) | Supplies regulated 3.3 V power to the board |
GND | GND | Common ground reference |
SWDIO | DIO (PA13) | Serial Wire Debug bidirectional data line |
SWCLK | DCLK (PA14) | Serial Wire Debug clock line |
External LED to STM32 Blue Pill
| Pin | STM32 Blue Pill Pin | Function |
|---|---|---|
LED Anode (+) via 220 Ohm resistor | PA5 | Driven high (3.3 V) to turn on the external LED |
LED Cathode (-) | GND | Ground 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
#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
- Open STM32CubeIDE and create a new project via
File > New > STM32 Project. - In the Target Selector search box, type
STM32F103C8, selectSTM32F103C8Tx, click Next, name your projectBluePill_Blink, and click Finish. - When the
.iocDevice Configuration Tool opens, setRCCHigh Speed Clock (HSE) to Crystal/Ceramic Resonator, setSYSDebug to Serial Wire, and configurePC13andPA5asGPIO_Output(or replace the generatedCore/Src/main.cwith the complete code above). - Plug the
ST-Link V2into a USB port on your computer with the Blue Pill wired to3.3V,GND,SWDIO, andSWCLK. - Click the green Run button (or press
Ctrl + F11), confirm that the debugger probe is set toST-LINK (OpenOCD)orST-LINK (ST-LINK GDB server), and click OK to flash the binary. - Watch the Blue Pill: the onboard green or blue LED connected to
PC13and your external breadboard LED onPA5will alternate on and off every0.5seconds.
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
PC13is active-low: On the Blue Pill schematic, the anode of the onboard LED connects to3.3 Vthrough a resistor and its cathode connects toPC13. CallingHAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET)pulls the pin low (0 V) and turns the LED ON, whileGPIO_PIN_SETturns it OFF. - External
PA5is active-high: Because our external LED cathode goes toGND, settingPA5high (3.3 V) turns the LED ON. - Pin current budget: The
PC13pin onSTM32F103chips is supplied through the backup power domain and can only sink or source up to3 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-LINK009USB 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
SWDIOandSWCLKare not swapped, ensureSYS > Debugis set to Serial Wire in your project configuration (otherwisePA13andPA14get disabled after the first upload), and hold the Blue PillRESETbutton while clicking Run if the chip is locked up. - Program runs after flashing but stops after unplugging and re-powering: Check the yellow
BOOT0jumper on the Blue Pill. It must be placed on the0position (GND); if left on1, the STM32 boots into the empty system bootloader instead of your Flash code. - Blue Pill board has a counterfeit or clone MCU (
CS32F103orCKS32F103): Many budget Blue Pill boards ship with compatible clone chips whose IDCODE differs slightly from genuine ST silicon. IfST-LINK GDB serverrejects the chip ID, switch the debug probe setting inRun > Debug Configurations > Debuggerto ST-LINK (OpenOCD). - External LED glows very dimly or not at all: Confirm the long leg (anode) of the LED connects toward
PA5through the220 Ohmresistor and the short leg (cathode) goes toGND.
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.