Use the GPIO pins of the ESP32
Discover all the relevant information on the ESP32 pinout on a single page to correctly use these GPIO pins for your DIY electronics project. Although there are many variations of ESP32 boards, the function of the pins remains the same. However, some pins may not be accessible, depending on your model.
There are plenty of ESP32 boards out there with different pin arrangements. Here is the detailed pin diagram of 2 various ESP32 boards. It will be beneficial for you to know the specific functions of each pin:
-
ESP32 WROOM Generic DevKit
-
uPesy ESP32 WROOM DevKit
Note
There is a significant difference between the 2 boards, which is not necessarily obvious: their widths . uPesy ESP32 boards can be put on a breadboard, and you can access pins on both sides, unlike most of the other ESP32 boards that are too “big” to be used easily on a breadboard …
Specific function of some ESP32 pins
The goal is to inform you of the limitations of some ESP32 GPIO pins to avoid having unpleasant surprises during your projects or having an incomprehensible bug in your program.
ESP32-based development boards typically have 33 pins except those for power. Some GPIO pins have a bit of a particular function:
-
If your ESP32 board has the pins GPIO6, GPIO7, GPIO8, GPIO9, GPIO10, GPIO11, you must not use them because they are connected to the ESP32 flash memory: if you use them on the ESP32 will not work.
Pins connected to the Flash memory of the ESP32 GPIO
Possible name
6
SCK / CLK
7
SDO / SD0
8
SDI / SD1
9
SHD / SD2
10
SWP / SD3
11
CSC / CMD
Note
For this reason that these pins are not available on the uPesy ESP32 boards.
-
The GPIO1 (TX0) and GPIO3 (RX0) pins communicate via USB in UART with the computer. If you use them, you will no longer upload programs to the board or use the serial monitor through the USB port. They can be helpful for programming the board without using USB with an external programmer. Fortunately, there are other UART interfaces available.
-
Pins GPIO36 (VP), GPIO39 (VN), GPIO34, GPIO35 can only be used as input. They also do not have built-in internal pullup and pulldown resistors (You cannot use
pinMode (36, INPUT_PULLUP)
orpinMode(36, INPUT_PULLDOWN)
. -
Some pins have a particular function when starting the ESP32. These are called Strapping Pins .
They are used to put the ESP32 in BOOT mode (to run the program written to flash memory) or FLASH mode (to upload the program to flash memory). Depending on the voltage on these pins, the ESP32 will boot either in BOOT mode or in FLASH mode.The strapping pins are the pins GPIO0, GPIO2, GPIO12 (MTDI) and GPIO15 (MTDO) .You can use them, but you just have to be careful when setting a logic state (3.3V or 0V) with an external pullup or pulldown resistor.Tip
If your ESP32 board works correctly, and then nothing works (can’t upload the code or launch it) when you add a sensor, it will likely be linked to these famous strapping pins.
-
When booting the ESP32, some pins quickly switch logic states (0V → 3.3V) during a short time. If a relay is attached to these pins, you may have some weird bugs that temporarily switch the relay. The faulty pins are:
-
GPIO 1 : Send the ESP32 boot logs via the UART
-
GPIO 3 : Voltage of 3.3V during boot
-
GPIO 5 : Sends a PWM signal during boot
-
GPIO 14 : Sends a PWM signal during boot
-
GPIO 15 : Send the ESP32 boot logs via the UART
Note
You can, of course, use these pins. If you have a weird behavior when starting the ESP32 with one of these pins, you will probably have to choose another one.
-
-
The EN pin is used to activate or deactivate the ESP32 via an external wire. It is connected to the EN button on the board. When the ESP32 is on, it’s at 3.3V. If we put this pin to the ground, the ESP32 is switched off. You can use it when the ESP32 is in a box, and you want to turn it on/off with a switch.
-
On boards that use an ESP32-WROVER module to have more RAM (the uPesy ESP32 Wrover DevKit, for example), the pins GPIO16 and GPIO17 are not available because they are used internally by the PSRAM.
-
Others GPIO pins have no particular restrictions.
Summary of all ESP32 GPIO pins
ESP32 Peripherals
To interact with modules, sensors, or electronic circuits, the ESP32, like any microcontroller, has a multitude of peripherals. There are also many more than on a classic Arduino Uno board.
- The ESP32 has the following peripherals:
Some peripherals are already used by the ESP32 during its essential operation. In reality, there are therefore fewer possible interfaces for each peripheral.
Tip
The ESP32 pins are much more modular than the Arduino: You can “attach” a UART, I2C, SPI, PWM peripheral to whatever pins you want. The SPI, I2C, UART, PWM, DAC are no longer associated with specific pins. For example, on the Arduino Uno, you could only have SPI on pins 10, 11, 12, 13. With the ESP32, you can choose which ones you want. Only the ADC and the capacitive sensors are assigned to fixed pins. ESP32 pinouts available on the Internet show the connection made by default (if we do not specify the pins used). It remains a good practice to use the pins by default to keep good compatibility with external libraries as long as this does not limit the connection of your wires.
UART on ESP32
UART is the serial protocol that allows data to be easily exchanged between 2 devices . The ESP32 has 3 UART buses: UART0, UART1, and UART2. They can be used to communicate with a sensor, an Arduino, a Raspberry Pi, a computer …
-
UART0 is by default on the pins GPIO1 (TX0) and GPIO3 (RX0) of the ESP32. It is used to communicate with the computer through the serial monitor. This is also the one that is used to flash the ESP32 board. Usually, it displays messages to the console when using
Serial.println()
. -
To use UART2, just add
Serial2.begin()
in thesetup()
function and use theSerial2.println()
to send messages. By default, the UART2 bus is on pins GPIO16 (RX2) and GPIO17 (TX2) , but they can be changed during setup (useful with an ESP32 Wrover module). This simple code allows you to use the UART2 bus:void setup() { Serial2.begin(115200); } void loop() { Serial2.println("Hello from UART2"); delay(100); }
Note
Messages will not be displayed on the serial monitor since the UART2 is not routed to the USB of the computer.
-
UART1 is by default on the pins used by the ESP32 flash. However, you can use it thanks to the “ESP32 GPIO matrix” by choosing the pins you want. So this code makes it possible to have a serial link on pins GPIO14 and GPIO12 using the UART1 bus.
void setup() { /* * UART1 -> Serial1 * RX Pin -> GPIO 14 * TX Pin -> GPIO 12 * UART Configuration -> SERIAL_8N1 */ Serial1.begin(115200,SERIAL_8N1,14,12); } void loop() { Serial1.println("Hello from UART1"); delay(100); }
The I2C on the ESP32
The ESP32 has 2 I2C buses :
-
The I2C0 bus is the one used by default by Arduino libraries. It is connected to the pins GPIO22 (SCL) and GPIO21 (SDA) of the ESP32. It can be used on any pin of the ESP32 when you use the
Wire.h
library by specifying the pins with the functionWire.begin (SDA_PIN, SCL_PIN)
. -
The I2C1 bus can also be used on any pin (pay attention to the limited pins described previously). Here is an example that uses the second I2C bus:
#include <Wire.h> TwoWire I2C1 = TwoWire(1); void setup() { I2C1.begin(14,12,400000); // SDA pin 14, SCL pin 12, 400kHz frequency } void loop() { I2C1.beginTransmission(0x42); I2C1.write(140); I2C1.endTransmission(); delay(100); }
SPI on ESP32
Even though the ESP32 has 3 SPI buses, only 2 can be used because one is used by the internal flash memory. The 2 available SPI buses are called VSPI and HSPI.
Note
Arduino libraries that use the SPI use the VSPI bus by default.
By default, the pin mapping is as follows:
SPI |
MOSI |
MISO |
SCK / CLK |
CS / SS |
---|---|---|---|---|
VSPI |
GPIO 23 |
GPIO 19 |
GPIO 18 |
GPIO 5 |
HSPI |
GPIO 13 |
GPIO 12 |
GPIO 14 |
GPIO 15 |
As for the I2C, it is possible to manually choose the pins that we want to use for the SPI by specifying the pins on the initialization:
SPI.begin
(CLK_PIN,
MISO_PIN,
MOSI_PIN,
SS_PIN);
PWM on the ESP32
The ESP32 has 16 channels that can be used to generate PWM signals: you can have up to 16 different PWM outputs. All pins that can be configured as a digital output can be used to output a PWM signal. The use of PWM is different from that on Arduino: we can configure more parameters. I suggest you check out using the PWM of ESP32 with Arduino code to use it in your projects.
Capacitive sensors on the ESP32
The ESP32 has 10 capacitive sensors (only 9 if the GPIO0 pin is not available). They can be used as tactile buttons. These are the recognized TOUCH pins seen on the ESP32 pinout. They can also be used when the ESP32 is in DeepSleep mode (power saving mode) to wake it up. I invite you to read the use capacitive sensors on the ESP32 with Arduino code for more details.
The internal capacitive sensors are connected to the following GPIO pins:
TOUCH0 |
TOUCH1 |
TOUCH2 |
TOUCH3 |
TOUCH4 |
GPIO 4 |
GPIO 0 |
GPIO 2 |
GPIO 15 |
GPIO 13 |
TOUCH5 |
TOUCH6 |
TOUCH7 |
TOUCH8 |
TOUCH9 |
GPIO 12 |
GPIO 14 |
GPIO 27 |
GPIO 33 |
GPIO 32 |
ADC on ESP32
The ESP32 has 2 separate ADC: the ADC1 with 8 channels and the ADC2 with 10 channels, for a total of 18 analog inputs. For example, there is on pin GPIO34, channel number 6 of ADC1 (ADC1_CH6) . However, there is much less available in practice because of the Wi-Fi.
Warning
The ADC2 cannot be used when Wi-Fi is enabled because the ESP32’s Wi-Fi uses it to operate.
ADC1_CH0 |
ADC1_CH1 |
ADC1_CH2 |
ADC1_CH3 |
ADC1_CH4 |
ADC1_CH5 |
ADC1_CH6 |
ADC1_CH7 |
---|---|---|---|---|---|---|---|
GPIO 36 |
GPIO 37 |
GPIO 38 |
GPIO 39 |
GPIO 32 |
GPIO 33 |
GPIO 34 |
GPIO 35 |
ADC2_CH0 |
ADC2_CH1 |
ADC2_CH2 |
ADC2_CH3 |
ADC2_CH4 |
---|---|---|---|---|
GPIO 4 |
GPIO 0 |
GPIO 2 |
GPIO 15 |
GPIO 13 |
ADC2_CH5 |
ADC2_CH6 |
ADC2_CH7 |
ADC2_CH8 |
ADC2_CH9 |
GPIO 12 |
GPIO 14 |
GPIO 27 |
GPIO 25 |
GPIO 26 |
In general, the ESP32 ADC is not very reliable despite its 12-bit resolution. I suggest you consult for more information on this, the article use and limits of ADC of ESP32 .
DAC on the ESP32
The ESP32 has two 8-bit DAC outputs to convert a digital signal into an analog signal (generating a sine, for example). The pins used are as follows:
DAC1 |
DAC2 |
---|---|
GPIO 25 |
GPIO 26 |
Note
The resolution of only 8 bits of the DAC is insufficient for audio applications without quality deterioration. It is better to use an external DAC with a better resolution (12-24 bits) and use the I2S bus of the ESP32.