Blink an LED on STM32
Learn GPIO output by blinking the onboard LED
Estimated time:20 minutes
Official reference documentationWhat 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
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
Circuit diagram
Circuit diagram placeholder
No external circuit is needed. The typical Blue Pill onboard LED is connected to PC13 and turns on when the pin is driven low.
Diagram coming soonPC13 → Onboard LED (active-low). No breadboard needed for this lab. Driving PC13 LOW turns the LED ON; driving it HIGH turns the LED OFF.
Code
Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.
#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.
Simulate It
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.
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.
Expected output image placeholder
Expected Blue Pill PC13 LED blinking output placeholder
Diagram coming soonCommon Errors
1. LED not blinking
Possible cause: GPIO clock not enabled.
Fix: Make sure __HAL_RCC_GPIOC_CLK_ENABLE() is called before GPIO initialization.
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. 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
Level 1 (Easy)
Change blink speed to 200 ms.
Level 2 (Medium)
Blink the LED 3 times fast, pause 1 second, then repeat.
Level 3 (Hard)
Create an SOS pattern using Morse code: 3 short, 3 long, 3 short blinks.