The ESP32 chip features 34 physical GPIO pins (GPIO0 ~ GPIO19, GPIO21 ~ GPIO23, GPIO25 ~ GPIO27, and GPIO32 ~ GPIO39). Each pin can be used as a general-purpose I/O, or be connected to an internal peripheral signal. Through IO MUX, RTC IO MUX and the GPIO matrix, peripheral input signals can be from any IO pins, and peripheral output signals can be routed to any IO pins. Together these modules provide highly configurable I/O. For more details, see ESP32 Technical Reference Manual > IO MUX and GPIO Matrix (GPIO, IO_MUX) [ PDF ].
The table below provides more information on pin usage, and please note the comments in the table for GPIOs with restrictions.
GPIO |
Analog Function | RTC GPIO | Comments |
---|---|---|---|
GPIO0 | ADC2_CH1 | RTC_GPIO11 | Strapping pin |
GPIO1 | TXD | ||
GPIO2 | ADC2_CH2 | RTC_GPIO12 | Strapping pin |
GPIO3 |
RXD | ||
GPIO4 | ADC2_CH0 | RTC_GPIO10 | |
GPIO5 | Strapping pin | ||
GPIO6 | SPI0/1 | ||
GPIO7 | SPI0/1 | ||
GPIO8 | SPI0/1 | ||
GPIO9 | SPI0/1 | ||
GPIO10 | SPI0/1 | ||
GPIO11 | SPI0/1 | ||
GPIO12 | ADC2_CH5 | RTC_GPIO15 | Strapping pin; JTAG |
GPIO13 | ADC2_CH4 | RTC_GPIO14 | JTAG |
GPIO14 | ADC2_CH6 | RTC_GPIO16 | JTAG |
GPIO15 | ADC2_CH3 | RTC_GPIO13 | Strapping pin; JTAG |
GPIO16 | SPI0/1 | ||
GPIO17 | SPI0/1 | ||
GPIO18 | |||
GPIO19 | |||
GPIO21 | |||
GPIO22 | |||
GPIO23 | |||
GPIO25 | ADC2_CH8 | RTC_GPIO6 | |
GPIO26 | ADC2_CH9 | RTC_GPIO7 | |
GPIO27 | ADC2_CH7 | RTC_GPIO17 | |
GPIO32 | ADC1_CH4 | RTC_GPIO9 | |
GPIO33 |
ADC1_CH5 | RTC_GPIO8 | |
GPIO34 | ADC1_CH6 | RTC_GPIO4 | GPI |
GPIO35 | ADC1_CH7 | RTC_GPIO5 | GPI |
GPIO36 | ADC1_CH0 | RTC_GPIO0 | GPI |
GPIO37 | ADC1_CH1 | RTC_GPIO1 | GPI |
GPIO38 | ADC1_CH2 | RTC_GPIO2 | GPI |
GPIO39 | ADC1_CH3 | RTC_GPIO3 | GPI |
Strapping pin: GPIO0, GPIO2, GPIO5, GPIO12 (MTDI), and GPIO15 (MTDO) are strapping pins. For more infomation, please refer to ESP32 datasheet .
SPI0/1: GPIO6-11 and GPIO16-17 are usually connected to the SPI flash and PSRAM integrated on the module and therefore should not be used for other purposes.
JTAG: GPIO12-15 are usually used for inline debug.
GPI: GPIO34-39 can only be set as input mode and do not have software-enabled pullup or pulldown functions.
TXD & RXD are usually used for flashing and debugging.
ADC2: ADC2 pins cannot be used when Wi-Fi is used. So, if you are having trouble getting the value from an ADC2 GPIO while using Wi-Fi, you may consider using an ADC1 GPIO instead, which should solve your problem. For more details, please refer to ADC limitations .
There is also separate “RTC GPIO” support, which functions when GPIOs are routed to the “RTC” low-power and analog subsystem. These pin functions can be used when:
In Deep-sleep mode
The Ultra Low Power co-processor is running
Analog functions such as ADC/DAC/etc are in use.
GPIO output and input interrupt example: peripherals/gpio/generic_gpio .
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_config( const gpio_config_t *pGPIOConfig) GPIO common configuration. Configure GPIO's Mode,pull-up,PullDown,IntrType Parameters: pGPIOConfig – Pointer to GPIO configure struct Returns: ESP_OK success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_reset_pin( gpio_num_t gpio_num ) Reset an gpio to default state (select gpio function, enable pullup and disable input and output). Note: This function also configures the IOMUX for this pin to the GPIO function, and disconnects any other peripheral output configured via GPIO Matrix. Parameters: gpio_num – GPIO number. Returns: Always return ESP_OK.
/*F******************************************************************** * **********************************************************************/ esp_err_t( gpio_num_t gpio_num, gpio_int_type_t intr_type) GPIO set interrupt trigger type. Parameters: gpio_num – GPIO number. If you want to set the trigger type of e.g. of GPIO16, gpio_num should be GPIO_NUM_16( 16 ); intr_type – Interrupt type, select from gpio_int_type_t Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_intr_enable( gpio_num_t gpio_num ) Enable GPIO module interrupt signal. Note ESP32: Please do not use the interrupt of GPIO36 and GPIO39 when using ADC or Wi-Fi and Bluetooth with sleep mode enabled. Please refer to the comments of adc1_get_raw. Please refer to Section 3.11 of ESP32 ECO and Workarounds for Bugs for the description of this issue. As a workaround, call adc_power_acquire() in the app. This will result in higher power consumption (by ~1mA), but will remove the glitches on GPIO36 and GPIO39. Parameters: gpio_num – GPIO number. If you want to enable an interrupt on e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16); Returns ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_intr_disable( gpio_num_t gpio_num ) Disable GPIO module interrupt signal. Note This function is allowed to be executed when Cache is disabled within ISR context, by enabling CONFIG_GPIO_CTRL_FUNC_IN_IRAM Parameters: gpio_num – GPIO number. If you want to disable the interrupt of e.g. GPIO16 , gpio_num should be GPIO_NUM_16 (16); Returns: ESP_OK success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_set_level( gpio_num_t gpio_num , uint32_t level) GPIO set output level. Note This function is allowed to be executed when Cache is disabled within ISR context, by enabling CONFIG_GPIO_CTRL_FUNC_IN_IRAM Parameters: gpio_num – GPIO number. If you want to set the output level of e.g. GPIO16 , gpio_num should be GPIO_NUM_16 (16); level – Output level. 0: low ; 1: high Returns: ESP_OK Success ESP_ERR_INVALID_ARG GPIO number error
/*F******************************************************************** * **********************************************************************/ int gpio_get_level( gpio_num_t gpio_num) GPIO get input level. Warning: If the pad is not configured for input (or input and output) the returned value is always 0. Parameters: gpio_num – GPIO number. If you want to get the logic level of e.g. pin GPIO16, gpio_num should be GPIO_NUM_16 (16); Returns: 0 the GPIO input level is 0 1 the GPIO input level is 1
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_set_direction ( gpio_num_t gpio_num , gpio_mode_t mode) GPIO set direction. Configure GPIO direction,such as output_only,input_only,output_and_input Parameters: gpio_num – Configure GPIO pins number, it should be GPIO number. If you want to set direction of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16); mode – GPIO direction Returns: ESP_OK Success ESP_ERR_INVALID_ARG GPIO error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_set_pull_mode( gpio_num_t gpio_num , gpio_pull_mode_t pull) Configure GPIO pull-up/pull-down resistors. Note ESP32: Only pins that support both input & output have integrated pull-up and pull-down resistors. Input-only GPIOs 34-39 do not. Parameters: gpio_num – GPIO number. If you want to set pull up or down mode for e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16); pull – GPIO pull up/down mode. Returns: ESP_OK Success ESP_ERR_INVALID_ARG : Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_wakeup_enable( gpio_num_t gpio_num , gpio_int_type_t intr_type) Enable GPIO wake-up function. Parameters: gpio_num – GPIO number. intr_type – GPIO wake-up type. Only GPIO_INTR_LOW_LEVEL or GPIO_INTR_HIGH_LEVEL can be used. Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_wakeup_disable( gpio_num_t gpio_num) Disable GPIO wake-up function. Parameters: gpio_num – GPIO number Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_isr_register( void (*fn)(void*), void *arg, int intr_alloc_flags , gpio_isr_handle_t *handle ) Register GPIO interrupt handler, the handler is an ISR. The handler will be attached to the same CPU core that this function is running on. This ISR function is called whenever any GPIO interrupt occurs. See the alternative gpio_install_isr_service() and gpio_isr_handler_add() API in order to have the driver support per-GPIO ISRs. To disable or remove the ISR, pass the returned handle to the interrupt allocation functions . Parameters: fn – Interrupt handler function. arg – Parameter for handler function intr_alloc_flags – Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. handle – Pointer to return handle. If non-NULL, a handle for the interrupt will be returned here. Returns: ESP_OK Success ; ESP_ERR_INVALID_ARG GPIO error ESP_ERR_NOT_FOUND No free interrupt found with the specified flags
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_pullup_en( gpio_num_t gpio_num ) Enable pull-up on GPIO. Parameters gpio_num – GPIO number Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_pullup_dis( gpio_num_t gpio_num) Disable pull-up on GPIO. Parameters: gpio_num – GPIO number Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_pulldown_en( gpio_num_t gpio_num) Enable pull-down on GPIO. Parameters: gpio_num – GPIO number Returns ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_pulldown_dis( gpio_num_t gpio_num) Disable pull-down on GPIO. Parameters: gpio_num – GPIO number Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_install_isr_service( int intr_alloc_flags) Install the driver’s GPIO ISR handler service, which allows per-pin GPIO interrupt handlers. This function is incompatible with gpio_isr_register() - if that function is used, a single global ISR is registered for all GPIO interrupts. If this function is used, the ISR service provides a global GPIO ISR and individual pin handlers are registered via the gpio_isr_handler_add() function. Parameters: intr_alloc_flags – Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. Returns: ESP_OK Success ESP_ERR_NO_MEM No memory to install this service ESP_ERR_INVALID_STATE ISR service already installed. ESP_ERR_NOT_FOUND No free interrupt found with the specified flags ESP_ERR_INVALID_ARG GPIO error
/*F******************************************************************** * **********************************************************************/ void gpio_uninstall_isr_service( void ) Uninstall the driver’s GPIO ISR service, freeing related resources. /*F******************************************************************** * **********************************************************************/ esp_err_t gpio_isr_handler_add( gpio_num_t gpio_num, gpio_isr_t isr_handler, void *args) Add ISR handler for the corresponding GPIO pin. Call this function after using gpio_install_isr_service() to install the driver’s GPIO ISR handler service. The pin ISR handlers no longer need to be declared with IRAM_ATTR, unless you pass the ESP_INTR_FLAG_IRAM flag when allocating the ISR in gpio_install_isr_service(). This ISR handler will be called from an ISR. So there is a stack size limit (configurable as “ISR stack size” in menuconfig). This limit is smaller compared to a global GPIO interrupt handler due to the additional level of indirection. Parameters: gpio_num – GPIO number isr_handler – ISR handler function for the corresponding GPIO number. args – parameter for ISR handler. Returns: ESP_OK Success ESP_ERR_INVALID_STATE Wrong state, the ISR service has not been initialized. ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_isr_handler_remove( gpio_num_t gpio_num) Remove ISR handler for the corresponding GPIO pin. Parameters: gpio_num – GPIO number Returns: ESP_OK Success ESP_ERR_INVALID_STATE Wrong state, the ISR service has not been initialized. ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_set_drive_capability( gpio_num_t gpio_num, gpio_drive_cap_t strength ) Set GPIO pad drive capability. Parameters: gpio_num – GPIO number, only support output GPIOs strength – Drive capability of the pad Returns ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_get_drive_capability( gpio_num_t gpio_num, gpio_drive_cap_t *strength ) Get GPIO pad drive capability. Parameters: gpio_num – GPIO number, only support output GPIOs strength – Pointer to accept drive capability of the pad Returns: ESP_OK Success ESP_ERR_INVALID_ARG Parameter error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_hold_en( gpio_num_t gpio_num) Enable gpio pad hold function. The gpio pad hold function works in both input and output modes, but must be output-capable gpios. If pad hold enabled: in output mode: the output level of the pad will be force locked and can not be changed. in input mode: the input value read will not change, regardless the changes of input signal. The state of digital gpio cannot be held during Deep-sleep, and it will resume the hold function when the chip wakes up from Deep-sleep. If the digital gpio also needs to be held during Deep-sleep, gpio_deep_sleep_hold_en should also be called. Power down or call gpio_hold_dis will disable this function. Parameters: gpio_num – GPIO number, only support output-capable GPIOs Returns: ESP_OK Success ESP_ERR_NOT_SUPPORTED Not support pad hold function
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_hold_dis( gpio_num_t gpio_num ) Disable gpio pad hold function. When the chip is woken up from Deep-sleep, the gpio will be set to the default mode, so, the gpio will output the default level if this function is called. If you don’t want the level changes, the gpio should be configured to a known state before this function is called. e.g. If you hold gpio18 high during Deep-sleep, after the chip is woken up and gpio_hold_dis is called, gpio18 will output low level(because gpio18 is input mode by default). If you don’t want this behavior, you should configure gpio18 as output mode and set it to hight level before calling gpio_hold_dis. Parameters: gpio_num – GPIO number, only support output-capable GPIOs Returns: ESP_OK Success ESP_ERR_NOT_SUPPORTED Not support pad hold function
/*F******************************************************************** * **********************************************************************/ void gpio_deep_sleep_hold_en( void ) Enable all digital gpio pad hold function during Deep-sleep. When the chip is in Deep-sleep mode, all digital gpio will hold the state before sleep, and when the chip is woken up, the status of digital gpio will not be held. Note that the pad hold feature only works when the chip is in Deep-sleep mode, when not in sleep mode, the digital gpio state can be changed even you have called this function. Power down or call gpio_hold_dis will disable this function, otherwise, the digital gpio hold feature works as long as the chip enter Deep-sleep.
/*F******************************************************************** * **********************************************************************/ void gpio_deep_sleep_hold_dis( void ) Disable all digital gpio pad hold function during Deep-sleep.
/*F******************************************************************** * **********************************************************************/ void gpio_iomux_in( uint32_t gpio_num , uint32_t signal_idx) Set pad input to a peripheral signal through the IOMUX. Parameters: gpio_num – GPIO number of the pad. signal_idx – Peripheral signal id to input. One of the *_IN_IDX signals in soc/gpio_sig_map.h.
/*F******************************************************************** * **********************************************************************/ void gpio_iomux_out( uint8_t gpio_num, int func, bool oen_inv) Set peripheral output to an GPIO pad through the IOMUX. Parameters: gpio_num – gpio_num GPIO number of the pad. func – The function number of the peripheral pin to output pin. One of the FUNC_X_* of specified pin (X) in soc/io_mux_reg.h. oen_inv – True if the output enable needs to be inverted, otherwise False.
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_sleep_sel_en( gpio_num_t gpio_num ) Enable SLP_SEL to change GPIO status automantically in lightsleep. Parameters: gpio_num – GPIO number of the pad. Returns: ESP_OK Success
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_sleep_sel_dis( gpio_num_t gpio_num) Disable SLP_SEL to change GPIO status automantically in lightsleep. Parameters: gpio_num – GPIO number of the pad. Returns: ESP_OK Success
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_sleep_set_direction( gpio_num_t gpio_num, gpio_mode_t mode ) GPIO set direction at sleep. Configure GPIO direction,such as output_only,input_only,output_and_input Parameters: gpio_num – Configure GPIO pins number, it should be GPIO number. If you want to set direction of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16); mode – GPIO direction Returns ESP_OK Success ESP_ERR_INVALID_ARG GPIO error
/*F******************************************************************** * **********************************************************************/ esp_err_t gpio_sleep_set_pull_mode( gpio_num_t gpio_num, gpio_pull_mode_t pull ) Configure GPIO pull-up/pull-down resistors at sleep. Note ESP32: Only pins that support both input & output have integrated pull-up and pull-down resistors. Input-only GPIOs 34-39 do not. Parameters: gpio_num – GPIO number. If you want to set pull up or down mode for e.g. GPIO16 , gpio_num should be GPIO_NUM_16 (16); pull – GPIO pull up/down mode. Returns: ESP_OK Success ESP_ERR_INVALID_ARG : Parameter error
Check whether it is a valid GPIO number.
Check whether it can be a valid GPIO number of output mode.
Configuration parameters of GPIO pad for gpio_config function.
Public Members
GPIO pin: set with bit mask, each bit maps to a GPIO
GPIO mode: set input/output mode
GPIO pull-up
GPIO pull-down
GPIO interrupt type
Values:
Values:
Use to signal not connected to S/W
GPIO0, input and output
GPIO1, input and output
GPIO2, input and output
GPIO3, input and output
GPIO4, input and output
GPIO5, input and output
GPIO6, input and output
GPIO7, input and output
GPIO8, input and output