#define BUTTON 2 // ESP32 GPIO16 pin connected to button's pin
#define BUZZER 4 // ESP32 GPIO21 pin connected to Buzzer's pin

void setup() 
{              
  pinMode(BUTTON, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
  pinMode(BUZZER, OUTPUT);       // set ESP32 pin to output mode
}

void loop()
{
  int buttonState = digitalRead(BUTTON); // read new state

  if (buttonState == LOW) 
  {
    digitalWrite(BUZZER, HIGH); // turn on
  }
  else
  if (buttonState == HIGH) 
  {
    digitalWrite(BUZZER, LOW);  // turn off
  }
}