digitalRead Serial Monitor with Button

Reads a digital input on pin 2, prints the result to the serial monitor.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0. 
When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.


- One leg of the button connect a resistor.
- Green wire connect from digital pin 2 to one leg of the pushbutton.
- Leg of the resistor to ground.
- The other leg of the button connects to the 5 volt supply.

Hardware Required:

10k ohm resistor


//Digital Read with Button

int pushButton =2; //digital pin 2 has a push button attached to it. Give it an name

//the setup routine runs once when you press reset
void setup() {

  Serial.begin(9600); //initialize serial comm. at 9600 bits per second

  pinMode(pushButton, INPUT); //make the push button's pin an input

//the loop routine runs over and over again forever
void loop() {

  int buttonState = digitalRead(pushButton); //read the input pin

  Serial.println(buttonState); //print out the state of the button

  delay(1); //delay in between reads for stability