Jump to content

Push Button with Arduino Uno

From cognifront

Interfacing Pushbuttons with Arduino UNO for Digital Input Monitoring

Interfacing Pushbuttons with Arduino UNO for Digital Input Monitoring demonstrates how multiple pushbuttons can be interfaced with an Arduino UNO board to read digital input signals.

A pushbutton is a momentary switching device used to establish or interrupt an electrical connection when pressed.

In this experiment, four pushbuttons are connected to digital pins 5, 4, 2 and 3 of the Arduino UNO.

The Arduino UNO reads the status of each pushbutton using the digitalRead() function and displays the corresponding digital values on the Serial Monitor.

Pushbuttons → Digital Input Pins → Arduino UNO → Serial Monitor

Theory

A pushbutton is a mechanical switching device used to control the flow of electrical signals in an electronic circuit.

It is commonly used to provide manual input to microcontrollers and electronic systems.

A pushbutton generally operates in two states:

  • Pressed State: The internal contacts of the switch are connected.
  • Released State: The internal contacts return to their normal open or closed condition, depending on the switch configuration.

The Arduino UNO reads the pushbutton status using digital input pins.

The digitalRead() function is used to read the logic state of a digital input pin.

The two possible digital input states are:

  • HIGH (1): The input pin detects a HIGH voltage.
  • LOW (0): The input pin detects a LOW voltage.

The Arduino UNO uses the pinMode() function to configure the selected pins as INPUT.

The Serial Monitor is used to display the digital values received from all four pushbuttons.

Pushbutton Press → Digital Signal → Arduino UNO → Serial Monitor

Pushbutton Pins

A standard four-terminal tactile pushbutton generally contains two pairs of internally connected terminals.

Pushbutton State Electrical Condition Function
Pressed Switch contacts connected Completes the circuit
Released Switch contacts disconnected Opens the circuit

The exact digital value displayed by Arduino depends on the pushbutton wiring and the presence of pull-up or pull-down resistors.

Digital Input Reading

The Arduino UNO uses the following functions to read pushbutton inputs:

pinMode(pin, INPUT);

digitalRead(pin);

Where:

Function / Parameter Description
pinMode() Configures the selected GPIO pin
INPUT Configures the pin as a digital input
digitalRead() Reads the digital state of the selected input pin
HIGH Represents digital logic 1
LOW Represents digital logic 0

The Serial.begin(9600) function initializes serial communication at a baud rate of 9600 bits per second.

The Serial.print() and Serial.println() functions display the pushbutton readings on the Serial Monitor.

Applications

Pushbuttons are commonly used in:

  • Electronic control panels
  • Home automation systems
  • Industrial control systems
  • Calculator keypads
  • Digital input systems
  • Robotics control systems
  • Security and access control systems
  • User interface applications

Connections

In this experiment, four pushbuttons are connected to digital input pins 5, 4, 2 and 3 of the Arduino UNO.

Pushbutton Arduino UNO Pin Function
Pushbutton 1 Digital Pin 5 Digital Input 1
Pushbutton 2 Digital Pin 4 Digital Input 2
Pushbutton 3 Digital Pin 2 Digital Input 3
Pushbutton 4 Digital Pin 3 Digital Input 4

Important: The reference program configures all four pins as INPUT. Therefore, each input must have a proper pull-up or pull-down arrangement to avoid floating readings.

Recommended connection using external 10K ohm pull-down resistors:

Component Connection
Pushbutton 1 One terminal to 5V and other terminal to D5
Pushbutton 2 One terminal to 5V and other terminal to D4
Pushbutton 3 One terminal to 5V and other terminal to D2
Pushbutton 4 One terminal to 5V and other terminal to D3
Pull-down resistors 10K ohm from each input pin to GND

With this arrangement:

  • Button released = LOW (0)
  • Button pressed = HIGH (1)

Algorithm

