How to operate the push button with Arduino: 3 basic examples

Learn how to operate push button with Arduino Uno using simple examples for input control.

How to operate the push button with Arduino

Push buttons are the basic on-off switching buttons most commonly used in less to highly complex electronic devices. Push buttons can connect two points whenever they are pressed. The Push-button acts as a manually operated control device. When the button is pressed that means the short circuit is achieved between two points. On the other side when the button is not pressed then the open circuit or no connection developed between the points.

In this article, I will explain to you all the basics about the push button with Arduino Uno. A push-button is a very simple control device but we can operate it with pull-up and pull-down resistors. I will explain to you how an LED can be operated using a push button with a pull-up and a pull-down resistor. I will also explain to you how a buzzer can be operated with the help of a push button. All these examples will help you to understand all the basics about the push button.

The push-button has four legs but we can use only two because of their internal structure. We can connect the push button directly with the VCC or 5-volt power supply. After that when we press it the software will read it as HIGH until we change the state and perform the operation. But when the button is released after that system will get confused and will not be able to recognize what is connected to the input.

As a result, the software will read HIGH or LOW values non-uniformly and assume some noise in this condition, due to which unexpected output will be observed. To avoid this situation it is necessary to connect pull-up or pull-down resistors with the push button.

How to operate LED using a push button (with an internal pull-up resistor)

Arduino has internal pull-up resistors (which means the resistors are connected to power internally). If we do not want to connect an external pull-down resistor then we can use these internal resistors directly with the help of the INPUT_PULLUP argument in pinMode(). This connection effectively inverts the operation of the push button. In this example, as the button is not pressed then the LED will be in one state, and as the button is pressed then the LED will be in the off state.

Required Component

Connection diagram

How to operate the push button with Arduino


Circuit Description

According to the connection diagram, one terminal of the pushbutton is connected to the 5-volt power supply, and the other terminal is connected to pin 7 of the board and GND. The shorter leg of the LED is associated with the GND and the longer leg is connected with pin 8 along with the 220-ohm resistor.

Code

/*
*How to operate LED by push button with internal pull up resistor using Arduino uno
*
*This is a very basic example which will help you to understand how LED can be operated by a push button with INTERNAL_PULLUP argument with pinMode() command.
*
*for more detail about this project please visit:https://arduinounomagic.com/2019/03/how-to-operate-push-button-with-arduino.html#more
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/
const int push_button=7; //Push buton is connected to pin 7
const int LED = 8; // LED is connected to pin 8
 
int button_state = 0; //Variable to store the bushbutton state
 
void setup()
{
  Serial.begin(9600);//Intiallize the serial connection
  pinMode(LED, OUTPUT); // Intiallize LED as output
  pinMode(push_button, INPUT_PULLUP); //Intiallize push button in input pull up mode
}
 
void loop()
{

  button_state = digitalRead(push_button);// Read the push button state

  if (button_state == 1) //Check the state is HIGH or LOW and operate LED accordingly
  {
     digitalWrite(LED, 0);// Turn off LED 
  } 
  else 
  {
    digitalWrite(LED, 1); //Turn on LED
  }
}

Output

Check out the video to understand, how the circuit is behaving according to the code in real time.

How to operate push button with internal pull up resistor using Arduino Uno

How to operate LED by Push Button (with external pull-down resistor)

In this example, as the button is pressed only then the LED will be on, and as the button is not pressed then the LED will be off. The LED will be “ON” only for the duration for which the button is pressed otherwise it will be off.

Required Component

Connection Diagram

How to operate LED by push button (with external pull-down resistor):

Circuit Description

According to the connection diagram, one terminal of the switch is connected to digital pin 7 and the other pin is connected to the 5-volt power supply pin of the Arduino board. One terminal of the 1 K ohm pull-down resistor is connected to the switch and the other terminal is connected to the GND. The shorter leg of the LED is connected with the GND and the longer leg is associated with the digital pin 8 along with the 220-ohm resistor.

Code

/*
*How to operate push button using Arduino uno
*
*This is a very basic example which will help you to understand how LED can be operated by a push button with external pull down resistor using arduino uno.
*
*for more detail about this project please visit:https://arduinounomagic.com/2019/03/how-to-operate-push-button-with-arduino.html#more
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com
*/

const int push_button = 7; // push button is connected to pin number 7
const int LED =  8;  // LED is connected to pin number 8
int button_state = 0;   // variable to read the status of push button
 
void setup() {
  
  pinMode(LED, OUTPUT); //set LED as output
  pinMode(push_button, INPUT); //set push button as input
}
 
void loop() {
  
  button_state = digitalRead(push_button); //read the status of the push button
   
  if (button_state == HIGH) //check the status of the push button ethier HIGH or LOW
  {
  digitalWrite(LED, HIGH); //if condition is true, LED turns on
  } 
  
  else
  
  {
  digitalWrite(LED, LOW); //if condition is false, LED turns off
  }
}

Output

Check out the video, this will help you understand the workings of the circuit more effectively.

How to operate the buzzer using a push button with Arduino Uno

In this example, a buzzer is operated with the help of the push button. As the button is pressed, the buzzer will generate a sound; as the button is released, the buzzer will stop immediately. Therefore buzzer will operate only for the duration for which the pushbutton is pressed. I have used the external pull-down resistor along with the push button.

Required Component

Connection Diagram

How to operate the buzzer using a push-button with Arduino Uno:

Circuit Description

A buzzer is a two-terminal (one is shorter leg and another one is longer leg) device. The longer leg of the buzzer is connected to pin 5 of the Arduino board. The shorter leg of the buzzer is connected to the GND terminal of the Arduino Uno board.

As we know push button has a fore terminal and we can provide connection only on two of them because of their internal structure. One terminal of the pushbutton is connected with a 1 k ohm pull-down resistor and pin 8 of the Arduino board and another terminal is associated with the 5-volt power supply pin of the Arduino board.

Code

/*
*How to operate push button using Arduino uno
*
*This is a very basic example which will help you to understand how buzzer can be operated by a push button using arduino uno.
*
*for more detail about this project please visit:https://arduinounomagic.com/2019/03/how-to-operate-push-button-with-arduino.html#more
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/
const int push_button=8; //connect push button to pin number 8

const int BUZZER =  5;  //connect Buzzer to pin 5



void setup() {
  Serial.begin(9600); //intiallize serial communication
  pinMode(BUZZER, OUTPUT);    // Define buzzer as output.   
  pinMode(push_button, INPUT);  // Define push button as Input.   

}


void loop(){
  
  int state = digitalRead(push_button); // variable to store status of push button

  if (state == 1) // check the status of push button either HIGH (1) OR LOW (0)
   {     
     digitalWrite(BUZZER, 1);  // If input is High, buzzer operated
   } 
  else  
  {
     digitalWrite(BUZZER, 0);  // For every other condition make buzzer off
  }
  delay(10);
}

Output

Check out the video provided below, it will help you understand the workings of the circuit.

How to operate buzzer using push button by Arduino

Hope you enjoy the article.

If you like the article, let me know in the comments.

If you have any questions, feel free to ask. I will try to solve your issue.

Leave a Reply

Your email address will not be published. Required fields are marked *