#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);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.