Translate

Arduino Digital Input: Reading a Pushbutton

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.

int pushButton = 2; // Digital pin for the button

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

  1. Upload the code to your Arduino board.

  2. Open the Serial Monitor (Tools > Serial Monitor).

  3. Set the speed to 9600 baud.

  4. Watch the screen: You should see a stream of 0s. When you press the button, the numbers will change to 1

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.