Skip to main content
Member
Associate II
May 21, 2021
Question

How to light up LED with a switch on the STM32 Board?

 while (1)

 {

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)) {

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, SET);

}

 }

}

I am using the STM32 WB55 Nucleoboard, I am wondering why when I press the button on the board it doesn't light up the LED. Is something wrong with my code?

This topic has been closed for replies.

4 replies

Tesla DeLorean
Guru
May 21, 2021

>>Is something wrong with my code?

Lacks context

Are clocks and pins properly initialized, can you change the register settings in the debugger, and set the button on/off

External or internal pull up on button?

if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)) 
 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
else
 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Member
MemberAuthor
Associate II
May 21, 2021
Yes, the clocks and pins are initialized. How do you change the register settings on the debugger to on/off?
It will be an internal pull-up on button since the board has them attached.
LMI2
Senior III
May 21, 2021

Try googling for instance HAL_GPIO_ReadPin

LMI2
Senior III
May 21, 2021

This is not Arduino, you need more than just 4 lines

I recommend starting with a tool called CubeMx. With it you can create settings for pins, that is, what is input and what is output, and then create project, where you can add your own code. After that the code from Tesla DeLorean (Community Member)

should work.