Arduino Pushbutton Example : Learn Arduino

arduino pushbutton example

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!

Circuit diagram
Circuit Diagram Showing Connection of Arduino Uno and a Push-button

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

Comments

avatar
  Subscribe  
Notify of