How to Use GPIO using STM32 HAL Library


General Purpose Input/Output (GPIO) is fundamental for controlling LEDs, buttons, and communicating with peripherals.

Step-by-Step Guide:

  1. Configure GPIO in STM32CubeMX:

    • Set pin modes (Input, Output, Alternate Function).

    • Configure pull-up/down resistors and speed.

  2. Generate Code.

  3. Write Application Code:

// Set pin high
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);

// Read pin state
GPIO_PinState state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);

Tips:

  • Use external pull-up/down resistors if needed.

  • Use EXTI (external interrupt) for input events.