ESP32 - Introduction

In this tutorial, we will learn about ESP32, a dual core MCU from Espressif Systems with integrated Wi-Fi and Bluetooth. This Getting Started with ESP32 guide is for complete beginners, with or without prior experience in IoT.

Video Tutorial

you can watch this video tutorial

What is ESP32

ESP32 is a low-cost System on Chip (SoC) Microcontroller from Espressif Systems, the developers of the famous ESP8266 SoC. It is a successor to ESP8266 SoC and comes in both single-core and dual-core variations of the Tensilica’s 32-bit Xtensa LX6 Microprocessor with integrated Wi-Fi and Bluetooth.

The good thing about ESP32, like ESP8266 is its integrated RF components like Power Amplifier, Low-Noise Receive Amplifier, Antenna Switch, Filters and RF Balun. This makes designing hardware around ESP32 very easy as you require very few external components.

ESP32 Button LED Wiring Diagram

Another important thing to know about ESP32 is that it is manufactured using TSMC’s ultra-low-power 40 nm technology. So, designing battery operated applications like wearables, audio equipment, baby monitors, smart watches, etc., using ESP32 should be very easy.

Specifications of ESP32

ESP32 has a lot features and it is difficult to include all the specifications in this Getting Started with ESP32 guide. So, I made a list of some of the important specifications of ESP32 here. But for complete set of specifications, I strongly suggest you to refer to the Datasheet.

  • Single or Dual-Core 32-bit LX6 Microprocessor with clock frequency up to 240 MHz.
  • 520 KB of SRAM, 448 KB of ROM and 16 KB of RTC SRAM.
  • Supports 802.11 b/g/n Wi-Fi connectivity with speeds up to 150 Mbps.
  • Support for both Classic Bluetooth v4.2 and BLE specifications.
  • 34 Programmable GPIOs.
  • Up to 18 channels of 12-bit SAR ADC and 2 channels of 8-bit DAC
  • Serial Connectivity include 4 x SPI, 2 x I2C, 2 x I2S, 3 x UART.
  • Ethernet MAC for physical LAN Communication (requires external PHY).
  • 1 Host controller for SD/SDIO/MMC and 1 Slave controller for SDIO/SPI.
  • Motor PWM and up to 16-channels of LED PWM.
  • Secure Boot and Flash Encryption.
  • Cryptographic Hardware Acceleration for AES, Hash (SHA-2), RSA, ECC and RNG.

Pinout of ESP32 Board

The ESP32 is available in 46 versions currently. The trend is expected to increase further in the future as announced by Espressif on thier 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.

ESP32 Button LED Wiring Diagram

※ NOTE THAT:

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 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) …

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 Button LED Wiring Diagram

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.

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.

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.

※ NOTE THAT:

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 Button LED Wiring Diagram

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.

ESP32 Button LED Wiring Diagram

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.

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 Button LED Wiring Diagram

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);

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.

ESP32 Button LED Wiring Diagram

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.

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);                

Digital to Anlog 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.

Comparason Between UART vs I2C vs SPI

Protocol UART I2C SPI
Complexity Simple Easy to chain multiple devices Complex as device increases
Speed Slowest Faster than UART Fastest
Number of devices Up to 2 devices Up to 127, but gets complex Many, but gets complex
Number of wires 1 2 4
Duplex Full Duplex Half Duplex Full Duplex
No. of masters and
slaves
Single to Single Multiple slaves and masters 1 master, multiple slaves

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!