Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Voltaat Arduino Ultimate Kit
40 min
Share

Display the state of a push button using Arduino

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.

Project Video

Overview

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.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
Push button Switch (5 Pack) - 12mm
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the push button and the Arduino, as shown in the image below.

Mini-Push-Button-wiring

 Connections from the push button:


     • First pin→ Breadboard 5v line


     • Second pin→ Arduino pin 2


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

Coding




/*
 Voltaat learn (http://learn.voltaat.com/)
 Link for full tutorial:

 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

know when your plant needs watering

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

know when your plant needs watering

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.

know when your plant needs watering

Resources

No items found.