#include "driver/gpio.h"
Documentation
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html
Input Pins
Combined method
const gpio_config_t InputPin = {
.pin_bit_mask = BIT64(GPIO_NUM_4),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
};
gpio_config(&InputPin);
if (gpio_get_level(GPIO_NUM_4))
//Do something
Individual functions method
gpio_set_direction(GPIO_NUM_4, GPIO_MODE_INPUT);
if (gpio_get_level(GPIO_NUM_4))
//Do something
//Pullup
gpio_pullup_en(GPIO_NUM_4);
//Pulldown
gpio_pulldown_en(GPIO_NUM_4);
//Disable
gpio_pullup_dis(GPIO_NUM_4);
gpio_pulldown_dis(GPIO_NUM_4);
Output Pins
//Normal output
gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_4, 1); //Set high
gpio_set_level(GPIO_NUM_4, 0); //Set low
//Using a define example:
#define MY_IO_PIN_NAME(state) (state ? gpio_set_level(GPIO_NUM_4, 1) : gpio_set_level(GPIO_NUM_4, 0))
//Open drain output
gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT_OD);
gpio_set_direction(GPIO_NUM_4, GPIO_MODE_INPUT_OUTPUT_OD);
//Set drive capability
gpio_set_drive_capability(GPIO_NUM_4, GPIO_DRIVE_CAP_DEFAULT);
//GPIO_DRIVE_CAP_0 (weak)
//GPIO_DRIVE_CAP_1 (stronger)
//GPIO_DRIVE_CAP_2 (medium)
//GPIO_DRIVE_CAP_DEFAULT (medium)
//GPIO_DRIVE_CAP_3 (strongest)
//GPIO_DRIVE_CAP_MAX
Disable Pins
gpio_set_direction(GPIO_NUM_4, GPIO_MODE_DISABLE);
