Subsection 2.1.2 Floating Pins and Pull-up Resistors
When a pushbutton is open (not pressed), it disconnects the pin from whatever it was connected to. If nothing else is driving the pin, the pin is said to be floating: its voltage is undefined, drifting with electrical noise, nearby signals, and even the movement of your hand near the board. Reading a floating pin produces unpredictable 0s and 1s that have nothing to do with the button state.
The standard fix is a pull-up resistor: a resistor connected between the pin and the supply voltage (3.3 V). When the button is open, the resistor pulls the pin to 3.3 V, giving it a well-defined logic 1. When the button is pressed and connects the pin to ground, the connection to ground overrides the pull-up, and the pin reads logic 0. This “active-low” convention (pressed = 0, released = 1) is the most common arrangement for buttons.
Alternatively, a pull-down resistor connects the pin to ground, so the unpressed state reads 0 and pressing (which connects to 3.3 V) reads 1. The STM32C031C6 has pull-up and pull-down resistors built into the chip, selectable per pin via the Pull-up/Pull-down Register (PUPDR), so in most cases you do not need an external resistor at all.
