Arduino as ESPIF Component in PlatformIO - solution that works

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 :slight_smile:

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.

:upside_down_face: :slightly_smiling_face: 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:

  1. -Access ESPIDF examples for PlatformIO: GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO
  2. -Download to your PC and unpack it;
  3. -Using the PLATFORMIO command line, go to the Examples/espidf-arduino-blink/ folder;
  4. -Open a PlatformIO command line (Symbol “>” down in VSCode PlatformIO screen), go to espidf-arduino-blink folder and for compiling type: pio run
  5. -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****
  1. -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.
  2. -The src\CMakeLists.txt must be adjusted to contain only: idf_component_register(SRCS “Blink.cpp”)
  3. -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
  1. -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.
  2. -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.

  1. -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:


Some links that DIDN’T WORK FOR ME on PlatformIO(I tried these solutions, but they didn’t work on PlatformIO.):
-https://www.youtube.com/watch?v=kioRPsvfyzs
-https://www.youtube.com/watch?v=hHzGX-K6lmo
-https://www.youtube.com/watch?v=RbWNnWIFjyA


Good luck :wink: :slight_smile: 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 :wink:

Sorry for not putting images/prints, I’m kind of out of time.

2 Likes

Thank you! Your comment definitely helped me to get the actual solution, but I have to say it was a little bit confusing and had some redundant parts.

So I’ll summarize the very few steps crucial needed to run Arduino as a component in ESP-IDF from PlatformIO

  1. Open PlatformIO, and create new project, select espidf framework and your board name
  2. In your project folder open platformio.ini file and just add arduino to framework, like this:
[env:esp-wrover-kit]  
platform = espressif32
board = esp-wrover-kit
framework = arduino, espidf  ; Most important line to edit
monitor_raw = yes            ; This just fixes colors on terminal when using log
monitor_speed = 115200
  1. Create a file called sdkconfig.defaults in your project folder and put the following inside:
CONFIG_AUTOSTART_ARDUINO=y
# CONFIG_WS2812_LED_ENABLE is not set
CONFIG_FREERTOS_HZ=1000
CONFIG_MBEDTLS_PSK_MODES=y
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y

This is very important as not having this default sdkconfig file, compiling will give errors where freertos hz is not 1000 and other garbage.

  1. Now do a clean build (just in case) and that’s it no need to do anything else :smiling_face:

PS. As you can see in sdkconfig.defaults CONFIG_AUTOSTART_ARDUINO is set to Yes, this basically just forces you to start your program with the usual arduino void setup() and void loop() functions, if you instead want to program it the default way with app_main() then open command line in PlatformIO and type

pio run -t menuconfig

Scroll to the section Arduino Configuration —> and change CONFIG_AUTOSTART_ARDUINO [*] accordingly.

1 Like