Arduino Digital Input (Push button)
Introduction
Embedded devices are all about getting input signals, processing these signals, and then sending out these processed signals in order to carry out a particular action. These signals can either be digital signals or analog signals. We will focus on digital input signals in this module and then discuss digital output and analog signals in subsequent ones (endeavor to check them out).
Here, we will use an Arduino controller to read digital input signals from a Push-button and then print the button states (1/0) on the Serial monitor.
​
​
Required components for this tutorial:
-
Arduino UNO R3
-
Push-button
-
Solderless breadboard
-
Jumper wires
​
​
Push button
​
A Push button is a device designed to close or open a circuit when a button or knop is depressed and to return to its normal position when released. Push buttons only send digital signals (0 and 1 / ON or OFF) to the Arduino. Some are push-and-hold, i.e., you have to push and hold the switch to activate and then release it to deactivate it. (These are found in doorbell systems). While some when depressed, click into position to activate and must be depressed again to deactivate. (These are used as ON/OFF buttons in most electronic devices).
Push button actual image
Push button circuit diagram
Now, let's hook up our push button to Arduino and read the states as promised.
​
​
Circuit
-
Place the push button across the groove in the middle of the breadboard (The section of the push button with the pins closer together should be on each side of the groove)
-
Connect the second pin of the push button (purple jumper wire) to Digital Pin 2 of the Arduino.
-
Connect the third pin of the push button (black jumper wire) to the GND pin of the Arduino.
Circuit diagram
Arduino Code
const int buttonPin = 2;
const int buttonState;
void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
​
void loop(){
buttonState = digitalRead(buttonPin);
Serial.print(buttonState);
delay(10);
}
​How code works
1. First, we need to declare the Arduino pin the push button signal pin will be connected.
​
const int buttonPin = 2;
​
​
2. Next, we will declare a variable to store the current button state.
​
const int buttonState;
​
3. The void setup() function will be called first, only once.
-
Here we initialize Serial communication, so that we can use the Serial Monitor to print the data we got from the button.
​
void setup(){
Serial.begin(9600);
-
We use "pinMode" to set the Arduino pin to the mode of choice. First, you name the Arduino pin then the mode you want to set the pin to. To use the push button with Arduino, we can either use an external resistor or we can invoke the internal pull-up resistor of the Arduino board. If you are using an external resistor - you set the mode to just "INPUT" but in this case, we use "INPUT_PULLUP".
​
pinMode(buttonPin, INPUT_PULLUP);
}
​
​
4. The void loop() function runs continuously.
-
Here, we use "digitalRead" to read digital values from the Arduino pin the push button is connected to and then send it to "buttonState" variable for storage.
​
void loop(){
buttonState = digitalRead(buttonPin);
​
-
Next, use the "Serial.print" function to print buttonState on the Serial monitor.
​
Serial.print(buttonState);
​
-
​​Lastly, we add a 100 milliseconds delay at the end. A fun way to explain this is that it gives the Arduino some breathing space to process after each button presses.
​
delay(100);
}