Display the state of a push button using Arduino

Many of the electronic devices we use in our daily life include control buttons that make them easier to use. These control buttons are called push buttons.


In this tutorial, we will learn more about bush buttons and how to use them. We'll use the push button in a simple project to control the Arduino's internal LED and print the current state of the button directly on your commuter.


Let's have a look at it together!


Overview


A push button is an electrical component that is used to connect or disconnect a circuit. They come in a variety of shapes and sizes but with the same function.

Getting the items


For this project you will need the following components you can buy them from our store.

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Mini Push button Switch (5 Pack)
1 QAR
Sale Off
Voltaat 1/4 Watt Resistor (20 Pack)
2 QAR

Make sure you also have a breadboard and jumper wires to connect everything together

Sale Off
Voltaat Full-size Breadboard
20 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


To set up the push button in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the push button and the Arduino. Once the push button and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.



Connections from the push button:

• Right pin→ Breadboard 5v line

• Left pin→ Arduino pin 2

• Left pin→10KΩ resistor → Breadboard GND line


The bush button has 4 pins. Each pair is connected to each other, but pins A and B are connected when the push button is pressed, as shown in the following image.



Coding


The purpose of this sketch is to control the Arduino's internal LED using the push button. We will also use the Arduino to print the push button status as well as its digital read directly on your computer.


The code is simple and does not require any libraries; you may use it later in more complex projects, such as operating a robot arm.


/*
Voltaat learn (https://www.voltaat.com/pages/voltaat-learn)
Link for full tutorial: https://bit.ly/3Dz4VSH

Tutorial: Display the state of a push button using Arduino

The purpose of this sketch is to control the Arduino's internal LED using the push button.
We will also use the Arduino to print the push button status
as well as its digital read directly on your computer.

Connections from the push button:
• First pin→ Breadboard 5v line
• Second pin→ Arduino pin 2
• Second pin→10KΩ resistor → Breadboard GND line

*/


//Define the variable pushButton to Digital Input Pin 2
const int pushButton =2;
//Define the variable buttonState
int buttonState;


//Commands inside void setup run once
void setup(){
//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
//pushButton is defined as an input
pinMode(pushButton,INPUT);
//Arduino builtin led is defined as an output
pinMode(LED_BUILTIN,OUTPUT);
}

//Commands inside void loop run forever
void loop(){

//Read the value of digital input from pushButton and assign it in the variable buttonState
buttonState =digitalRead(pushButton);
if(buttonState ==HIGH)
{
//Turn on the arduino builtin led
digitalWrite(LED_BUILTIN,HIGH);
//Print to serial monitor
Serial.print("Push Button is pressed ");
Serial.print("| Button state: ");
Serial.println(buttonState);
//Delay for one millisecond for more stability
delay(1);
}
else
{
//Turn off the arduino builtin led
digitalWrite(LED_BUILTIN,LOW);
//Print to serial monitor
Serial.print("Push Button is released ");
Serial.print("| Button state: ");
Serial.println(buttonState);
//Delay for one millisecond for more stability
delay(1);
}
}

Testing it out




Now you must have correctly wired the soil moisture sensor to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.


You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.



The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It allows us to send and receive different commands as well as view information directly from the Arduino.


Now, as shown in the image below, the serial monitor displays the state of the push button, whether it is pushed or released. as well as the digital read from it.


You can also notice that the Arduino's built-in led is switched on and off in response to the push button state.


You should also make sure you have chosen the right baud rate (9600) as specified in the code.


Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials


LCDs are used in a range of everyday applications, including the automobile radio and the house air conditioning remote. They display data and let you control it through menus.


A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.


Most of us are familiar with the switches used in household devices. A relay is a type of switch that can be connected to an Arduino or any other microcontroller.