ESP32, GPIO pins and associated functions. I/O, PWM, RTC, I2C, SPI, ADC, DAC

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp
Table of Contents

The ESP32 is a micro-controller which has WiFi and Bluetooth connectivity (BLE, BT4.0 and Bluetooth Smart) as well as a GPIO. The pins are multi-function, ie it is possible to define its use by programming. I/O, PWM, RTC, I2C, SPI, ADC, DAC. You may have noticed that development boards have a more limited number of pins. Some pins are reserved (access to flash memory, programming, etc.). Each development board manufacturer chooses which pins they display.

 

The ESP32 is available in 46 versions currently. The trend is expected to increase further in the future as announced by Espressif on this blog post announcing the launch of the ESP32-C3. We clearly end up getting lost! Fortunately Espressif has put online a search engine for designers of connected objects and Makers who design their own PCBs.

This article summarizes the pins that can be used – regardless of the manufacturer of your ESP32 development board – to access the various functions offered by the ESP32. I2C port, I2S, UART (serial port), Touch (capacitive key) …

The pins available on the development boards are different for each manufacturer and depend on the equipment on board the board (LoRa radio module, GSM module, GPS, screen OLED SSD1306, micro-SD card reader …)

This article takes into account the technical specifications of the latest generation of ESP32 DevkitC v4 card. Please feel free to point out any errors or clarifications in the comments.

Quick access to themes:

How to interpret the pin number on your development board?

You have probably noticed that the number of pins of the GPIO expansion connector varies from manufacturer to manufacturer. First, some pins are reserved for access to flash memory through the SPI bus. Then, each manufacturer chooses the pins that it leaves available (exposes) to the user (developer) according to its own design and the equipment integrated into the development board (OLED screen, sensor, camera, micro card reader SD …).

The majority of consumer ESP32 development board manufacturers mainly use two versions:

  • The ESP32-D0WDQ6-V3 is a 6x6mm QFN-48 package. 
  • The ESP32-D0WD is equivalent to the ESP32-D0WDQ6, but has a smaller chip size of 5x5mm, which reduces the footprint for the design.

Whatever the device, you will need to add additional external flash memory on which the program will be stored (developed with the Arduino IDE) and possibly other data (HTML interface, audio files, images, configuration files, etc.). As we will see later in the article, some pins of the ESP32 are reserved for communicating with Flash memory.

 ESP32-D0WDQ6-V3ESP32-D0WD
Housing (mm)QFN 6×6QFN 5×5
esp32 pinout layout qfn 6x6 ESP32-D0WDQ6-V3esp32 pinout layout qfn 5x5
Brooches48 pins + GND (49)
Memory

ROM 448 No

SAM 520 Ko

RTC SRAM 16 Ko

Working voltage2.3 V to 3.6 V
GPIOx34
ADC converterUp to 18 channels. 9 to 12 bit resolution
DAC converter2 8-bit channels
I2Cx2
I2Sx2
SPIx4
UARTx3
Security1024 bit OTP

AES, SHA, RSA, ECC, RNG

Secure Boot, Flash Encryption

Operation-40°C to 125°C

Source: Espressif technical documentation

As you can see, there is no need to worry too much. Espressif has ensured the compatibility of the pins whatever the case and whatever the generation. The difference is mainly found in the amount of flash memory or PSRAM (memory extension of the processor of the ESP32) associated.

ESP32-PICO-D4

The ESP42-PICO-D4 is a version that combines on a single chip an ESP32 SoC, a crystal oscillator, filter capacitors, as well as 4MB of flash memory in a single 7x7mm QFN package. With its very small footprint, it is particularly well suited to mobile applications (connected watches, bracelet and activity tracker, etc.).

ESP32-WROVER

This generation launched in 2028 is still available in the Espressif catalog. You might find it on many entry-level development boards.

Pin identification

Since version 4 of the official Espressif development kit, the pins are numbered in the form GPIO27 or simply by the corresponding number on the board, here 27.

The GPIO pin does not match the pin on the ESP32 processor which is located under a metal cover. For example pin 27 of the GPIO is connected to pin 16 of the SoC 🙄

It does not matter at the programming level. We never refer to the pin on the processor but this can be confusing … especially with the generations and versions that follow one another.

esp32 qfn pinout to gpio

If you have an older generation board (or the manufacturer has not yet sold all of these stocks), the GPIO pins are preceded by IO, for example IO27.

It’s even simpler than with the SDK for ESP8266 because you just need to indicate the pin number in the Arduino code. For example :

pinMode(27, OUTPUT);

Before detailing more precisely the functions of each spindle, here is the marking of the last generation (DevKitC v4)

 

