Reading a pushbutton is one of the first steps in learning how to make your electronics projects interactive. In this guide, we will use an Arduino Uno to detect when a button is pressed and display that status on your computer screen.
Components Needed
Arduino Uno board.
Pushbutton (Tactile switch).
10k-ohm Resistor (used as a pull-down resistor).
Jumper wires and a Breadboard.
The Circuit Diagram
To get this project working, the wiring must be precise. Without a pull-down resistor, the Arduino pin will "float," causing the Serial Monitor to show random data.
Pin 2: Connect to one side of the pushbutton.
5V: Connect to the other side of the pushbutton.
GND: Connect to Pin 2 through the 10k-ohm resistor.
The Working Code
Copy and paste this code into your Arduino IDE. This script reads the voltage on Pin 2 and sends it to your computer.
void setup() {
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// Make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
void loop() {
// Read the input pin (0 = Not pressed, 1 = Pressed):
int buttonState = digitalRead(pushButton);
// Print out the state of the button:
Serial.println(buttonState);
delay(1); // Small delay for stability
}
How to Verify It's Working
Upload the code to your Arduino board.
Open the Serial Monitor (Tools > Serial Monitor).
Set the speed to 9600 baud.
Watch the screen: You should see a stream of
0s. When you press the button, the numbers will change to1