Interfacing a Push Button with Arduino.

1,787 views

Introduction

Interfacing a push button with an Arduino is a simple yet essential skill for anyone interested in electronics and microcontrollers. Push buttons can be used in various applications such as controlling LEDs, motors, and other electronic components.

In this article, we will explore the basics of interfacing a “Push Button” with an Arduino, including the required hardware and software, as well as the steps to read the state of the button and perform an action based on it. By the end of this article, you will have the knowledge to build your own projects using push buttons and Arduino.

What is Push Button?

A push button is a simple mechanical switch that allows an electrical circuit to be opened or closed by pressing the button. It consists of a small button or disk that is pushed by a person’s finger to activate the switch. When the button is pressed, it makes contact with the electrical contacts inside the switch, which completes the circuit and allows current to flow.

Hardware Components

Connecting a Push Button with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
Pushbutton1
Breadboard1
Jumper Wires1

Push Button Pinout

Active-Low Push Button Circuit

Make connections according to the circuit diagram given below.

Active-High Push Button Circuit

Connections

ArduinoPush ButtonLED
5vVCC
D8orange Wire
GNDGNDGND
D4+

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /ProgRam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

#define LED_PIN 2
#define BUTTON_PIN 8
 
unsigned int button_status = 0;
 
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}
 
void loop() {
 
  button_status = digitalRead(BUTTON_PIN);
 
  if (button_status == 0) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Code Explanation

This Arduino code sets up the connections between a push button and an LED using digital input/output pins on the Arduino board.

The first two lines of code use the #define preprocessor directive to define LED_PIN and BUTTON_PIN as constants with values of 2 and 8, respectively. This makes it easier to use these pins throughout the code without having to remember their specific numbers.

The unsigned int button_status variable is then declared and initialized to 0. This variable will store the current state of the button, which will be read in the loop() function.

In the setup() function, the pins are initialized as inputs and outputs using pinMode(). LED_PIN is set as an output, while BUTTON_PIN is set as an input with a pull-up resistor enabled.

In the loop() function, digitalRead() is used to read the current state of BUTTON_PIN and store it in the button_status variable. If the button is pressed and its state is LOW (since the pull-up resistor is enabled, the pin is normally HIGH), the if statement is executed, and the LED_PIN is set to HIGH, turning on the LED. If the button is not pressed and its state is HIGH, the else statement is executed, and the LED_PIN is set to LOW, turning off the LED.

Overall, this code sets up a simple circuit where a push button controls an LED, and the LED turns on when the button is pressed and turns off when the button is released.

Applications

Here are some applications of push buttons:

  • On/off switches for electronic devices such as lights, appliances, and toys
  • Selection buttons for menu navigation on electronic displays
  • Start and stop buttons for machines and equipment in industrial and automation settings
  • Reset buttons for electronic devices and systems
  • Emergency stop buttons for safety systems and machines
  • Push-to-talk buttons for communication devices such as radios and intercoms
  • Call buttons for elevators and intercom systems
  • Doorbell buttons for home and office entrances
  • Game controller buttons for video game consoles and arcade machines
  • Trigger buttons for cameras and other photographic equipment

Conclusion

Interfacing a push button with an Arduino is a fundamental skill that opens up a world of possibilities in electronics and robotics. By following the steps outlined in this article, you can easily connect a push button to an Arduino and use it to control various electronic components.