The question often arises regarding the use of the ESP32 GPIO, what some pins can be used for and which pins should be avoided in our projects. The ESP32 chip has 48 pins, usually these pins have several different functions. We can assign these functions to the corresponding pins in our code.
Az ESP-WROOM-32 modul
If we are designing a custom circuit based on ESP32, we can choose the ESP-WROOM-32 raw module. Due to its SMT design, it can be soldered relatively easily, thanks to its size of 18 x 25 mm, it takes up little space and does not increase the size of our own circuit. It contains all the pins like development boards with ESP32 chip without the external auxiliary circuits.
Several different ESP32 development boards are available with different pin assignments. Not all GPIO pins are present on all devices, however all GPIOs behave the same no matter which ESP32 development board is used.
ESP32 development board pinout, 30-pin version
The pin assignment of the 30-pin version of the ESP32-WROOM-32 development board can be seen in the picture below.
ESP32 development board pinout, 38-pin version
The following figure shows the pin assignment of the 38-pin version of the ESP32 development board.
ESP32 peripherals
ESP32 peripherals are assigned to specific pins. In the code, we can define whether these pins are used as digital outputs or inputs or to activate one of the peripherals. (e.g.: UART, I2C, SPI, DAC, ADC, PWM, etc.)
The ESP32 architecture allows certain peripherals to be configured for any pin of the ESP32 that is handled by the IO MUX GPIO. This means that the IO MUX and GPIO matrix can be used to redirect internal peripherals to another physical pin.
Some peripherals use dedicated GPIO pins, such as ADC, DAC, Touch and JTAG.
Pins GPIO6 to GPIO11 connect to the flash memory of the ESP32 module. If any peripheral is routed to one of these pins, the ESP32 will not boot. Don’t use them!
Using peripherals mapped to GPIOs by default can make some libraries and example code easier to use.
The ESP32 chip contains the following peripherals:
18 analog-digital converter (ADC) |
2 digital-analog converter (DAC) |
16 PWM output channel |
3 UART interface |
2 I2C interface |
3 SPI interface |
2 I2S interface |
10 capacitive touch sensor GPIO |
1 built-in hall-effect sensor |
SparkFun ESP32 WROOM with ESP32-D0WDQ6 chip
Xtensa dual-core 32-bit LX6 microprocessor Up to 240MHz clock frequency, Integrated 802.11 BGN WiFi, 21 GPIO 8-electrode capacitive touch support
advertising
Use ESP32 GPIO as output or input
Some pins may not work as expected when the ESP32 chip starts up, so avoid using these pins if possible. Below we can see which contacts are suitable as inputs and/or outputs, and which ones we should pay attention to when using them, or which ones are not recommended to use.
The meanings of the icons in the following table:
– can be used
– we have to be careful when using it
– their use is not recommended
GPIO 0
Input: Output:
It is in the HIGH state at system startup, it must be pulled to the LOW state for programming. We only use it when absolutely necessary. Strapping Pin, (Strapping pins set the ESP32 boot configuration.)
GPIO 1
Input: Output:
UART Tx0 pin, can be used for uploading a program and debugging during system startup. When the GPIO15 pin is high, the ESP32 sends boot logs via the UART. It can be conditionally used as an output.
GPIO 2
Input: Output:
This pin connects to the onboard LED. Strapping Pin.
GPIO 3
Input: Output:
UART Rx0 pin, can be used for uploading a program and debugging at system startup. It can be conditionally used as an input.
GPIO 4
Input: Output:
It can be used unconditionally as an output and input.
GPIO 5
Input: Output:
Sends a PWM signal at startup. Strapping Pin.
GPIO 6
Input: Output:
GPIO 6 (SCK/CLK) pin has been removed on some ESP32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO 7
Input: Output:
GPIO 7 (SDO/SD0) pin has been removed on some ESP32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO 8
Input: Output:
GPIO 8 (SDI/SD1) pin has been removed on some ESP32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO 9
Input: Output:
GPIO 9 (SHD/SD2) pin has been removed on some ESP32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO 10
Input: Output:
GPIO 10 (SWP/SD3) pin has been removed on some ESP32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO 11
Input: Output:
GPIO 11 (CSC/CMD) pin has been removed on some ESP32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO 12
Input: Output:
It should be low at startup. Boot fails if it goes high, it sets the voltage of the internal voltage regulator. Strapping Pin. Jtag MTDI pin.
GPIO 13
Input: Output:
Jtag MTCK pin.
GPIO 14
Input: Output:
Sends a PWM signal at system startup. Jtag MTMS pin.
GPIO 15
Input: Output:
It should be high at boot time. Strapping Pin. Jtag MTDO pin.
GPIO 16
Input: Output:
UART Rx2 pin
GPIO 17
Input: Output:
UART Tx2 pin
GPIO 18
Input: Output:
GPIO 19
Input: Output:
GPIO 21
Input: Output:
I2C SDA pin
GPIO 22
Input: Output:
I2C SCL pin
GPIO 23
Input: Output:
GPIO 25
Input: Output:
GPIO 26
Input: Output:
GPIO 27
Input: Output:
GPIO 32
Input: Output:
GPIO 33
Input: Output:
GPIO 34
Input: Output:
These needles have no internal pull-up or pull-down resistance. It can only be used as an input.
GPIO 35
Input: Output:
These needles have no internal pull-up or pull-down resistance. It can only be used as an input.
GPIO 36
Input: Output:
These needles have no internal pull-up or pull-down resistance. It can only be used as an input.
GPIO 39
Input: Output:
These needles have no internal pull-up or pull-down resistance. It can only be used as an input.
ESP32 Development Board 2.4 GHz Dual Core WLAN WiFi + Bluetooth
advertising – amazon.com
UART
UART (Universal Asynchronous Receiver/Transmitter) is a hardware function that handles widely used asynchronous serial communication interfaces such as RS232, RS422 and RS485. UART provides a popular and cost-effective way to exchange full or half-duplex data between different devices. The ESP32 also supports the Infrared Data Association (IrDA) protocol.
The ESP32 chip contains 3 UART controllers that can be independently configured with parameters such as baud rate, data bit length, bit order, number of stop bits, parity bit, etc. All three UART controllers of the ESP32 operate at 3.3V TTL level.
To use ESP32 UART1 and UART2 on Arduino, we need the Hardware Serial library.
The GPIO Matrix allows UART pins to be logically assigned to almost any GPIO pin on the ESP32. GPIO34, GPIO35, GPIO36 and GPIO39 pins cannot be used as TX pins because they can only be used as inputs.
UART0 is connected to the USB-to-serial converter and is used for communication and programming with the ESP32.
The default pins of UART1, GPIO9 and GPIO10, have been removed on some ESP32 development boards. These pins are internally connected to the SPI flash memory. If we want to use it, UART1 must be assigned to another pin.
The use of UART2 is not restricted by anything.
UART | RX IO | TX IO | CTS | RTS |
---|---|---|---|---|
UART0 | GPIO3 | GPIO1 | – | – |
UART1 | GPIO9 | GPIO10 | GPIO6 | GPIO11 |
UART2 | GPIO16 | GPIO17 | GPIO8 | GPIO7 |
I2C
The ESP32 has two I2C channels. I2C also does not have a dedicated GPIO, similar to UART pins, any GPIO can be configured as SDA or SCL. Exceptions are the GPIO34, GPIO35, GPIO36 and GPIO39 pins as they are input only pins and therefore cannot be used as SCL or SDA.
In arduino, GPIO21 (SDA) and GPIO22 (SCL) are the default I2C pins.
GPIO21 | SDA |
GPIO22 | SCL |
The following code snippet shows how to take advantage of both I2C channels of the ESP32.
#include <Wire.h>
const char sdaPin1 = 26;
const char sclPin1 = 25;
const char sdaPin2 = 33;
const char sclPin2 = 32;
TwoWire mywire1 = TwoWire(0);
TwoWire mywire2 = TwoWire(1);
void setup()
{
mywire1.begin(sdaPin1, sclPin1, 100000);
mywire2.begin(sdaPin2, sclPin2, 100000);
}
void loop()
{
mywire1.beginTransmission(0x10);
mywire1.write(0x20);
mywire1.endTransmission();
mywire2.beginTransmission(0x12);
mywire2.write(0x20);
mywire2.endTransmission();
}
SPI
The ESP32 chip has three SPI interfaces. These are SPI, HSPI and VSPI.
SPI is reserved as an interface for Flash memory, although it is ported on some ESP32 development boards, do not use it!
So we have two usable SPI interfaces, HSPI and VSPI. With the help of the GPIO matrix, they can also be redirected to another pin. By default, these are located on the following GPIOs:
SPI | MOSI | MISO | CLK | CS |
VSPI | GPIO 23 | GPIO 19 | GPIO 18 | GPIO 5 |
HSPI | GPIO 13 | GPIO 12 | GPIO 14 | GPIO 15 |
Integrated SPI flash memory
Pins GPIO 6 to GPIO 11 are found on some ESP-WROOM-32 development boards. However, these pins are connected to the integrated SPI flash memory of the ESP-WROOM-32 chip, do not use them.
GPIO6 | SCK/CLK |
GPIO7 | SDO/SD0 |
GPIO8 | SDI/SD1 |
GPIO9 | SHD/SD2 |
GPIO10 | SWP/SD3 |
GPIO11 | CSC/CMD |
myQ Chamberlain Smart Garage Control
advertising
Analog to digital converter (ADC)
The ESP32 has two 12-bit SAR analog-to-digital converters. ADC1 has 8 channels, while ADC2 has 10 channels.
ADC input channels have 12-bit resolution. This means that we get analog values between 0 and 4095, where 0 corresponds to 0V and 4095 to 3.3V.
In the code, we can set the resolution of the ADC channels as well as the ADC range.
ADC2 may work abnormally when using Wi-Fi. If we need Wi-Fi, we use ADC1 instead if possible.
The dedicated GPIOs for the ADC can be found in the table below:
ADC1_0 | GPIO36 |
ADC1_1 | GPIO37 |
ADC1_2 | GPIO38 |
ADC1_3 | GPIO39 |
ADC1_4 | GPIO32 |
ADC1_5 | GPIO33 |
ADC1_6 | GPIO34 |
ADC1_7 | GPIO35 |
. | |
ADC2_0 | GPIO4 |
ADC2_1 | GPIO0 |
ADC2_2 | GPIO2 |
ADC2_3 | GPIO15 |
ADC2_4 | GPIO13 |
ADC2_5 | GPIO12 |
ADC2_6 | GPIO14 |
ADC2_7 | GPIO27 |
ADC2_8 | GPIO25 |
ADC2_9 | GPIO26 |
The ESP32 ADC is a bit noisy and its behavior is non-linear. It probably doesn’t differentiate between 0 and 0.1V and 3.2 and 3.3V. If a more accurate Analog-to-Digital converter is needed, consider using an external ADC.
Digital-to-analog converter (DAC)
On board the ESP32 we find 2 independent 8-bit digital-to-analog converters (DACs) for converting digital values into analog voltages. This DC voltage appears on the GPIO output of the DAC channel.
The 8-bit DAC means that the ESP32 can convert its digital input value from 0 to 255 into an equivalent analog voltage output from 0V to the reference voltage (Vref). The following formula helps you understand what analog voltage is expected at the output compared to the digital input value.
Output voltage(DAC) = Digital input value X (Vref / 255)
The DAC has an internal resistor network and uses a voltage level close to the supply voltage as the input reference voltage (Vref).
The DAC obtains the reference voltage ‘Vref’ from the VDD3P3_RTC pin of the ESP32 chip, which is ideally the same as the supply voltage.
Dedicated GPIOs for the DAC:
DAC1 | GPIO25 |
DAC2 | GPIO26 |
In the figure below you can see where these two pins are located.
RTC GPIOs
The ESP32 has 16 RTC GPIOs that are part of the RTC Low-Power subsystem. These RTC GPIOs are able to wake up the ESP32 from deep sleep from an external source when the Ultra Low Power (ULP) co-processor is running.
This means that we can wake up the ESP32 from deep sleep, for example, by pressing a button connected to one of the RTC GPIOs.
The dedicated pins of the RTC GPIO are shown in the following figure:
RTC_GPIO0 | GPIO36 |
RTC_GPIO3 | GPIO39 |
RTC_GPIO4 | GPIO34 |
RTC_GPIO5 | GPIO35 |
RTC_GPIO6 | GPIO25 |
RTC_GPIO7 | GPIO26 |
RTC_GPIO8 | GPIO33 |
RTC_GPIO9 | GPIO32 |
RTC_GPIO10 | GPIO4 |
RTC_GPIO11 | GPIO0 |
RTC_GPIO12 | GPIO2 |
RTC_GPIO13 | GPIO15 |
RTC_GPIO14 | GPIO13 |
RTC_GPIO15 | GPIO12 |
RTC_GPIO16 | GPIO14 |
RTC_GPIO17 | GPIO27 |
In the following picture, we can see which RTC_GPIO pins are on the ESP32 38 pins development board:
PWM
PWM (Pulse Width Modulation) is a method of generating analog signals on ESP32 digital output pins. We create a square signal alternating between 0V and 3.3V (HIGH / LOW). The duration between HIGH and LOW levels, the pulse width, determines the output analog voltage.
The ESP32 PWM controller has 16 independent channels that can be configured to generate PWM signals with different characteristics. Any output that can act as an output can be used as a PWM pin. GPIO34, GPIO35, GPIO36 and GPIO39 pins cannot generate PWM because they are input only pins.
To set the PWM signal, you need to specify the following parameters in the code:
Frequency. |
Pulse width resolution (1 – 16 bit). |
PWM channel (0-15). |
GPIO that generates the PWM signal. |
Interrupt
Interrupts can be configured for any GPIO pin on the ESP32 microcontroller.
SwitchBot Smart Motion Door Sensor
Wireless Home Security System, PIR Motion Detector Alert, Add SwitchBot Hub to Make it Compatible with Alexa
advertising
Capacitive touch sensitive GPIOs
The ESP32 contains 10 internal capacitive touch sensors. These sensors can detect changes in anything that contains an electrical charge. This allows them to detect touching the GPIOs with a stylus or finger, for example.
With the help of capacitive touch sensors, we can wake up the ESP32 microcontroller from deep sleep.
Internal touch sensors connect to these dedicated GPIO pins.
Touch0 | GPIO4 |
Touch1 | GPIO0 |
Touch2 | GPIO2 |
Touch3 | GPIO15 |
Touch4 | GPIO13 |
Touch5 | GPIO12 |
Touch6 | GPIO14 |
Touch7 | GPIO27 |
Touch8 | GPIO33 |
Touch9 | GPIO32 |
GPIO current consumption
The maximum output current of the ESP32 GPIO pin is 40mA, and they can absorb a maximum current of 28mA individually.
ESP32 Boot – Strapping Pins
The ESP32 chip needs specific configuration parameters every time it boots or reboots, which determine the boot mode, voltage settings, possible printing of ROM messages, JTAG signal source settings, etc.
Some pins have a unique function when starting the ESP32. These are called Strapping Pins. Combinations of high, low, or floating states of the strapping pins determine the boot behavior of the ESP32.
After successful boot, these pins act as normal GPIO pins.
Avoid using these pins whenever possible, as changing their state during the boot process can lead to unexpected behavior, errors, or even system crashes.
The ESP32 chip has the following Strapping pins:
GPIO0 | It is in the HIGH state at system startup, it must be pulled to the LOW state for programming. The serial adapter of the ESP32 Wroom module handles this for us. |
GPIO2 | It must be pulled to LOW for programming. |
GPIO4 | The “GPIO_STRAP” register contains GPIO 4, but is not used by any boot mode. |
GPIO5 | The voltage level of GPIO5 and GPIO15 affects the SDIO Slave timing. |
GPIO12 | Sets the SPI flash memory voltage. If the voltage is low, it is 3.3V. If it is high, it is 1.8V. The ESP32 Wroom module contains 3.3V Flash memory |
GPIO15 | If high, this is the default, the ESP32 will print the debug log on boot to the serial port. If you pull it low, this doesn’t happen. The voltage level of GPIO5 and GPIO15 affects the SDIO Slave timing. |
Meross Dimmable Smart Table Lamp
WiFi Table Lamp, Support HomeKit (iOS13+), Alexa, Google Assistant and SmartThings, Tunable White and Multi-Color, Touch Control, Voice and APP Control, Schedule and Timer
advertising – amazon.com
JTAG
JTAG pins can be used for debugging purposes. If JTAG debugging is not required, these pins can be used as normal GPIOs.
GPIO12 | MTDI pin | |
GPIO13 | MTCK pin | |
GPIO14 | MTMS pin | |
GPIO15 | MTDO pin |
Enable – EN pin
The EN pin of the ESP32-wroom development board is pulled high and connected to the “CHIP_PU” (Chip PowerUp) pin of the ESP32 chip. When pulled low, the ESP32 chip will be disabled. This pin is connected to the EN button of the module. By pressing the button, we can restart the ESP32 microcontroller.
ESP32 built-in Hall effect sensor
The ESP32 also has a built-in Hall effect sensor that detects changes in the magnetic field in its environment.
I2S
I2S (Inter-IC Sound) is a digital audio transmission protocol. The ESP32 has two I2S peripherals, I2S0 and I2S1. Each can be configured as a controller or target, and each can be an audio transmitter or receiver.
There are no dedicated I2S pins on the ESP32, they must be assigned in the code.