• Pooja Velavan

How to read a Digital Value from Arduino using Push Button?

Digital Values are used to represent the analog values in discrete bands. These values help the devices to process the function easier, faster, and in a secure way. The two digital values are 0's and 1's. By using these values multiple operations can be performed. Generally, the 1 represents a "HIGH" value and the 0 represents the "LOW" value.


This article describes how to read a Digital value from Arduino using Push Button and the Arduino Code. Let's begin with the requirements to build the project.


Hardware Requirements:


1. Arduino UNO R3


Arduino UNO R3
Arduino UNO R3

Arduino #UNO R3 is the most commonly used and latest version among the Arduino board. Arduino Board can address inputs such as light on a sensor, Twitter information, a finger on a button, and turn it to an output such as initiating a motor, turning on an LED, advertising something online. All of this can be done by transferring a set of instructions to the microcontroller on the board by the use of the #Arduino programming language.


Buy Arduino UNO R3 from here.


2. Push Button


Push Button
Push Button

Push Buttons are simple devices used for switching control. It is easy to use and starts or stops the function when connected in a circuit. The Push Button has 4 pins.


Buy Push Button from here.


4. Resistor


Resistor
Resistor

Resistors are passive devices that restrict the flow of current or divide the voltage through the circuit. The input power passes through these resistors and then to the sensors to avoid damage. Here in this project, a 10K ohm resistor is used.


Buy Resistors from here.


5. Jumper Wires


Jumper Wires
Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit. There are three types male to female, female to female, and male to male.


Buy Jumper Wires from here.


6. USB Cable


USB Cable
USB Cable

This cable is used to connect the #development boards to the power supply. It also helps in the transmission of data.


Buy USB Cable from here.


Software Requirements:


1. Arduino IDE


Arduino IDE LOGO
Arduino IDE LOGO

Arduino #IDE (Integrated Development Platform) which is used to #interface the boards like NodeMCU to run the desired code designed to interface hardware devices.


Circuit Connections:


Circuit Connection
Circuit Connection

To build the circuit breadboard or PCB (Printed Circuit Board) anything can be used. The breadboard helps in testing the circuit and the PCB will provide a permanent circuit in the board. The connection between the Arduino Board and the #Push Button by using jumper wires involves the following steps:

  • The Push Button has 4- pins in which one of the pins is connected to the Digital pin 2 of the Arduino board.

  • Another pin is connected to the 10 K ohm resistor and another side of the resistor is connected to the negative terminal of the breadboard.

  • Finally, in the remaining 2 pins, one is connected to the 5Volt of the Arduino, and another one is connected to the Ground pin of the Arduino board by connecting to the negative terminal of the breadboard to establish a common ground connection.

Code Explanation:

The code is simple and very easy to understand.

const int buttonPin = 2;

Assign the Arduino pin in which the button pin is connected and from which the input is taken.

int buttonState = 0;         // variable for reading the pushbutton status

Initially, assign the button state to 0 for reading the variable from the Push Button.

void setup() {

  Serial.begin(9600);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

The setup( ) function defines all the necessary functions inside it needed for the components and the circuit to work accordingly. First, the inbuilt function pinMode( ) is used to declare the particular pin of the Arduino board as Input/Output. Digital Pin 2 is declared as an input pin here.

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

Inside the loop ( ) function, which is used to run the particular code repeatedly. Here the variable buttonState is assigned digitalRead( ) function to read the value from the button pin 2 that is connected to the Arduino.

Serial.print("Digital value: ");
  Serial.println(buttonState);
  delay(150);
}

On reading the input from the pin the value is stored in the buttonState. The Serial.print function is used to print the output in the serial monitor. With a delay of 150 milliseconds, the output is continuously printed in the serial monitor that ends the loop( ) function.


Complete Code:


const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

  Serial.begin(9600);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  Serial.print("Digital value: ");
  Serial.println(buttonState);
  delay(150);
}

For the complete code for this program, download the following document.

Button
.txt
TXT • 934B

Note: Please change the file extension to .ino(Arduino IDE) file format before you upload the code to the Arduino board.


Working:


The working of the circuit is very simple. The Push #Button receives the supply voltage from the Arduino board. Initially, the button state is assigned to 0. The below image shows the picture of the circuit when the code is not applied.


When the circuit is OFF
When the circuit is OFF

On uploading the program to the Arduino board by using a #USB cable. Compile the code and open the Serial Monitor, it displays the Digital value has "0" that we initially assigned it. Now press the push button to change the Digital Value to "1" and it will be displayed on the serial monitor. You can again change the value by pressing the Push Button.


Video explanation with the practical implementation, for a more beneficial understanding.


Video By - Harshit Gupta

Take the First step today to design the circuit, learn to read the digital values by using the #Pushbutton. With the help of this basic designing article and circuit build your own circuits by applying them in different applications by adding elements and modifying the code according to your wish.


See Also:

  1. How to control an LED using Push button and Arduino?

  2. Interfacing Temperature Sensor with Arduino

  3. Interfacing DC Motor with Arduino