Skip to main content
Explorer II
September 29, 2025
Solved

GPIO , Button detection ( USER1) on STM32N6570-DK

  • September 29, 2025
  • 3 replies
  • 369 views

Post edited by ST moderator to be inline with the community rules especially with the code sharing. In next time please use </> button to paste your code. Please read this post: How to insert source code

Hi  , 

I’m currently playing around with the STM32N66570-DK kit.

I’m having a simple problem with button detection. For testing, I’m just using the USER1 button  (BP1 in my code ) and LED1.

I couldn’t get the button working properly with interrupts, so in this code I just read the state of USER1 ( BP1) and turn LED1 on or off accordingly.  The USER1 button is always read as the same value, no matter whether I press it or not.  

 

in debug Mode : 

GPIOx const GPIO_TypeDef * 0x56020800
GPIO_Pin uint16_t 8192
bitstatus GPIO_PinState GPIO_PIN_RESET

 

Here are the code and screenshots. The code was generated with CubeMX.

Thanks for your help!

Once I get the button detection working, I’ll move on to using interrupts.

Emmanuel

main.h :

#define BP1_Pin GPIO_PIN_13
#define BP1_GPIO_Port GPIOC
#define LED1_Pin GPIO_PIN_1
#define LED1_GPIO_Port GPIOO

 

main.c :

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */



/* USER CODE BEGIN 3 */

// code to test the LED1

//HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);

//HAL_Delay(500);

//HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);



// Read the button USER1



if(HAL_GPIO_ReadPin (BP1_GPIO_Port, BP1_Pin) == GPIO_PIN_RESET)

{



//Turn LED ON

HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);

}

else

{

//Turn LED OFF

HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);

}

}

/* USER CODE END 3 */

}



-----------------------------------------------------------



static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* USER CODE BEGIN MX_GPIO_Init_1 */

/* USER CODE END MX_GPIO_Init_1 */



/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOO_CLK_ENABLE();



/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);



/*Configure GPIO pin : BP1_Pin */

GPIO_InitStruct.Pin = BP1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(BP1_GPIO_Port, &GPIO_InitStruct);



/*Configure GPIO pin : LED1_Pin */

GPIO_InitStruct.Pin = LED1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);



/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */

}

 

    This topic has been closed for replies.
    Best answer by mfgkw

    One more issue: following your code you have no pull up or pull down resistor to define the state of PC13 if the button is open.

    Following the MB1939-N6570-C02 Board schematic (https://www.st.com/resource/en/schematic_pack/mb1939-n6570-c02-schematic.pdf) the button switches to 3v3. There is a resistor R225 (10k) to GND, but marked as DNF.

    So I suggest to enable an internal pull down for the button GPIO to be sure.

    3 replies

    Technical Moderator
    September 29, 2025

    Hello,

    You share a bed resolution images.

    But to be sure, is MX_GPIO_Init() called in the main before the while loop. You shared only the while loop.

    mfgkwAnswer
    Graduate II
    September 30, 2025

    One more issue: following your code you have no pull up or pull down resistor to define the state of PC13 if the button is open.

    Following the MB1939-N6570-C02 Board schematic (https://www.st.com/resource/en/schematic_pack/mb1939-n6570-c02-schematic.pdf) the button switches to 3v3. There is a resistor R225 (10k) to GND, but marked as DNF.

    So I suggest to enable an internal pull down for the button GPIO to be sure.

    emmanuel_Author
    Explorer II
    September 30, 2025

    Hi mfgkw ! ,

     

    Perfect, thanks for your help. Configuring BP1 with a pull-down has solved the problem !

     

    Cheers

     

    Emmanuel