The following algorithm is used to read the digital status of four pushbuttons:

  1. Start the Arduino IDE.
  2. Connect the Arduino UNO board to the computer using a USB cable.
  3. Declare four variables to assign digital pin numbers 5, 4, 2 and 3.
  4. Declare four integer variables to store the pushbutton readings.
  5. Initialize serial communication at 9600 baud rate.
  6. Configure all four pushbutton pins as INPUT.
  7. Read the status of Pushbutton 1 using the digitalRead() function.
  8. Display the reading of Pushbutton 1 on the Serial Monitor.
  9. Provide a delay of 500 milliseconds.
  10. Read and display the status of Pushbutton 2.
  11. Provide a delay of 500 milliseconds.
  12. Read and display the status of Pushbutton 3.
  13. Provide a delay of 500 milliseconds.
  14. Read and display the status of Pushbutton 4.
  15. Provide a delay of 500 milliseconds.
  16. Repeat the process continuously.

The overall operation is:

Read Button 1 → Read Button 2 → Read Button 3 → Read Button 4 → Repeat

Working

The Arduino UNO continuously monitors the digital status of four pushbuttons connected to its digital input pins.

The working sequence is:

  1. The Arduino UNO initializes serial communication at 9600 baud rate.
  2. Digital pins 5, 4, 2 and 3 are configured as INPUT.
  3. The Arduino reads the status of Pushbutton 1 using digitalRead().
  4. The corresponding value is displayed on the Serial Monitor.
  5. A delay of 500 milliseconds is provided.
  6. The same operation is performed for Pushbuttons 2, 3 and 4.
  7. The digital values of all four pushbuttons are displayed sequentially.
  8. The process repeats continuously.

Pushbutton → Digital Input → Arduino UNO → Serial Monitor Output

Observation

Reading Four Pushbuttons Using Arduino UNO

The following program is used to read the digital input values of four pushbuttons connected to the Arduino UNO.

int push1Pin = 5;
int push2Pin = 4;
int push3Pin = 2;
int push4Pin = 3;
int push1Value;
int push2Value;
int push3Value;
int push4Value;

void setup() {
  Serial.begin(9600);
  pinMode(push1Pin, INPUT);
  pinMode(push2Pin, INPUT);
  pinMode(push3Pin, INPUT);
  pinMode(push4Pin, INPUT);
}

void loop() {
  push1Value = digitalRead(push1Pin);
  Serial.print("Button 1 value : ");
  Serial.println(push1Value);
  delay(500);
  push2Value = digitalRead(push2Pin);
  Serial.print("Button 2 value : ");
  Serial.println(push2Value);
  delay(500);
  push3Value = digitalRead(push3Pin);
  Serial.print("Button 3 value : ");
  Serial.println(push3Value);
  delay(500);
  push4Value = digitalRead(push4Pin);
  Serial.print("Button 4 value : ");
  Serial.println(push4Value);
  delay(500);
}

Program Explanation

  • The variables push1Pin, push2Pin, push3Pin and push4Pin are assigned to digital pins 5, 4, 2 and 3 respectively.
  • Four integer variables are declared to store the digital readings of the pushbuttons.
  • The Serial.begin(9600) function initializes serial communication at 9600 baud rate.
  • The pinMode() function configures all four pushbutton pins as INPUT.
  • The digitalRead() function reads the digital state of each pushbutton.
  • The readings are stored in the respective integer variables.
  • The Serial.print() function displays the button identification.
  • The Serial.println() function displays the corresponding digital reading.
  • A delay of 500 milliseconds is provided after reading each pushbutton.
  • The loop() function repeats the complete operation continuously.

Observation Table

Sr. No. Pushbutton Arduino UNO Pin Released State Pressed State
1 Pushbutton 1 D5 LOW (0) HIGH (1)
2 Pushbutton 2 D4 LOW (0) HIGH (1)
3 Pushbutton 3 D2 LOW (0) HIGH (1)
4 Pushbutton 4 D3 LOW (0) HIGH (1)

Note: The observation table assumes the recommended external pull-down resistor connections.

Serial Monitor Output

The Serial Monitor displays the digital values of all four pushbuttons.

Example output when all buttons are released:

Button 1 value : 0
Button 2 value : 0
Button 3 value : 0
Button 4 value : 0

Example output when Pushbutton 1 and Pushbutton 3 are pressed:

Button 1 value : 1
Button 2 value : 0
Button 3 value : 1
Button 4 value : 0

Result

The Arduino UNO successfully interfaces with four pushbuttons connected to digital pins 5, 4, 2 and 3. The digital input values of all four pushbuttons are successfully read using the digitalRead() function and displayed on the Serial Monitor. Thus, multiple pushbuttons are successfully interfaced with the Arduino UNO for digital input monitoring.