Push Buttons; used in various electronic devices, for example, the power button of a smartphone, computer keyboard, TV Remote buttons, handheld calculator etc. These switches are normally open and connect two points in an electronic circuit as long as they are pressed. Here we will read the signal generated by the push button with the help of Arduino microcontroller. The example turns on an LED when you press the button and turns it off when released.

1. CONCEPTS

What is a Push Button?

Push-button is a very simple mechanism which is used to control electronic signal either by blocking it or allowing it to pass. This happens when you mechanically apply pressure to connect two points of the switch together.

Although a switch can be used alone without any micro-controller, to increase the functionality we connect it to an Arduino and program it for different applications. In addition, we can read various other states of a push-button using a program which will not be possible if you use it otherwise.

arduino push button diagram
Arduino Push Button using tactile metallic dome

Figure 1: Push Button

In the graphic above, you can see a tactile metal dome inside the switch(Figure 1.2). When pressure is applied on the push button (Figure 1.3) this metallic dome flattens and connects the conducting points of both sides. Moreover in Figure 4, you can see that pins a-b and c-d are internally shorted so you can use either a and d or b and c for connections.

2. COMPONENTS

3. CONNECTIONS

While making the connections make sure to use a pull-down resistor (Read FAQ’s at last for more info) because directly connecting two points of a switch to the circuit will leave the input pin in floating condition and the circuit may not work according to the program. Alternatively, you can also use built-in pull-up resistors of 20K by setting pinMode as INPUT_PULLUP.

Arduino Push Button Switch with led
Arduino Push Button Switch with LED

Figure 2: Push Button connections with arduino

4. PROGRAM

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM: Arduino Push Button Switch with LED
  Read the signal generated by the push button
 
  Connections:
  13    LED
  2     Switch
*/
const int LED=13;
const int button=2;
void setup() {
  pinMode(LED, OUTPUT);           // sets the digital pin 13 as output
  pinMode(button, INPUT);         // sets the button pin 2 as input
}

void loop() {
  int val = digitalRead(button);  //reads the value on digital PIN
  digitalWrite(LED, val);         //write the value on LED
}

5. FUNCTIONS/VARIABLE QUALIFIERS USED


const

const is an abbreviation for the word constant. It’s a Variable qualifier which makes any variable read-only. You can use it like any other variable but you cannot change its value in the program. It’s a good practice to name your pins using a constant qualifier.


pinMode ()

Description: Configure specific pin to behave either as input or output.

Syntax: pinMode (pin,MODE);

  • pin: the Arduino pin number to set the mode of.
  • MODE: INPUT, OUTPUT, or INPUT_PULLUP.

Returns: Nothing

Notes and Warnings:

  • It is only applicable to digital pins.

digitalRead ()

Description: Reads digital values from digital pins.

Syntax: digitalRead (pin);

  • pin: the Arduino pin number.

Returns: HIGH or LOW

Notes and Warnings:

  • If the pin is not connected to anything, the digitalRead function can return HIGH or LOW values randomly.
  • You can also use analog input pins as digital pins except in Arduino Nano and Pro Mini.
  • Avoid using pin 13 as an input pin, because it has an internal LED

digitalWrite ()

Description: Write a HIGH or LOW value to a digital pin.

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino pin number.
  • value: HIGH or LOW

Returns: Nothing

Notes and Warnings:

  • It is very important to set pinMode() as OUTPUT first before using digitalWrite() function on that pin.
  • If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim.
  • The analog input pins can be used as digital pins, referred to as A0, A1, etc. However, the exception is the Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only be used as analog inputs.

FAQ’s

Why using a pull-up or pull-down resistor is compulsory with the Arduino push button?

A digital circuit can be either HIGH or LOW but when we don’t connect anything to the input pin we are leaving it to a floating state; which means it will randomly pick HIGH or LOW voltage. As a result, your circuit will behave in an abnormal manner.

To fix this we use either pull-up or pull-down resistors as shown in the Figure below. By using them we make sure that when the button is not pressed, the input pin remains either HIGH or LOW and do not float.

Figure: Pull-Up and Pull Down Circuit Diagram
I don’t want to use an external pull-up or pull-down resistor. Is there any other way?

Well, the ATmega328 chip on your Arduino Board consists of internal pull-up resistors which can be accessed using the software. However, keep note that in this case, the push button will give LOW voltage when pressed and HIGH voltage when released.

Still, having doubts?

Ask your questions in comment section below or Contact us.

This Post Has 2 Comments

  1. Abdul Khayyum

    Hi

    What is IDE?

    1. ashish

      IDE stands for “Integrated Development Environment”. Earlier there were different software for writing the code and different software for uploading that code to your microcontroller. But now we have this All in one software which is capable of editing, compiling and uploading that code to the microcontroller. This type of software is called an IDE.

Leave a Reply