Build the following circuit:

Run the code below and press the button to watch numbers appear in the Serial Terminal:

const int btn_pin = 2;

// We need to remember the previous button state between loops
int btn_prev = HIGH;

// Counter
int counter = 0;

void setup() {
  
  Serial.begin(9600);
  
  // Set up pins
  pinMode(btn_pin, INPUT_PULLUP);
}

void loop() {
  
  int btn_state;
  
  // Read current button state
  btn_state = digitalRead(btn_pin);
  
  // If the button was previously HIGH and now LOW, it's been pressed
  if ( (btn_prev == HIGH) && (btn_state == LOW) ) {
    
    // Increment and print counter
    counter++;
    Serial.println(counter);
  }
  
  // Remember the previous button state for the next loop iteration
  btn_prev = btn_state;
}

Simulator: https://tinkercad.com/things/aHZWTIEr5RK