const int ledPin = 9;
const int buttonPin = 5;
int lastButtonState = HIGH;

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

  Serial.begin(115200);
  Serial.println();
  Serial.println("Click on the button");
}

void loop() 
{
  int button = digitalRead(buttonPin);

  if(button != lastButtonState)
  {
    if(button == LOW)
    {
      Serial.println("Button Pressed");

      digitalWrite(ledPin,HIGH);
      delay(500);
      digitalWrite(ledPin,LOW);
    }
    lastButtonState = button;
  }

  delay(20);  // delay to avoid the bouncing of the button
}
Loading
esp32-c3-devkitm-1