esp32 devkitc v4 spotting pin gpio pinout 2021

ESP32 digital I/O pins

The majority of the ESP32 pins can be used as a digital input or output. Some pins are reserved or are used by the processor. For example pins 6 to 11 are reserved for access to flash memory via the SPI bus, the pins of the serial port for uploading the program or debugging

The pins framed in green can be used for digital input or output as well.

The pins framed in orange can be used but beware of unexpected behavior at startup (see explanations in the table).

esp32 pinout ESP32-D0WDQ6-V3 io input output entrees sorties

It is not recommended – not to say prohibited – to use the pins framed in red. Usually these pins are reserved (and used) by the ESP32 processor. So you may run into serious problems using these pins in your projects.

GPIO pinDigital inputDigital outputNote
0PULL UPSends a PWM signal at startup.
1TXDebug output at startup
2Connected to the on-board LED
3RXTakes the HIGH state at startup
4
5Sends a PWM signal at startup
12Fail to start if in PULLUP mode
13
14Sends a PWM signal at startup
15Sends a PWM signal at startup
16
17
18
19
21
22
23
25
26
27
32
33
34x
35x
36x
39x

Pins 37 and 38 are not available on most ESP32 development boards. Pins 34, 35, 36, 37, 38, and 39 can only be used as digital input.

Data collected from official documentation.

Follow this more detailed tutorial to learn how to use the digital inputs and outputs of the ESP32.

Read Also
ESP32. How to use GPIO digital IO with Arduino code

PWM outputs

The ESP32 has 16 independent channels which can be configured to generate PWM signals with different properties. All pins framed in green in the previous table can be used as a PWM pin.

Spindle to avoid (prohibited) in your projects

Some GPIO pins automatically go HIGH or output PWM signals when starting up or during reset. You may experience unexpected results in your programs when the ESP32 resets or starts up.

Spindle (GPIO)Particularity, precaution to be taken
0Sends a PWM signal at startup.

Pin  0  is used as the boot pin and must be low to enter UART download mode. Make sure it is not pulled down by a device while booting or the firmware will not boot!

1Debug output at startup
2Pin  2  is used as the boot pin and must be low to enter UART download mode. Make sure it is not pulled up by a device during boot or you will not be able to flash any firmware on the module!
3Takes the HIGH state at startup
5Sends a PWM signal at startup
6Used for SPI flash memory
7
8
9
10
11
12Startup failure if in PULL-UP mode

Pin 12 is used as the boot pin to select the output voltage of an internal regulator that powers the flash chip (VDD_SDIO). This pin has an internal pull-down resistor. If it is not connected, it will display a low level when resetting (selection of default 3.3V operation). Make sure it is not pulled up by a device during boot or the ESP32 might not boot.

14Sends a PWM signal at startup
15Sends a PWM signal at startup

Pin 15 can be used to stop the debug output during the startup phase. If pulled down with a resistance (pull-down), there will be no output on the serial port during the boot process. This can be useful in battery powered applications where you do not want to use the serial port at all to reduce power consumption.

Note on ESP32-PICO-D4

Pins 16 and 17 are reserved for writing to internal flash memory.

Note on ESP32-WROVER

Pins 16 and 17 are reserved for accessing PSRAM memory

GPIO consumption in OUTPUT mode (digital output)

The ESP32’s GPIO is designed to send logic signals. The GPIO will be able to deliver enough power to power a few LEDs, on the other hand, the GPIO of the ESP32 cannot be used to power equipment that requires more energy such as a motor.

Each output can deliver a current of 40mA maximum.

In addition, the total power used by all the equipment connected to the GPIO must not exceed 1200mA according to the Absolute Maximum Ratings section of the official technical documentation.

Pins compatible with interrupts

An interrupt is a process that is triggered asynchronously by an external event. Interrupts are used to detect an event in real time while letting the microcontroller processor do other tasks.

Interrupts only work with digital inputs. The digital inputs that can trigger an interrupt are circled in the diagram below.

Do not use pins colored orange or red. Your program might behave unexpectedly while using these.

Warning. Some pins go HIGH or emit PWM signals on startup or during reset (already mentioned earlier). Others are used by the system to access flash memory or upload the program. They should not be used in your projects.

esp32 gpio pinout interrupt interruptions

Follow this tutorial which explains in detail how to use the ESP32 interrupts.

Read Also
ESP32. How to use external interrupts with Arduino code

Real-time (RTC) and capacitive (Touch) subsystem pins

The ESP32 processor has a very low power real time subsystem called RTC which remains active even when it is in standby.

