Beginner’s Guide to Transitioning from Arduino to ESP32: Advanced Edition

Advanced Edition

Continuing from the beginner’s guide on how to quickly transition from Arduino to ESP32 – Basic Edition, let’s further explore the various pin interfaces of the ESP32!

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

Content Introduction

  1. 1. SPI Pins

  2. 2. I2C Pins

  3. 3. UART Pins

  4. 4. RTC Pins

  5. 5. Strapping Pins

  6. 6. EN Pins

SPI Pins

Pin Name Corresponding Pin
VSPI_MOSI 23
VSPI_MISO 19
VSPI_CLK 18
VSPI_CS 5
HSPI_MOSI 13
HSPI_MISO 12
HSPI_CLK 14
HSPI_CS 15
CS 2

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

What is SPI

SPI (Serial Peripheral Interface) is a technology used for connecting devices. Through SPI, the ESP32 can transfer data with other sensors that also use SPI connections.

SPI transmission uses a synchronous timeline, also known as a shared clock signal, which helps ensure the accuracy and consistency of transmitted data.

SPI is mainly used in scenarios that require short-distance, high-speed serial communication, such as displays needing fast synchronized data, SD card readers, accelerometers, etc.

You can think of SPI as a precise way of dialogue, where the master device (ESP32) and the slave device (sensors, displays, etc.) communicate information according to specific rules under a common clock.

In SPI communication, there are typically four signal lines:

  1. 1. MOSI (Master Out Slave In): The line for the master device to send data to the slave device.

  2. 2. MISO (Master In Slave Out): The line for the slave device to send data to the master device.

  3. 3. SCLK (Serial Clock): The clock signal used to synchronize data transmission between the master and slave devices.

  4. 4. SS/CS (Slave Select/Chip Select): The line that selects the slave device, indicating when the slave device is ready to receive or send data.

Notes on Using SPI

Avoid Conflicts

When we deal with using pins, especially when handling devices that are not SPI interfaces, it is advisable to avoid directly using SPI interfaces. This is mainly because, during the introduction of libraries, some key pins of the SPI interface, such as SCLK, MOSI, and MISO, are typically preset for SPI functionality, which may lead to conflicts.

Customizable SPI Pins

In the ESP32, you can adjust the configuration of the SPI interface through code, including changing the pins for SCLK, MOSI, MISO, etc. This provides greater flexibility to accommodate different hardware connection needs.

Shared Pins

If you need to connect multiple SPI interface devices simultaneously, you can share the SCLK, MOSI, and MISO pins, and then differentiate different devices using separate CS pins.

Choosing between VSPI and HSPI

In the ESP32, VSPI and HSPI are two available SPI interfaces, and their functions are the same. Some libraries may default to using one of them, mostly VSPI.

I2C Pins

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

What is I2C

I2C (Inter-Integrated Circuit) is similar to SPI and is also used for communication with various devices.

The rules for devices to communicate with each other are typically referred to as serial communication protocols.

Notes on Using I2C

I2C Pins

I2C only requires two pins, regardless of how many I2C devices are connected, which are SDA and SCL. This makes it very convenient for connecting multiple devices, as it occupies relatively few pins.

Differences between I2C and SPI SDA

  • • SPI’s SDA: In SPI communication, SDA usually refers to MOSI (Master Out Slave In), which means the master device outputs data to the slave device.

  • • I2C’s SDA: In I2C communication, SDA stands for Serial Data Line, which is used for bidirectional data transmission.

Differences between I2C and SPI SCL

  • • SPI’s SCL: In SPI communication, the clock line is usually called SCLK (Serial Clock), which is used to synchronize data transmission, with the master device sending clock pulses to the slave device to ensure effective data transfer.

  • • I2C’s SCL: In I2C communication, SCL stands for Serial Clock Line, which is also used to synchronize data transmission, with the master device sending clock signals to the slave device.

If a device uses the I2C protocol for communication, its SCL and SDA lines should be connected to the I2C bus. Similarly, if a device uses the SPI protocol for communication, its SCLK, MISO, MOSI, etc., should be connected to the SPI bus. These two types of lines cannot be directly interconnected; otherwise, communication will not function correctly.

UART Pins

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

What is UART

UART stands for Universal Asynchronous Receiver/Transmitter. Like SPI and I2C, it is a serial communication protocol.

However, unlike them, UART does not have a shared clock signal. In UART communication, the sending and receiving devices coordinate data transmission through start bits, data bits, parity bits, and stop bits.

