Push Button Counter

Learn how to read a push button and count the number of presses using Arduino.

Components Needed

  • Arduino Uno
  • Push Button
  • Breadboard
  • Jumper Wires

  • How It Works

    The push button is connected to digital pin 2. Every time the button is pressed, the Arduino detects the change and increases a counter variable by one. The current count is then displayed in the Serial Monitor, allowing you to track how many times the button has been pressed.

  • What You'll Learn

    • How to read a push button using Arduino.
    • The difference between HIGH and LOW digital signals.
    • How to use the INPUT_PULLUP mode.
    • How to create and update variables.
    • How to display data using the Serial Monitor.
  • Code

    const int buttonPin = 2;

    int counter = 0;
    int lastState = HIGH;

    void setup() {
    pinMode(buttonPin, INPUT_PULLUP);

    Serial.begin(9600);

    Serial.println("Button Counter");
    Serial.println("Press the button...");
    }

    void loop() {

    int currentState = digitalRead(buttonPin);

    if (lastState == HIGH && currentState == LOW) {
    counter++;

    Serial.print("Count: ");
    Serial.println(counter);

    delay(200);
    }

    lastState = currentState;
    }