Connecting push button to microcontroller
simulate this circuit &ndash; Schematic created using CircuitLab

The above setup reads '1' when switch = off and '0' when switch = on

\$R\$ is an external pull-up resistor that avoids the floating of input in case the digital IO pin of the controller has no internal pull-up. In this case, it will pull the pin to 5V when switch = off.

Alternative setup:







simulate this circuit

The above setup reads '1' when switch = on and '0' when switch = off

\$R\$ is an external pull-down resistor here.