#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

void app_main() {
  gpio_reset_pin(GPIO_NUM_2);   // It is necessary to reset the pin after butting up
  gpio_reset_pin(GPIO_NUM_4);   
  gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT); // set the direction of the pin as output
  gpio_set_direction(GPIO_NUM_4, GPIO_MODE_INPUT);  // Set the direction of the pin as input
  while (true) {
    /* Use the get and set level to get and set the gpio level to high (1) or low (0) */
    if (gpio_get_level(GPIO_NUM_4) == 0) {
      gpio_set_level(GPIO_NUM_2, 1);        
      printf("ON \n");
      vTaskDelay(500 / portTICK_PERIOD_MS);
      gpio_set_level(GPIO_NUM_2, 0);
      printf("OFF \n");
      vTaskDelay(500 / portTICK_PERIOD_MS);
    }
    else {
      gpio_set_level(GPIO_NUM_2, 0);
    }
  }
}
Loading
esp32-s3-devkitc-1