A button is a simple switch with 2 values: on or off, which is 1 or 0 respectively. No surprise there. This post details how to hook it up and read its value into your Arduino program.
int btn = 4; // a variable for the button's digital pin
Serial.begin(9600); // start reading values from the serial port
pinMode(btn, INPUT); // set the button's pin as an input
Serial.println(digitalRead(btn)); // read the value of the button and print it to the serial monitor
delay(100); // delay the loop for 0.1 sec
Electricity will constantly try to pass through the button (the path of least resistance); however, it only works when the button is pressed. If the button is pressed, it’s able to flow through the button and receive power from the 5V pin (red wire). This will read a 1 into your program to say that it’s receiving power. Otherwise, it’s forced to pass through the resistor and to ground. This will read a 0 into your program to say that it’s NOT receiving power.
Now that you know how to hook up a button and take it a step further. By using a simple if-else statement to determine if the button is pressed, you can easily turn on an LED when a button is pressed.
if(digitalRead(btn) == 1) digitalWrite(13, HIGH); // if button is pressed, turn LED on
else digitalWrite(13, LOW); // else turn LED off