Skip to main content

Working with ESP-IDF

This chapter includes the following sections, please read as needed:

ESP-IDF Getting Started

New to ESP32 ESP-IDF development and looking to get started quickly? We have prepared a general Getting Started Tutorial for you.

Please Note: This tutorial uses the ESP32-S3-Zero as a teaching example, and all hardware code is based on its pinout. Before you start, it is recommended that you check the pinout of your development board to ensure the pin configuration is correct.

Setting Up the Development Environment

info

The V1 examples require ESP-IDF V5.1.4 or later. Note: When using ESP-IDF V5.2.0 or later, 10_FactoryProgram may fail to scan for nearby Wi-Fi networks. To test this feature, please compile with an earlier version, or directly flash the BIN firmware provided in the V1 resource package.

The 09_LVGL_V9_Test in the V2 repository is configured for ESP-IDF V5.5.2, with its dependency lock file pinned to V5.5.4. When working with V2 examples, please prioritize using ESP-IDF V5.5.x.

note

The following guide uses Windows as an example, demonstrating development using VS Code + the ESP-IDF extension. macOS and Linux users should refer to the official documentation.

Version Selection

The screenshots in this section use ESP-IDF V5.5.2 as an example. When installing, please select the ESP-IDF version that matches your board's example.

Install the ESP-IDF Development Environment

  1. Download the installation manager from the ESP-IDF Installation Manager page. This is Espressif's latest cross-platform installer. The following steps demonstrate how to use its offline installation feature.

    Click the Offline Installer tab on the page, then select Windows as the operating system and the ESP-IDF version you need (the version shown in the screenshot is for reference only — choose the version that fits your actual needs).

    Download EIM and offline package

    After confirming your selection, click the download button. The browser will automatically download two files: the ESP-IDF Offline Package (.zst) and the ESP-IDF Installer (.exe).

    Download EIM and offline package 2

    Please wait for both files to finish downloading.

  2. Once the download is complete, double-click to run the ESP-IDF Installer (eim-gui-windows-x64.exe).

    The installer will automatically detect if the offline package exists in the same directory. Click Install from archive.

    Auto-detect offline package

    Next, select the installation path. We recommend using the default path. If you need to customize it, ensure the path does not contain Chinese characters or spaces. Click Start installation to proceed.

    Select installation path
  3. When you see the following screen, the ESP-IDF installation is successful.

    Installation successful
  4. We recommend installing the drivers as well. Click Finish installation, then select Install driver.

    Install drivers via ESP-IDF Installation Manager

Install Visual Studio Code and the ESP-IDF Extension

  1. Download and install Visual Studio Code.

  2. During installation, it is recommended to check Add "Open with Code" action to Windows Explorer file context menu to facilitate opening project folders quickly.

  3. In VS Code, click the Extensions icon Extensions Icon in the Activity Bar on the side (or use the shortcut Ctrl + Shift + X) to open the Extensions view.

  4. Enter ESP-IDF in the search box, locate the ESP-IDF extension, and click Install.

    Search and install ESP-IDF extension in VS Code

  5. For ESP-IDF extension versions ≥ 2.0, the extension will automatically detect and recognize the ESP-IDF environment installed in the previous steps, requiring no manual configuration.

Example

Version Note

Some pin definitions differ between V1 and V2, so the ESP-IDF example programs are not interchangeable. Please confirm the actual hardware version of your board first, and then download the corresponding version from Related Resources.

The ESP-IDF example programs are located in:

  • V1: ESP-IDF
  • V2: 02_Example/ESP-IDF

The following example descriptions are based on the V1 package. The directory names and implementations of V2 examples may differ; please refer to the programs in the V2 repository.

Example Flow

