Skip to main content

Working with ESP-IDF

This chapter contains the following content. Read it as needed:

ESP-IDF Getting Started Tutorial​

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.

Development Environment Setup​

info

For the ESP32-S3-Touch-LCD-4 board, ESP-IDF V5.2.5 or later is required.

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 Programs​

Demo-flow-01

ESP32-S3-Touch-LCD-4 example programs

ExampleBasic example description
01_RS485_TestTests the RS-485 connector
02_SD_TestTests the TF card slot
03_RTC_TestRTC clock and RTC interrupt
04_TWAIreceiveTests the CAN connector
05_TWAItransmitTests the CAN connector
06_LVGL_ProtingTests the RGB touchscreen

01_RS485_Test​

Hardware Connection

ESP32-S3-Touch-LCD-4-demo-04

Code Analysis

  • echo_task() :
    • First configures the UART parameters, including the baud rate, data bits, parity, stop bits, and hardware flow control.
    • Then installs the UART driver, sets the UART pins, and allocates a temporary buffer for receiving data.
    • In an infinite loop, it reads data from the UART, writes the data back to the UART, and logs information when data is received.

Results

  • Open a serial debugging assistant and send a message to the ESP32-S3-Touch-LCD-4; the device returns the received message to the serial debugging assistant

ESP32-Arduino-32

02_SD_Test​

Hardware Connection

  • Connect the board to the computer with a USB cable
  • Insert the TF card into the board

ESP32-S3-Touch-LCD-4-demo-03

Code Analysis

  • waveshare_sd_card_init() :

This function is mainly used to initialize the TF card. It first initializes I2C, then pulls the TF card CS pin low through an I2C-controlled chip. Next it configures the TF card mount options, including whether to format on mount failure, the maximum number of files, and the allocation unit size. After that it initializes the SPI bus and mounts the TF card file system using the configured SPI bus and mount options. If mounting succeeds, it returns ESP_OK, indicating that TF card initialization is complete.

  • waveshare_sd_card_test() :

This function tests the TF card functions. It first prints the information of the initialized TF card. Then it creates a file and writes data, renames the file, and reads the content of the renamed file. Next it formats the file system and checks whether the file was deleted after formatting. Finally it creates a new file and reads its content; after the test it unmounts the TF card and releases the SPI bus resources.

Results

  • After a successful flash, the serial port prints information about the memory card, such as the name, type, capacity, and maximum supported frequency, and then creates a file, writes to it, renames it, and reads the renamed file:

ESP32-S3-Touch-LCD-7-demo-20

  • The ESP-IDF serial monitor can also be used directly

03_RTC_Test​

Hardware Connection

  • Connect the board to the computer with a USB cable

Code Analysis

  • app_main() :
    • First defines the structure for storing the current time and some variables, including the alarm interrupt flag.
    • Then initializes the chip, sets the initial time and the alarm time, enables the alarm function, and initializes the interrupt input pin and the interrupt handler. In an infinite loop, it reads the current time, converts it to a string, and prints it; when the alarm interrupt occurs, it resets the interrupt flag and may re-enable the alarm, and prints a message that the alarm went off; the loop also delays for 1 second.
    • Overall it implements a simple alarm function, implementing time reading and alarm response through operations on the PCF85063A chip and interrupt handling.

Results

  • After a successful flash, it sets the time, starts the alarm, then reads the current time and waits for the alarm
  • The serial terminal prints the relevant information

ESP32-IDF-17

04_TWAIreceive​

Hardware Connection

  • Connect the board to the computer with a USB cable
  • Connect the board to a USB-CAN-A, as shown

ESP32-S3-Touch-LCD-4-demo-05

Code Analysis

  • waveshare_twai_receive() :
    • If the driver is not installed, waits for a while and returns a failure state
    • Reads the triggered alerts and obtains the TWAI status information
    • Prints the corresponding log information for the different alert types triggered, including the error-passive alert, bus error alert, and receive queue full alert, and prints the related status information
    • If the receive-data alert is triggered, receives messages in a loop and calls the handle_rx_message function to process each received message. Finally returns a success state

Results

  • The ESP32-S3-Touch-LCD-4 waits for the USB-CAN-A_TOOL to send a message, and prints it to the serial port if reception succeeds

ESP32-Arduino-CN-2

ESP32-Arduino-23

  • Open a serial debugging assistant and you can see that the ESP32-S3-Touch-LCD-4 has started receiving CAN messages (if reception reports errors, try resetting and restarting the device in software several times, and wait patiently)

ESP32-S3-Touch-LCD-7-demo-15

  • If the following error appears, click the serial monitor and resend the data with the debugging tool. (After pressing reset, you may also need to click the serial monitor again):

ESP32-IDF-22

05_TWAItransmit​

Hardware Connection

  • Connect the board to the computer with a USB cable
  • Connect the board to a USB-CAN-A, as shown

ESP32-S3-Touch-LCD-4-demo-05

Code Analysis

  • waveshare_twai_transmit() :
    • If the driver is not installed, waits for a while and returns a failure state
    • Reads the triggered alerts and obtains the TWAI status information
    • Prints the corresponding log information for the different alert types, including the error-passive alert, bus error alert, transmission failed alert, and transmission success alert, and prints the related status information
    • Determines whether it is time to send a message, and if so, sends the message and updates the last transmission time

Results

  • The serial port prints that the CAN message was sent successfully; after configuring the USB-CAN-A_TOOL and starting it, you can see the CAN messages sent by the ESP32-S3-Touch-LCD-4.

ESP32-S3-Touch-LCD-7-demo-14

  • Then look at the USB-CAN-A_TOOL to see the CAN messages sent by the ESP32-S3-Touch-LCD-4

ESP32-S3-Touch-LCD-7-demo-16

06_LVGL_Proting​

Hardware Connection

  • Connect the board to the computer with a USB cable

ESP32-S3-Touch-LCD-4-demo-02

Code Analysis

  • app_main() :
    • Initializes the Waveshare ESP32-S3 RGB LCD and uses pin 2 of the TCA9554 IO expander chip to drive the level high or low and thereby control the screen backlight.
    • Then prints a message indicating that the LVGL demo content is about to be displayed; because the LVGL API is not thread-safe, it locks the mutex first.
    • Then it can run different LVGL demo programs, such as lv_demo_stress, lv_demo_benchmark, lv_demo_music, lv_demo_widgets, or example_lvgl_demo_ui.
    • Finally it releases the mutex.

Results

  • After flashing the code, a series of touch operations can be performed, providing an LVGL porting example; this example adds the LVGL anti-tearing function, for a better experience than the Arduino IDE approach

ESP32-S3-Touch-LCD-4-demo-06