YouLearnt Community

November 29, 2026

How do I read a button with a GPIO pin, and what are pull-up resistors and debouncing?

A GPIO pin is a general-purpose pin on a microcontroller that your program can read or set as high or low. To read a button, you connect it so the pin sees a clear high or low level when pressed or released.

The main ideas: 1) A pin with nothing connected “floats” and can read random values, so you add a pull-up resistor (to the supply voltage) or a pull-down resistor (to ground). Many microcontrollers have internal ones you can switch on in software. 2) With a pull-up, the pin reads high when the button is open and low when pressed. 3) Real buttons bounce: the contact opens and closes quickly for a few milliseconds. Debouncing, in hardware or software, ignores these quick changes so one press counts once.

A common software approach is to accept a change only after the pin has stayed stable for a short time, such as 10 to 50 milliseconds.

Have you wired a button to a microcontroller before?