ExampleBasic DescriptionDependency Library
01_ADC_TestRead the current system voltage value-
02_I2C_PCF85063Print real-time time from the RTC chip-
03_I2C_QMI8658Print the raw data from the IMU-
04_SD_CardLoad and display TF card information-
05_WIFI_STASet to STA mode, connect to Wi-Fi and obtain IP address-
06_WIFI_APSet to AP mode to obtain the IP address of the access device-
07_EX_GPIOUse extended I/O and internal I/O-
08_Li_ION_TestEnable lithium battery-
09_LVGL_TestLVGL exampleLVGL
10_FactoryProgramComprehensive projectLVGL

01_ADC_Test

This example converts the analog voltage connected through GPIO to digital values via ADC, then calculates the actual system voltage and prints it to the terminal.

ESP-IDF Example 1

ADC_CHANGPIO_PIN
ADC2_CHANNEL_6GPIO17

Code Walkthrough

  • adc_bsp_init(void): Initializes ADC2, which includes creating an ADC oneshot unit and configuring ADC2 channel 6.
  • adc_get_value(float *value, int *data): Reads the value from ADC2 channel 6 and calculates the corresponding voltage based on the reference voltage and resolution. If the read fails, it stores a value of 0.
  • adc_example(void* parameter): After initializing ADC2, this function creates an ADC task that reads the ADC value and calculates the system voltage every 1 second.

Expected Behavior: Once the program is flashed, open the Serial Monitor to view the printed ADC values and voltages. The ADC sample value is approximately 1628, and the BAT voltage is about 4.2 V.

02_I2C_PCF85063

This example uses the I2C protocol to initialize the PCF85063 chip, set the time, periodically read the time, and print it to the terminal.

RTCGPIO_PIN
RTC_SDAGPIO47
RTC_SCLGPIO48
RTC_INTEXIO4

Code Walkthrough

  • void PCF85063_example(void* parameter): Creates an RTC task to implement RTC functionality. It reads the clock from the RTC chip every 10 seconds and outputs it to the terminal.

Expected Behavior: Once the firmware is flashed, open the Serial Monitor to view the printed RTC time. The data is output every 10 seconds.

03_I2C_QMI8658

This example uses the I2C protocol to initialize the QMI8658 chip, then reads the corresponding attitude data and prints it to the terminal every 1 second.

IMUGPIO_PIN
IMU_SDAGPIO47
IMU_SCLGPIO48
IMU_INT1EXIO4
IMU_INT2EXIO3

Code Walkthrough

  • qmi8658c_example(void* parameter): Initializes the QMI8658 device. In an infinite loop, it reads and prints the accelerometer, gyroscope, and temperature data every 1 second.

Expected Behavior: Open the Serial Monitor to view the raw data from the IMU (Euler angles need to be converted manually). The data is output every 1 second.

04_SD_Card

This example uses a macro definition to select either SPI or SDMMC to drive the TF card. Once the TF card is successfully mounted, its information is printed to the terminal.

ESP-IDF Example 4

SPI/MMCGPIO_PIN
CSGPIO2
MISO/D0GPIO6
MOSI/CMDGPIO5
SCLK/MCLKGPIO4

Code Walkthrough

  • The user can select the communication protocol for the TF card. In the sd_card_bsp.c source file, find the macro SD_Read_Mode. By default, it uses the SDMMC communication protocol, but can be changed to SDSPI.
#define SD_Read_Mode USER_SPI

Expected Behavior: Click on the Serial Monitor device to view the printed TF card information. practical_size indicates the actual capacity of the TF card.

05_WIFI_STA

This example configures the development board as a station that can connect to an available AP. Upon successful connection, it prints the obtained IP information to the terminal.

ESP-IDF Example 5

Code Modification: This project configures the chip in STA mode and connects to Wi-Fi. Before compiling and downloading the firmware, modify the Wi-Fi SSID and password in the code to match your network credentials.

Code Walkthrough

  • espwifi_Init(void): Initializes Wi-Fi on the ESP32. It sequentially initializes non-volatile storage, the TCP/IP stack, creates the default event loop and the default Wi-Fi station network interface, registers event handlers, sets Wi-Fi connection parameters, and starts Wi-Fi.