As a reminder, the ESP32 has several standby modes. Depending on the mode activated, the different subsystems are deactivated to reduce consumption and / or save battery.

Active Mode
GPIOWirelessBluetoothCPUULPRTCConsumption*
gpiowifibluetoothcpucpurtc95 ~ 240mA
Modem Sleep
gpiowifibluetoothcpucpurtc20 ~ 68 mA
Light Sleep
gpiowifibluetoothcpucpurtc0.8 mA
 Core paused
Deep Sleep
gpiowifibluetoothcpucpurtc10 ~ 150 μA
RTC only
Hibernation
gpiowifibluetoothcpucpurtc1 μA
RTC onlyx1

To learn more about putting the ESP32 to sleep, read this detailed article

Read Also
ESP32. Arduino code for Deep Sleep and wake-ups (Timer, Touch Pad, GPIO ...)

The ESP32 therefore has

  • 18 pins marked RTC numbered and 0 to 17.
  • 10 pins for touch screens numbered from 0 to 9.

Pin identification has changed between versions of ESP32 SoCs. The following table summarizes the RTC and Touch pins of the Espressif ESP32-DevKitC v4 (2020) and ESP32-DevKitC v2 (obsolete) development boards.

RTC pin labelCapacitive pin (Touch Pad)Pin tag on an ESP32 DevKitC version 4 (2020) boardPin tag on an ESP32-DevKitC V2 board (obsolete)
Map markerCPU pin (49 pins)Map markerCPU pin (39 pins)
RTC_GPIO0VP5VP4
RTC_GPIO1Not exposed37Not exposed
RTC_GPIO2Not exposed7Not exposed
RTC_GPIO3VN8VN5
RTC_GPIO43410IO346
RTC_GPIO53511IO357
RTC_GPIO62514IO2510
RTC_GPIO72615IO2611
RTC_GPIO8Touch83313IO339
RTC_GPIO9Touch93212IO328
RTC_GPIO10Touch044IO426
RTC_GPIO11Touch100IO025
RTC_GPIO12Touch222IO224
RTC_GPIO13Touch31515IO1523
RTC_GPIO14Touch41313IO1316
RTC_GPIO15Touch51212IO1214
RTC_GPIO16Touch61417IO1416
RTC_GPIO17Touch72716IO2712

Here is the pinout of the ESP32 DevKitC version 4 (2020) kit.

The circled number corresponds to the processor pin.

The number indicated on the development board corresponds to the GPIO, it is this number that must be used in the Arduino code of the project.

esp32 pinout rtc touch pin

Source: identification of the RTC and capacitive (Touch) pins (pinout) of the official ESP32 DevkitC v4 kit  according to the official Espressif documentation.

Read this article to learn more about PSTN or touch pins.

Read Also
ESP32. Arduino code for Deep Sleep and wake-ups (Timer, Touch Pad, GPIO ...)

Pin EN (Enable)

The EN (ENable) pin enables the 3.3V regulator to be activated. By default, the regulator is enabled. To deactivate the regulator, it is necessary to ground (GND) voltage It is pulled up, then connect to the earth to deactivate the 3.3V regulator. This means that you can use this pin connected to a push button to restart your ESP32, for example.

I2C bus

The ESP32 has 2 independent I2C buses. Each bus can play the role of main equipment (bus pilot) or secondary equipment.

GPIOI2C bus n ° 1I2C bus n ° 2
SDA21User defined
SCL22User defined

However, it is possible to use any digital pin if the default pins (21 and 22) are not exposed on the development board. To do this, simply indicate the SDA and SCL pins to use when initializing the bus like this

Wire.begin(BROCHE_SDA, BROCHE_SCL);

The I²C interfaces support the following modes:

  • Standard mode (100 Kbit / s)
  • Fast mode (400 Kbit / s)
  • Up to 5 MHz depending on the circuit power
  • 7-bit / 10-bit addressing mode
  • Dual addressing mode

SPI bus

The ESP32 has 3 SPI buses. SPI, HSPI (H for Hardware) and VSPI (V for Virtual).

By default, the HSPI and VSPI buses are connected to the following pins

HSPIVSPI
CLKGPIO14GPIO18
MISOGPIO12GPIO19
DAWDLEGPIO13GPIO23
SSGPIO15GPIO5

As for the I2C bus, it is possible to manually choose any pin that you want to use to communicate with SPI equipment (OLED screen, LCD screen, sensor, micro SD card reader, etc.) by switching to sets the pins during bus initialization.

SPI.begin(CLK, MISO, MOSI, SS);

12-bit (18-channel) Analog-to-Digital Converter (ADC)