It can be understood as:

  • • UART: Synchronizing information through relative timing.

  • • SPI, I2C: Ensuring communication synchronization through absolute clock signals.

In UART, devices interpret and synchronize data through a pre-agreed sequence of bits. This asynchronous method makes UART more flexible and suitable for scenarios that do not require strict clock synchronization.

Notes on Using UART

In simple terms, only use UART2; UART0 cannot be used.

UART0

Connected to USB, usually used for flashing and debugging.

UART2

UART2 can connect to external devices such as GPS, fingerprint sensors, distance sensors, etc., without affecting flashing and debugging functions.

RTC Pins

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

What is RTC

When we use RTC (Real-Time Clock), it is actually an independent clock system. Regardless of whether the main power is off, the RTC can continue to be powered to ensure the device maintains accurate time even during power outages.

In the ESP32, we use RTC to wake up devices from sleep mode to achieve power-saving functionality.

Using RTC to wake devices from sleep mode is a common power-saving strategy in devices like the ESP32. Sleep mode (or deep sleep) is a very low-power mode where the main processor is turned off, but the RTC remains running to ensure the device can wake up at a scheduled time. This is very useful for periodically executing tasks, timed data collection, or waking up the device when specific events occur.

Some common application scenarios include:

  • • Timed Wake-Up: Waking the device at a scheduled time to perform specific tasks, such as data collection, communication, etc.

  • • Event-Triggered Wake-Up: Waking the device through RTC when certain specific events occur to respond immediately to the event.

  • • Power-Saving Mode for Battery-Powered Devices: Using RTC can help minimize power consumption and extend battery life in battery-powered devices.

Strapping Pins

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

In the ESP32, there are two important modes, namely BOOT mode and FLASH mode. BOOT mode is used to run programs on the ESP32, while FLASH mode is used to upload programs to the ESP32. Strapping pins are used to configure the operating mode of the ESP32. Typically, development boards with built-in USB/Serial will automatically configure these pins to the correct state to support flashing or booting. However, if other devices use these pins, it may cause the ESP32 to enter the wrong mode.

Specifically, Strapping pins are a set of pins used to configure the boot mode. On development boards with built-in USB/Serial, the system automatically configures these pins as needed. However, when other devices use the same pins, it may affect the ESP32’s boot mode, so special attention is needed when designing hardware connections.

EN Pins

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

For the ESP32, the EN (Enable) pin is a critical pin used to control the chip’s enable and disable functions. Here are some basic details about the EN pin:

  • • Default State: The EN pin is pulled high by default, indicating that the chip is enabled. When the EN pin is pulled high, the ESP32 can operate normally.

  • • Pulled Low to Disable: When the EN pin is pulled low, the chip is disabled, meaning it stops working. Pulling the EN pin low usually leads to the chip being powered off or entering low-power mode, depending on the system design.

  • • Reset Function: The EN pin is also connected to a button switch, and pressing the button pulls the EN pin low, triggering a reset operation for the chip. This button provides a way to manually reset the chip.

Pulled High: Applying high potential to the pin; Pulled Low: Applying low potential to the pin.

Overall, the EN pin provides control over enabling and disabling the ESP32 chip while allowing manual reset via a button. This is a very useful feature during development and testing, as it allows users to manually reset the chip or disable it to reduce power consumption when needed.

Error Correction Notes

ESP32 WROOM-32 Upload Selection

When using the Arduino IDE to upload programs to the ESP32 development board, for common ESP32 development boards, such as the ESP32 WROOM-32, you can select “ESP32 Dev Module” in the Arduino IDE for uploading. This board type is usually supported by default in the Arduino IDE.

The “ESP32 WROOM DA” option used in previous articles is for another type of ESP32 module development board.

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced EditionAlthough using the “ESP32 WROOM-32” development board can also successfully select “ESP32 WROOM DA” for uploading, this may be because the “ESP32 WROOM-32” also has corresponding support in the “esp32” support package of the Arduino IDE.

Therefore, it is ultimately recommended to select “ESP32 Dev Module” when using the ESP32 WROOM-32 development board to ensure more comprehensive support and compatibility in the Arduino IDE. This will facilitate development and debugging, ensuring that the code can be correctly uploaded to the ESP32 development board.

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

In the future, I will continue to share teaching articles for beginners in ESP32,

and I also welcome you to follow my video account and Bilibili: Teacher Eva is here.

On November 26, there will be a workshop for beginners; interested parties are welcome to scan the code to sign up!

Beginner's Guide to Transitioning from Arduino to ESP32: Advanced Edition

Leave a Comment

Your email address will not be published. Required fields are marked *