📘 COMPLETE STM32 BLUE PILL TUTORIAL

1. INTRODUCTION TO STM32 BLUE PILL

What is STM32 Blue Pill?

The STM32 Blue Pill is a low-cost development board based on STM32F103 microcontrollers.

Common Variants:

Chip

Flash

SRAM

STM32F103C6T6

32 KB

10 KB

STM32F103C8T6

64 KB

20 KB

Why STM32 over Arduino?

  • 32-bit ARM Cortex-M3 processor

  • Faster clock speed (72 MHz maximum)

  • Professional toolchain (CubeIDE)

  • Industry-standard HAL/LL drivers

  • Used in automotive, robotics, and industrial control applications

Blue Pill Board Overview

  • ARM Cortex-M3 core

  • Peripherals: GPIO, ADC, UART, SPI, I2C, Timers

  • SWD debugging interface

  • PC13 onboard LED (typically)


2. HARDWARE CONNECTION WITH ST-LINK V2

Required Hardware

  • STM32 Blue Pill board

  • ST-LINK V2 programmer (original or clone)

  • Breadboard and jumper wires

  • USB cable

ST-LINK to Blue Pill Connections (Critical)

ST-LINK

Blue Pill

SWDIO

PA13

SWCLK

PA14

GND

GND

3.3V

3.3V (optional)

NRST

NRST (recommended)

Important: BOOT0 must be connected to GND

Power Supply Rules

  • Use only ONE power source at a time

  • Options: USB cable OR ST-LINK 3.3V output

  • Never apply 5V to GPIO pins


3. INSTALLING AND SETTING UP STM32CUBEIDE (v1.18)

Why CubeIDE 1.18?

  • Integrated CubeMX configuration tool

  • Integrated OpenOCD debugger

  • Stable STM32F1 family support

  • No external tools required

Installation Steps

  1. Navigate to the STMicroelectronics website

  2. Download STM32CubeIDE version 1.18.x

  3. Run the installer

  4. Launch the IDE

  5. Select or create a workspace directory


4. STM32CUBEIDE AND CUBEMX WORKFLOW (Critical Section)

Step 1: Create New Project

  1. File → New → STM32 Project

  2. MCU Selection:

    • If your chip is C6T6, select STM32F103C6T6

    • If your chip is C8T6, select STM32F103C8T6

    • DO NOT select TR or ATR variants

Step 2: Clock Configuration (Safe Setup)

RCC Configuration:

  • HSI: ENABLED

  • HSE: DISABLED

  • PLL: OFF (recommended for beginners)

Result: SYSCLK = 8 MHz

Rationale:

  • Most clone boards have unreliable crystals

  • HSI (internal oscillator) is more reliable

Step 3: GPIO Pinout Configuration (LED Blink Example)

Onboard LED (PC13):

  • Set PC13 to GPIO_Output

GPIO Settings:

  • Mode: Output Push Pull

  • Speed: Low

  • Pull: No pull

Step 4: SYS Settings

  1. Navigate to System Core → SYS

  2. Debug: Serial Wire

  3. Disable SWV and Trace

Step 5: Generate Code

Click “Generate Code” button

LED Blink Code (Correct Implementation)

Note: PC13 is ACTIVE LOW

while (1)
{
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); // LED ON
  HAL_Delay(500);
  
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);   // LED OFF
  HAL_Delay(500);
}

5. FLASHING USING OPENOCD AND ST-LINK (Critical Knowledge)

Debug Configuration in CubeIDE

Debugger Settings:

  • Debug Probe: ST-LINK (OpenOCD)

  • Interface: SWD

  • Frequency: 400 kHz

  • Reset Mode: Connect under reset

Warning: High SWD speed causes erase failure

Correct Flashing Workflow

  1. Hold RESET button on the board

  2. Click Debug in CubeIDE

  3. Release RESET after connection established

  4. Click Resume

  5. Stop debug session

  6. Power-cycle the board

Common Errors Explained

Error

Meaning

Fix

vFlashErase packet

Target not halted

Lower SWD frequency and use connect under reset

target not halted

CPU running before erase

Lower SWD frequency and use connect under reset


6. STM32F103C6T6 vs C8T6 (Very Important)

Comparison Table

Parameter

C6T6

C8T6

Flash

32 KB

64 KB

SRAM

10 KB

20 KB

Important Rules

  • If you have a C6 chip, select C6 in CubeMX

  • Selecting C8 for a C6 chip will cause runtime faults

  • Many boards sold as “C8” are actually fake or mislabeled


7. STM32 WITH ARDUINO IDE

When to Use Arduino IDE

Advantages:

  • Quick prototyping

  • Familiar syntax for Arduino users

Disadvantages:

  • Less control over hardware

  • Slower execution

  • Less professional approach

Arduino IDE Setup

  1. Install Arduino IDE

  2. Add STM32 core URL to Board Manager:

    https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json

  3. Open Board Manager and install “STM32 MCU based boards”

Board Selection

  • Board: Generic STM32F103C series

  • Upload method: ST-Link

  • CPU Speed: 72 MHz (or 8 MHz for safe operation)

Arduino Blink Example

void setup() {
  pinMode(PC13, OUTPUT);
}

void loop() {
  digitalWrite(PC13, LOW);  // ON
  delay(500);
  digitalWrite(PC13, HIGH); // OFF
  delay(500);
}

8. IMPORTANT CONCEPTS

What is HAL?

Hardware Abstraction Layer (HAL) is a software layer that allows portable, readable, and maintainable code across different STM32 microcontrollers.

What is OpenOCD?

Open On-Chip Debugger is an open-source debug server that:

  • Communicates with ST-LINK hardware

  • Interfaces with GDB debugger

  • Halts the CPU

  • Erases flash memory

  • Programs flash memory

Why “Software Reset”?

  • Prevents CPU from executing corrupted code

  • Allows flash erase operations

  • Mandatory for STM32F1 series

Difference: RUN vs DEBUG Mode

Mode

Meaning

RUN

Standalone firmware execution

DEBUG

MCU halted and inspectable by debugger


End of Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *