Master STM32 GPIO: Light Up an LED in Minutes
This guide walks you through the fundamentals of STM32 GPIO, covering pin naming, push‑pull vs open‑drain modes, wiring an LED, configuring PB8 with CubeMX, and using HAL functions to programmatically toggle the LED with concise code examples.
1. What Is GPIO?
GPIO (General‑Purpose Input/Output) ports on an STM32 allow the microcontroller to exchange signals with external devices by setting pins high or low or reading input levels.
1.1 Definition
Each pin can be configured to output a high or low voltage or to read an incoming signal, enabling control and data acquisition.
1.2 Naming Convention
Pins are named by group letter and number, e.g., PA0‑PA15, PB0‑PB15, etc. The STM32F103C8T6 used here provides groups A‑D with up to 16 pins each.
1.3 Internal Structure
The official reference manual shows a complex internal block diagram, but beginners can ignore low‑level details until the LED lights up.
1.4 Push‑Pull vs Open‑Drain
Push‑pull outputs can drive both high and low levels directly, while open‑drain outputs require an external pull‑up resistor to achieve a high level.
2. How to Light an LED
The LED is connected with a series resistor to VCC on one side and to a GPIO pin on the other. Pulling the pin low turns the LED on.
In the schematic, LED1 is attached to pin PB8, so setting PB8 low will illuminate the LED.
3. Programming the LED
3.1 Common HAL GPIO Functions
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init);
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);HAL_GPIO_Init configures a pin (CubeMX can generate this automatically). HAL_GPIO_WritePin sets the pin level, HAL_GPIO_ReadPin reads it, and HAL_GPIO_TogglePin flips the level.
3.2 CubeMX Configuration
Enable Debug mode.
Select pin PB8 and set its mode to GPIO_Output .
Ensure the pin is initially set high to keep the LED off at power‑up.
After configuring, click GENERATE CODE to produce the project files.
3.3 Writing the Code
CubeMX generates the initialization function:
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}Insert the blinking logic inside the USER CODE BEGIN and USER CODE END sections of main.c to avoid being overwritten:
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
HAL_Delay(1000);This toggles the LED on for one second and off for one second repeatedly.
4. Summary
Lighting an LED with STM32 introduces the essential GPIO concepts, naming rules, and practical HAL usage. Mastering this simple example builds a solid foundation for more advanced embedded projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
