2026-03-16 11:30 AM - edited 2026-03-16 2:39 PM
This article provides a step-by-step guide to configure the EXTI (external interrupt) function for a GPIO in STM32CubeMX2. The tutorial uses the NUCLEO-C562RE board.
The EXTI module in a microcontroller enables asynchronous event detection by generating an interrupt in response to signal transitions on external input lines. It can be configured to trigger on rising edges, falling edges, or both, allowing precise detection of external events such as button presses or sensor outputs. This capability is critical for real-time responsiveness in embedded systems.
This article presents a demonstration in which pressing the user button on the board triggers an interrupt that causes the on-board LED to blink once.
Install the following tools:
The hardware used in this tutorial is the NUCLEO-C562RE board.
Follow these steps to create an application project for the NUCLEO-C562RE board. The hands-on exercise creates a simple EXTI implementation that toggles the LED when the push button on the NUCLEO board is pressed.
Open STM32CubeMX2. On the Home page, click the MCU square to create a new project.
In the search field under MCU name, enter STM32C562RE and select the MCU. Click [Continue].
Enter the project name and location. Click [Automatically Download, Install & Create Project] to finish project creation.
Select [Launch Project] to start.
To generate the code:
Open Visual Studio Code and open the project folder.
If prompted, select the configuration. If not prompted, press Ctrl+Shift+P, type CMake: Select Configure Preset, and choose the debug configuration.
Build the project to ensure everything is set, then proceed to code implementation.
After opening the generated project in Visual Studio Code, edit the main.c file as follows:
/**
******************************************************************************
* file : main.c
* brief : Main program body
* Calls target system initialization then loop in main.
******************************************************************************
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
hal_exti_handle_t *pEXTI13; /* Pointer to the EXTI handle from the generated code */
volatile uint8_t ExtiDetected = 0;
/* Private functions prototype -----------------------------------------------*/
/**
* brief: The application entry point.
* retval: none but we specify int to comply with C99 standard
*/
int main(void)
{
/** System Init: this code placed in targets folder initializes your system.
* It calls the initialization (and sets the initial configuration) of the peripherals.
* You can use STM32CubeMX to generate and call this code or not in this project.
* It also contains the HAL initialization and the initial clock configuration.
*/
if (mx_system_init() != SYSTEM_OK)
{
return (-1);
}
else
{
/*
* You can start your application code here
*/
mx_cortex_nvic_init(); // Initialize the NVIC
pEXTI13 = mx_gpio_default_exti13_gethandle(); // Get the EXTI13 handle
HAL_EXTI_Enable(pEXTI13,
HAL_EXTI_MODE_INTERRUPT); // Enable the EXTI13 interrupt
while (1) {
if (ExtiDetected == 1) {
HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_5);
HAL_Delay(100);
ExtiDetected = 0;
}
}
}
}
void HAL_EXTI_TriggerCallback(hal_exti_handle_t *hexti, hal_exti_trigger_t trigger)
{
/* Prevent unused argument compilation warning */
STM32_UNUSED(trigger);
if (hexti == pEXTI13) {
ExtiDetected = 1;
}
}
/* end main */
After building the application, select the [Run and Debug] icon. Create a debug session by selecting the [STM32Cube: STLINK GDB Server] option.
The USER_LED is off at the beginning of program execution. Pressing the USER_BUTTON toggles the LED state.
Insert a breakpoint inside HAL_EXTI_TriggerCallback to verify that the interrupt is triggered only when the button is pressed.
This article demonstrates the use of EXTI to enable asynchronous event detection using STM32CubeMX2. The example of blinking the on-board LED upon user button press highlights the practical application of EXTI in embedded systems, emphasizing its importance for responsive and efficient hardware interaction.