The ESP32 microcontroller, developed by Espressif Systems, has gained widespread popularity for its versatility, low cost, and robust performance. To harness the full potential of ESP32 devices and develop feature-rich applications, developers often rely on the Espressif IoT Development Framework (ESP-IDF). In this article, we’ll explore the process of programming ESP32 devices using the ESP-IDF framework in the command-line interface (CLI), offering flexibility and efficiency in development workflows.
While using Arduino IDE is easy to use for smaller projects, using ESP-IDF Framework is the professional way for development since it unleashes the full power of ESP32.
Understanding ESP-IDF and CLI Programming
The ESP-IDF framework provides a comprehensive set of libraries, tools, and components for developing applications for ESP32-based devices. While IDEs like PlatformIO and Arduino IDE offer graphical interfaces for ESP-IDF development, programming in the CLI environment offers greater control, customization, and integration with version control systems and automated build pipelines.
---
Prerequisites & Steps
This guide is for installing and working with the CLI tool. That is the first of the setup. You’ll get the online installer here:
1 | https://dl.espressif.com/dl/esp-idf/ |
We recommend using the online installer since in that way you will get the latest updates. The total size is around 1.5GB but the CDNs are fast enough. In my case, among the prerequisite tools Python 3, Git, cross-compilers, and CMake were already installed on my Windows computer. The Ninja build tools were installed by the wizard. Here is the official documentation:
1 | https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/windows-setup.html |
Launch the installer, click on next, leave the settings on default or select which components you want to install. The ESP-IDF will now begin to download and install, this will take a few minutes.
Zero bug. After everything is ready, the wizard will launch the Windows Command Line Tool. Also, you’ll find shortcut icons on your desktop to launch the CMD tool and PowerShell. In the case of PowerShell, you may have to add the path of the PowerShell executable, which is in my case:
1 | C:\Users\abhishekghosh\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell |
Whenever you launch PowerShell, it will return this output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | ... Setting PYTHONNOUSERSITE, was not set Using Python in C:\Users\abhishekghosh\.espressif\python_env\idf5.2_py3.11_env\Scripts Python 3.11.2 Using Git in C:\Users\abhishekghosh\.espressif\tools\idf-git\2.43.0\cmd git version 2.43.0.windows.1 Setting IDF_PATH: C:\Espressif\frameworks\esp-idf-v5.2.1 Checking Python compatibility Adding ESP-IDF tools to PATH... Name Value ---- ----- OPENOCD_SCRIPTS C:\Users\abhishekghosh\.espressif\tools\openocd-esp32\v0.12.0-esp32-20230921\openocd-... IDF_CCACHE_ENABLE 1 ESP_ROM_ELF_DIR C:\Users\abhishekghosh\.espressif\tools\esp-rom-elfs\20230320\ IDF_PYTHON_ENV_PATH C:\Users\abhishekghosh\.espressif\python_env\idf5.2_py3.11_env ESP_IDF_VERSION 5.2 IDF_DEACTIVATE_FILE_PATH C:\Users\ABHISH~1\AppData\Local\Temp\tmpxbh9s1q4idf_21164 Added to PATH ------------- C:\Users\abhishekghosh\.espressif\tools\xtensa-esp-elf-gdb\12.1_20231023\xtensa-esp-elf-gdb\bin C:\Users\abhishekghosh\.espressif\tools\riscv32-esp-elf-gdb\12.1_20231023\riscv32-esp-elf-gdb\bin C:\Users\abhishekghosh\.espressif\tools\xtensa-esp-elf\esp-13.2.0_20230928\xtensa-esp-elf\bin C:\Users\abhishekghosh\.espressif\tools\riscv32-esp-elf\esp-13.2.0_20230928\riscv32-esp-elf\bin C:\Users\abhishekghosh\.espressif\tools\esp32ulp-elf\2.35_20220830\esp32ulp-elf\bin C:\Users\abhishekghosh\.espressif\tools\cmake\3.24.0\bin C:\Users\abhishekghosh\.espressif\tools\openocd-esp32\v0.12.0-esp32-20230921\openocd-esp32\bin C:\Users\abhishekghosh\.espressif\tools\ninja\1.11.1\ C:\Users\abhishekghosh\.espressif\tools\idf-exe\1.0.3\ C:\Users\abhishekghosh\.espressif\tools\ccache\4.8\ccache-4.8-windows-x86_64 C:\Users\abhishekghosh\.espressif\tools\dfu-util\0.11\dfu-util-0.11-win64 C:\Espressif\frameworks\esp-idf-v5.2.1\tools Checking if Python packages are up to date... Constraint file: C:\Users\abhishekghosh\.espressif\espidf.constraints.v5.2.txt Requirement files: - C:\Espressif\frameworks\esp-idf-v5.2.1\tools\requirements\requirements.core.txt Python being checked: C:\Users\abhishekghosh\.espressif\python_env\idf5.2_py3.11_env\Scripts\python.exe Python requirements are satisfied. Detected installed tools that are not currently used by the active ESP-IDF version. To remove old versions of idf-driver use command 'python.exe C:\Espressif\frameworks\esp-idf-v5.2.1\tools\idf_tools.py uninstall' For free up even more space, remove installation packages of those tools. Use option 'python.exe C:\Espressif\frameworks\esp-idf-v5.2.1\tools\idf_tools.py uninstall --remove-archives'. Done! You can now compile ESP-IDF projects. Go to the project directory and run: idf.py build |
In the case of Windows CMD, you’ll get this output:
1 | Using Python in C:\Users\abhishekghosh\.espressif\python_env\idf5.2_py3.11_env\Scripts\ Python 3.11.2 Using Git in C:\Users\abhishekghosh\.espressif\tools\idf-git\2.43.0\cmd\ git version 2.43.0.windows.1 Checking Python compatibility Setting IDF_PATH: C:\Espressif\frameworks\esp-idf-v5.2.1 Adding ESP-IDF tools to PATH... C:\Users\abhishekghosh\.espressif\tools\xtensa-esp-elf-gdb\12.1_20231023\xtensa-esp-elf-gdb\bin C:\Users\abhishekghosh\.espressif\tools\riscv32-esp-elf-gdb\12.1_20231023\riscv32-esp-elf-gdb\bin C:\Users\abhishekghosh\.espressif\tools\xtensa-esp-elf\esp-13.2.0_20230928\xtensa-esp-elf\bin C:\Users\abhishekghosh\.espressif\tools\riscv32-esp-elf\esp-13.2.0_20230928\riscv32-esp-elf\bin C:\Users\abhishekghosh\.espressif\tools\esp32ulp-elf\2.35_20220830\esp32ulp-elf\bin C:\Users\abhishekghosh\.espressif\tools\cmake\3.24.0\bin C:\Users\abhishekghosh\.espressif\tools\openocd-esp32\v0.12.0-esp32-20230921\openocd-esp32\bin C:\Users\abhishekghosh\.espressif\tools\ninja\1.11.1\ C:\Users\abhishekghosh\.espressif\tools\idf-exe\1.0.3\ C:\Users\abhishekghosh\.espressif\tools\ccache\4.8\ccache-4.8-windows-x86_64 C:\Users\abhishekghosh\.espressif\tools\dfu-util\0.11\dfu-util-0.11-win64 C:\Espressif\frameworks\esp-idf-v5.2.1\tools Checking if Python packages are up to date... Constraint file: C:\Users\abhishekghosh\.espressif\espidf.constraints.v5.2.txt Requirement files: - C:\Espressif\frameworks\esp-idf-v5.2.1\tools\requirements\requirements.core.txt Python being checked: C:\Users\abhishekghosh\.espressif\python_env\idf5.2_py3.11_env\Scripts\python.exe Python requirements are satisfied. Detected installed tools that are not currently used by active ESP-IDF version. For removing old versions of idf-driver use command 'python.exe C:\Espressif\frameworks\esp-idf-v5.2.1\tools\idf_tools.py uninstall' For free up even more space, remove installation packages of those tools. Use option 'python.exe C:\Espressif\frameworks\esp-idf-v5.2.1\tools\idf_tools.py uninstall --remove-archives'. Done! You can now compile ESP-IDF projects. Go to the project directory and run: idf.py build C:\Espressif\frameworks\esp-idf-v5.2.1> |
The above return indicates that there is no error in setup.
How to Use the ESP-IDF Framework in CLI
Now connect your ESP32 board to the computer and check under which serial port the board is visible. Serial port names start with COM in Windows. In my example, it is COM8. If you are not sure how to check the serial port name, open the Device Manager from the Windows start menu and get the port number.
Go to the ESP-IDF’s home location, which is this in my case:
1 | C:\Espressif\frameworks\esp-idf-v5.2.1 |
cd to examples/get-started directory. You’ll get hello_world example here. cd to that directory and do an ls:
1 2 3 4 5 6 7 8 9 | Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12-5-24 10:33 build da---- 12-5-24 9:55 main -a---- 12-5-24 9:55 242 CMakeLists.txt -a---- 12-5-24 9:55 1661 pytest_hello_world.py -a---- 12-5-24 9:55 2474 README.md -a---- 12-5-24 10:23 57704 sdkconfig -a---- 12-5-24 9:55 0 sdkconfig.ci |
The main directory contains hello_world_main.c, which is the program file. While you are inside the hello_world directory (not the main directory):
1 | C:\Espressif\frameworks\esp-idf-v5.2.1\examples\get-started\hello_world |
In order to compile, run this command:
1 | idf.py build |
It will check for errors and compile the thing. The required command to upload this to ESP32, need to run:
1 | idf.py -p PORT flash |
Which in my case is:
1 | idf.py -p COM8 flash |
You’ll get the indication that the upload is complete:

