Arduino Pushbutton Example : Learn Arduino

Arduino pushbutton example shows how to read a pushbutton with Arduino Uno. Pushbuttons (also spelled push-buttons) are widely used in calculators, phones and appliances. It closes the circuit when pushed and keeps it close until it is pressed. As soon as you release the button the circuit is open again. Here, we are going to use a push-button to flash built-in LED on Arduino Uno. The circuit is pretty simple! [caption id=”attachment_832” align=”alignright” width=”300”]Circuit diagram Circuit Diagram Showing Connection of Arduino Uno and a Push-button[/caption] Parts needed:

  1. Arduino Uno
  2. A push-button

Approximate Cost: ₹ 30 ($ 0.50)

         

Code:

// Declare the pins for the Button and the LED
int buttonPin = 12;
int LED = 13;

void setup() {
// Define pin #12 as input and activate the internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Define pin #13 as output, for the LED
pinMode(LED, OUTPUT);
}

void loop(){
// Read the value of the input. It can either be 1 or 0
int buttonValue = digitalRead(buttonPin);
if (buttonValue == LOW){
// If button pushed, turn LED on
digitalWrite(LED,HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
}

References:

  1. What is internal pull-up resistor?
  2. Wikipedia: Push-button
  3. Same project with an external pull-up resistor