How to create a new component in ESP-IDF Vs code

Understanding Components in ESP-IDF

Before we dive into the practical steps, let's briefly understand what components are in the ESP-IDF framework. Components in ESP-IDF encapsulate functionality and can be shared across multiple projects. They promote code reusability, modularity, and maintainability in your projects.


Before we begin, make sure you have the following prerequisites installed:

  1. ESP-IDF: Ensure you have the ESP-IDF development framework installed on your system.

  2. VS Code: Download and install Visual Studio Code, a powerful and versatile code editor.

  3. ESP-IDF Extension: Install the ESP-IDF extension for VS Code, which provides valuable tools and functionalities for ESP-IDF development.

    Create Component in esp-idf

To create the new component in the file, follow the below steps:

  1. Go to the search bar in the file at the top middle of the Vs code software

  2. Type ">" to get the suggestions

  3. Select the "ESP-IDF: Create New ESP-IDF Component"

  4. Enter the name of the component

  5. Once enter the name click Enter to create the component, In my case test

Make sure the CMakeList.text is created in the component

Call the component Function to the main.c
To call the component function

  1. In the below, I have added the component function to the test.c as component();

You can see the below image there is a folder created named Component>test

  1. Call the component function in the main.c
    i. You should include the function .h file in the main file to call another function.
    ii. In the loop I am going to call the function, component();

Then build and run the project