Expected Behavior: The chip connects to Wi-Fi in STA mode and obtains an IP address.

06_WIFI_AP

This example configures the development board as an Access Point (AP) waiting for STA connections. Upon a successful connection, the assigned IP address is printed to the terminal.

Code Walkthrough

  • wifi_init_softap(void): Initializes the ESP32's Wi-Fi SoftAP, including setting up the network interface, registering event handlers, configuring SoftAP parameters, and starting the SoftAP.

Expected Behavior: When the chip is in AP mode, after a phone connects to the Wi-Fi, the serial port outputs the MAC address and assigned IP address of that device.

07_EX_GPIO

This example tests the basic functionality of the extended I/O and internal GPIO.

ESP-IDF Example 7

Hardware Connection: You need to use two wires to connect EXIO5 to EXIO6, and GPIO2 to GPIO1.

GPIOxGPIOy
GPIO1GPIO2
EXIO5EXIO6

Code Walkthrough

  • I2C_master_Init(): Initializes I2C, which is the communication protocol for the external extended I/O.
  • esp32_gpio_init(): Initializes GPIO, setting GPIO1 and EXIO5 as outputs, and GPIO2 and EXIO6 as inputs.
  • example_GPIO_task(): Creates a separate task to test GPIO functionality; can be modified as needed.

Expected Behavior: The serial monitor outputs GPIO test passed. to indicate success, and GPIO test failed. to indicate failure.

08_Li_ION_Test

This example controls GPIO to enable power supply from the lithium battery.

Code Walkthrough

  • BAT_GPIO_Init(): Uses GPIO16 to control the lithium battery power supply; initializes GPIO16.
  • BAT_ON(): Enables the lithium battery to supply power to the system.
  • BAT_OFF(): Disables the lithium battery power supply to the system.

Expected Behavior: After enabling the lithium battery power supply in software, connect the battery, and then the PWR button can be used to control power delivery from the lithium battery.

09_LVGL_Test

This example ports LVGL to implement some multi-functional GUI interfaces on the screen.

Example 9 Rotated 90° Rotate_90

Example 9 No Rotation Rotate_NONO

Code Walkthrough

  • lv_demo_widgets(): Verifies screen performance by running an LVGL example.

Code Modification: If you need to rotate the display 90 degrees, locate the AMOLED_Rotate macro definition in the source file where main() resides, and choose one of the two:

#define AMOLED_Rotate Rotate_90 // Landscape
#define AMOLED_Rotate Rotate_NONO // Portrait

Expected Behavior: The LVGL example consumes a significant amount of RAM and ROM; ensure the development board is configured according to the environment setup requirements. The operation result after programming is shown in the images above.

10_FactoryProgram

This example is a comprehensive project that tests onboard features.

Version Note

This note applies only to V1: When compiled with ESP-IDF V5.2.0 or later, the program may fail to scan for nearby Wi-Fi networks. If you need to test this, compile with an older version, or directly flash the BIN firmware provided in the V1 package.

Expected Behavior

  • Swipe left or right to switch pages. It first displays RGB colors every 1.5 seconds to check the screen for issues.
  • After displaying RGB, it automatically jumps to the clock interface.
  • Swipe left to see onboard hardware information.
  • Swipe left again to enter the function interface.
    • Tap the Wi-Fi icon to enter the Wi-Fi test interface, then tap "Scan" to search for nearby Wi-Fi networks.
    • Tap the BLE icon to enter the BLE test screen, then tap "Scan" to search for nearby BLE devices.
  • Swipe left again to enter the brightness adjustment interface.

Factory Example 1 Factory Example 2 Factory Example 3 Factory Example 4 Factory Example 5 Factory Example 6 Factory Example 7 Factory Example 8 Factory Example 9 Factory Example 10