HOW to use Arduino as an ESP-IDF component within PlatformIO
I decided to post the solution I found here, as I spent a lot of time with this problem, and I hope to help others who have the same difficulty as me
I tried to do this also using the ESP-IDF extension for VSCode, but it didn’t work for me ;-( , but using Platformio It worked well, and the best thing is not needing to download the Arduino as a component, as it takes up a few gigabytes for each project that uses Arduino as an ESP-IDF component.
Sometimes it may be desirable to use the Arduino within the ESP-IDF. This brings advantages such as:
- Be able to use or change ESP-IDF libraries. Because they are pre-compiled for ARDUINO, not allowing changes;
- Be able to debug an Arduino program, and whenever the debugger pointer goes to an ESP-IDF library, you will not see Assembler codes, but rather the native C/C++ code of the libraries, being able to even make changes or adaptations to the libraries , find and fix bugs or identify why some of your devices connected to the ESP32 (sensors, cameras, etc…) are not working as they should. Here is the link that shows how to configure the ESP32S3 JTAG interface on PLATFORMIO that allows you to debug the program made on the ESP32-S3: How to use JTAG built-in debugger of the ESP32-S3 in PLATFORMIO and here how to use changing variable values in PLATFORMIO (very useful): Platformio with ESP32 Arduino: How to Change Variable values in DEBUG mode
**It is also possible to debug the standard ESP32 using a JTAG interface called ESP-Prog. It’s very cheap, available on Aliexpress for US$18.00. There are some tutorials on the internet, but the way to configure the ESP-Prog is similar to the ESP32S3.
Well, let’s get to the point, HOW to use Arduino as an ESP-IDF component within PlatformIO:
- -Access ESPIDF examples for PlatformIO: GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO
- -Download to your PC and unpack it;
- -Using the PLATFORMIO command line, go to the Examples/espidf-arduino-blink/ folder;
- -Open a PlatformIO command line (Symbol “>” down in VSCode PlatformIO screen), go to espidf-arduino-blink folder and for compiling type: pio run
- -However, if you want to compile for another type of ESP32, such as ESP32-S3, change platformio.ini as in the following example:
; ****Platformio.ini**** [env:esp32-s3-devkitc-1] platform = espressif32 framework = arduino, espidf ;Here's the "ace in the hole" for using Arduino as an ESPIDF component within PlatformIO build_flags = -D CONFIG_BLINK_GPIO=2 ;variable that adjusts the pin number of the Blink LED (SET to 2 in most ESP32 kits). -D erases previous settings. monitor_speed = 115200 board = esp32-s3-devkitc-1 ;Example using ESP32S3 with 8MB Flash memory upload_speed = 2000000 ;(optional) upload_protocol = esptool board_build.f_cpu = 240000000L ;(optional) board_build.f_flash = 80000000L ;If it is 8MB of Flash memory (optional) board_build.arduino.memory_type = qio_opi ;(optional) ;build_flags = -DCORE_DEBUG_LEVEL=0 ; -DBOARD_HAS_PSRAM ;lib_deps = ;lovyan03/LovyanGFX@^1.1.7 ;esp32-camera ;espressif/esp32-camera@^2.0.4 ;lib_deps = esp32-s3-devkitc-1\esp32-camera ;lib_deps = espressif/esp32-camera@^2.0. 4 ;debug_tool = esp-builtin ;I haven't tested it yet. But it is not necessary for Debugging. If you use ESP-Prog, set this here. monitor_port = COM9 ;Serial communication port on the ESP32-S3 board (optional) upload_port = COM15 ;JTAG port of the ESP32-S3 board (optional) build_type = debug ; ****END Platformio.ini****
- -Remember: The example file (inside "scr" folder) name is “Blink.cpp”, not “main.cpp”. If you use “main.cpp” it will not compile and will give an error.
- -The src\CMakeLists.txt must be adjusted to contain only: idf_component_register(SRCS “Blink.cpp”)
- -The file that the compiler automatically creates after the first compilation is “sdkconfig.env_name_in_platformio.ini”. For example “sdkconfig.esp32-s3-devkitc-1”. For a compillling success with Arduino libraries, this file must have 2 lines edited as follows(look for these lines and edit them as follows):
CONFIG_FREERTOS_HZ=1000 # default is CONFIG_FREERTOS_HZ=100 CONFIG_AUTOSTART_ARDUINO=y # default is CONFIG_AUTOSTART_ARDUINO is not set
- -After changing the sdkcondig and “platformio.ini” file, it is necessary to wait some time for platformio to reconfigure. Sometimes it is even important to restart platformio after this.
- -Still at the PLATFORMIO command prompt, upload to your ESP32-S3 board using the command: pio run -e esp32-s3-devkitc-1 -t upload
ps: It is also possible to “build” and “upload” using the standard PlatformIO shortcuts (“check symbol” only builds and “->” symbol build and upload to ESP32). These symbols are next to a “trashcan symbol” at the bottom of the screen.
- -IMPORTANT: For Arduino working as an ESP-IDF component in platformio, it is important to maintain the basic program format configured in “Blink.cpp”. The file name can however be changed, as long as you change \src\CMakeLists.txt. The name can be with the extension .cpp
Some useful links:
- -esp-idf-arduino-blink example: https://github.com/platformio/platform-espressif32/tree/develop/examples/espidf-arduino-blink
- -ESPIDF examples for PlatformIO: GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO
- -PLATFORMIO commands (How to build & upload from commandline): https://techoverflow.net/2022/07/23/how-to-run-platformio-build-upload-from-the-command-line/
- -how the “platformio.ini” file works and configuration examples: “platformio.ini” (Project Configuration File) — PlatformIO latest documentation
Some links that DIDN’T WORK FOR ME on PlatformIO(I tried these solutions, but they didn’t work on PlatformIO.):
Good luck and I hope it helped you to use Arduino as an ESP-IDF component. It was very difficult for me to arrive at this solution. Although it is available as an example, I couldn’t easily find an explanation of it in online forums.
Now I’m happy to be able to debug my Arduino code and see what’s inside the libraries and no longer fall into compiled assembler code
Sorry for not putting images/prints, I’m kind of out of time.