LED lights up even though there is not supposed to be an output from the STM32
You need a pull down resistor in you switch GPIO pin.

Connect a 10K resistor from the pin to ground.

The GPIO is probably floating causing the input to switch on/off very quickly when the button is not pressed.  This will cause your LED to glow slightly or give random behavior.







simulate this circuit &ndash; Schematic created using CircuitLab