Lab 01
Beginner
STM32

Blink an LED on STM32

Learn GPIO output by blinking the onboard LED

MCU:STM32F103C8T6 (Blue Pill)

Estimated time:20 minutes

Official reference documentation
01

What you'll learn

  • Setting up a new project in STM32CubeIDE
  • Configuring GPIO pin as output
  • Using HAL_GPIO_TogglePin()
  • Using HAL_Delay() for timing
  • Understanding active-low LED logic on Blue Pill
02

Components needed

  • STM32F103C8T6 Blue Pill boardUses the onboard LED connected to PC13
  • Onboard LEDActive-low on the typical Blue Pill board
  • ST-Link programmerFor flashing and debugging the STM32
  • USB cable and jumper wiresPower and programmer connections
03

Circuit diagram

PC13 → Onboard LED (active-low). No breadboard needed for this lab. Driving PC13 LOW turns the LED ON; driving it HIGH turns the LED OFF.

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"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config(); // Generated by STM32CubeIDE
MX_GPIO_Init();
while (1)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_Delay(500); // Wait 500 milliseconds
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
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);
}

Code explanation

  • HAL_GPIO_TogglePin() flips the pin state each call.
  • HAL_Delay(500) waits 500 ms: 500 ms ON + 500 ms OFF gives a 1 Hz blink cycle.
  • PC13 is active-low: pin HIGH = LED OFF, pin LOW = LED ON.
  • GPIO_PIN_SET in HAL_GPIO_WritePin() sets the initial state to HIGH (LED off).
  • Use this main.c in an STM32CubeIDE-generated project; keep its generated SystemClock_Config() implementation and HAL support files.
05

Simulate It

STM32 Blue Pill3.3VGNDPC13PC13LED OFFILLUSTRATIVE · ACTIVE-LOWSTM32F103C8T6
Stopped
500ms
Slow · 1000msNormal · 500msFast · 200ms

Serial Output

What you should see

  • The onboard LED alternates ON and OFF every 500 ms.
  • In the built-in simulator, PC13 toggles at the selected interval: 1000 ms, 500 ms or 200 ms. LOW turns the LED ON; HIGH turns it OFF.
  • If the LED stays ON or OFF constantly, check the HAL_Delay value and that the loop and HAL timebase are running.
  • If nothing happens, check that the GPIOC clock is enabled.
06

Expected Output

The Blue Pill onboard LED connected to PC13 toggles every 500 ms. Because it is active-low, the LED is on while PC13 is low.

Common Errors

  1. 1. LED not blinking

    Possible cause: GPIO clock not enabled.

    Fix: Make sure __HAL_RCC_GPIOC_CLK_ENABLE() is called before GPIO initialization.

  2. 2. LED always ON

    Possible cause: pin mode not set to output push-pull.

    Fix: Check GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP, the output state and the wiring.

  3. 3. Project not compiling

    Possible cause: main.h not included.

    Fix: Add #include "main.h" at the top. Also retain the generated clock configuration and project support files.

Key Concepts

GPIO (General Purpose Input Output)
Digital pins that can be set as input or output.
HAL (Hardware Abstraction Layer)
ST’s library that simplifies hardware control.
Active-low
Logic where a LOW signal represents the ON state.
Toggle
Flip between HIGH and LOW each time the toggle function is called.

07 · Challenge Task

Take this lab further

  1. Level 1 (Easy)

    Change blink speed to 200 ms.

  2. Level 2 (Medium)

    Blink the LED 3 times fast, pause 1 second, then repeat.

  3. Level 3 (Hard)

    Create an SOS pattern using Morse code: 3 short, 3 long, 3 short blinks.

Up next · Lab 02

UART Communication on STM32

In Lab 02, you will learn UART communication — send “Hello World” from your STM32 to your PC and read it on a serial monitor.

Next Lab
Back to all labs