Now open any serial monitor software on your Windows PC. You can use Arduino IDE’s serial monitor if you have no other serial monitor software installed on your PC. You’ll get the informative output from the program.
To create a new project, navigate to the directory where you want to create your ESP32 project. Use the idf.py command-line tool to create a new project. For example:
1 | idf.py create-project my_esp32_project |
You can customize project settings such as board configuration, Wi-Fi credentials, and other parameters using the idf.py menuconfig command. Also, you can configure components, drivers, and features required for your ESP32 application. It will create a file named my_esp32_project.c under the main sub-directory with this content:
1 2 3 4 5 6 | #include <stdio.h> void app_main(void) { } |
Use a text editor to write your ESP32 application code in C or C++ or copy-paste some examples, like this one to blink the LED connected to Pin 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" #define PIN 2 void app_main(void) { gpio_set_direction(PIN, GPIO_MODE_OUTPUT); uint32_t isOn = 0; while (true) { isOn = !isOn; gpio_set_level(PIN, isOn); vTaskDelay(1000 / portTICK_PERIOD_MS); } } |
You already know how to compile and upload code to ESP32.
Now, you’ll install the drivers/libraries for your sensors such as DHT11. Here is the component registry:
1 | https://components.espressif.com/ |
Here is your driver for DHT11:
1 | https://components.espressif.com/components/zorxx/dht |
You can add dependency with this command:
1 | idf.py add-dependency "zorxx/dht" |
You’ll find the installed components and drivers here (example of my PC):
1 2 | C:\Espressif\frameworks\esp-idf-v5.2.1\components C:\Espressif\frameworks\esp-idf-v5.2.1\components\driver |
They have full documentation (and also component manager):
1 | https://docs.espressif.com/projects/idf-component-manager/en/latest/ |
This is the shortest and easiest possible guide to get started with the ESP-IDF Framework. I would suggest programming in this way instead of Arduino IDE for your serious projects, including home automation projects.