The ESP32 is equipped with a 12-bit Analog / Digital converter (or ADC in English which stands for  A nalog to D igital C onverter) having 18 independent channels on the pins listed in the table below.

The ESP32’s ULP processor is also designed to access the ADC converter even when the processor is idle. This greatly reduces energy consumption. The CPU can be woken up by a threshold setting and / or other triggers.

GPIOADC channel
0ADC2_CH1
2ADC2_CH2
4ADC2_CH0
12ADC2_CH5
13ADC2_CH4
14ADC2_CH6
15ADC2_CH3
25ADC2_CH8
26ADC2_CH9
27ADC2_CH7
32ADC1_CH4
33ADC1_CH5
34ADC1_CH6
35ADC1_CH7
36ADC1_CH0
37ADC1_CH1
38ADC1_CH2
39ADC1_CH3

Knowing the channel is not very important, on the other hand it must always be kept in mind that the voltage on the spindle must never exceed 3V3. Beyond this tension, you have a great chance of break the ESP32 development board. There is a discussion on GitHub about this.

The sampling being carried out on 12-bits, the returned value will be between 0 (0V) and 4095 (3V3).

It is possible to adjust the resolution from 9 to 12-bits (see the next paragraph) which will give

  • 9-bits: value between 0 and 511
  • 10-bits: value between 0 and 1023
  • 11-bits: value between 0 and 2047
  • 12-bits: value between 0 and 4095

The analogRead() method is available on the ESP32-Arduino library, all you have to do is pass as a parameter the pin on which you want to retrieve the analog value.

For example to read the value of a potentiometer connected to pin 35

val = analogRead(35); 
Serial.print("Angle actuel :"); 
Serial.println(val);

circuit for obtaining analog values ​​from a potentiometer with an ESP32

Advanced functions of ADC converter

There are other more advanced functions that allow fine tuning of data acquisition parameters.

FunctionDescription
analogReadResolution(resolution)Set the sample bits and the resolution. This can be a value between 9 (0 – 511) and 12 bits (0 – 4095). The default resolution is 12 bits.

9-bits (0-511), 10-bits (0-1023), 11-bits (0-2047) and 12-bits (0-4095)

analogSetWidth(width)Set the sample bits and the resolution. This can be a value between 9 (0 – 511) and 12 bits (0 – 4095). The default resolution is 12 bits.

9-bits (0-511), 10-bits (0-1023), 11-bits (0-2047) and 12-bits (0-4095)

analogSetCycles(cycles)Define the number of cycles per sample. The default is 8. Range 1 to 255
analogSetSamples(samples)Set the number of samples in the range. The default is 1 sample. It has the effect of increasing sensitivity.
analogSetClockDiv(attenuation)set the ADC clock separator. The default is 1. Range from 1 to 255
analogSetAttenuation(attenuation)Set the input attenuation for all ADC pins. The default is ADC_11db. Accepted values:

  • ADC_0db: does not define any attenuation (1 V input = CAN reading of 1088).
  • ADC_2_5db: defines an attenuation of 1.34 (1 V input = CAN reading of 2086).
  • ADC_6db: defines an attenuation of 1.5 (input 1 V = CAN reading of 2975).
  • ADC_11db: defines an attenuation of 3.6 (1 V input = CAN reading of 3959).
analogSetPinAttenuation(pin, attenuation)Set the input attenuation for the specified pin. The default is ADC_11db. The attenuation values ​​are the same as for the previous function.
adcAttachPin(pin)Attach a pin to the ADC (also clears any other analog mode that might be activated). Returns a TRUE or FALSE result.
adcStart(pin), adcBusy(pin)andresultadcEnd(pin)Start an ADC on the attached pin bus. Check if the conversion on the pin’s ADC bus is in progress (returns TRUE or FALSE). Get the result of the conversion: returns a 16-bit integer.

Digital to Analog Converter (DAC)

The ESP32 is equipped with a Digital-to-Analogue converter (or D igital to A nalogic C onverter) offering two 8-bit channels. They are accessible on pins 25 and 26.

GPIOADC channel
25DAC1
26DAC2

Infrared interface

The infrared interface supports eight channels of infrared remote transmission and reception. By programming the pulse waveform, it supports various infrared protocols. Eight channels share a 512 x 32 bit memory block to store the transmit or receive waveform.

Updates

09/02/2021  Publication of the article

French version

Click to rate this post!
[Total: 1 Average: 5]

Thanks for your reading

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
We will be happy to hear your thoughts

      Leave a Reply

      Ads
      Read more
      Recent posts on the Forum
      DIY Projects
      DIY Projects