This method is recommended for advanced users. To use this method, you will need to have the ESP-IDF toolchain installed.
For a simplified method, see Installing using Boards Manager.
If you don't need any modifications in the default Arduino ESP32 core, we recommend you to install using the Boards Manager.
Arduino Lib Builder is the tool that integrates ESP-IDF into Arduino. It allows you to customize the default settings used by Espressif and try them in Arduino IDE.
For more information see Arduino lib builder
Note
Latest Arduino Core ESP32 version is now compatible with ESP-IDF v4.4. Please consider this compatibility when using Arduino as a component in ESP-IDF.
- Download and install ESP-IDF.
- For more information see Get Started.
- Create a blank ESP-IDF project (use sample_project from /examples/get-started) or choose one of the examples.
- In the project folder, create a new folder called
components
and clone this repository inside the newly created folder.
mkdir -p components && \
cd components && \
git clone https://github.com/espressif/arduino-esp32.git arduino && \
cd arduino && \
git submodule update --init --recursive && \
cd ../.. && \
idf.py menuconfig
Depending on one of the two following options, in the menuconfig set the appropriate settings.
Go to the section Arduino Configuration --->
- For usage of
app_main()
function - Turn offAutostart Arduino setup and loop on boot
- For usage of
setup()
andloop()
functions - Turn onAutostart Arduino setup and loop on boot
Experienced users can explore other options in the Arduino section.
After the setup you can save and exit:
- Save [S]
- Confirm default filename [Enter]
- Close confirmation window [Enter] or [Space] or [Esc]
- Quit [Q]
- In main folder rename file main.c to main.cpp.
- In main folder open file CMakeList.txt and change main.c to main.cpp as described below.
- Your main.cpp should be formatted like any other sketch.
//file: main.cpp
#include "Arduino.h"
void setup(){
Serial.begin(115200);
while(!Serial){
; // wait for serial port to connect
}
}
void loop(){
Serial.println("loop");
delay(1000);
}
In main.c or main.cpp you need to implement app_main()
and call initArduino();
in it.
Keep in mind that setup() and loop() will not be called in this case.
Furthermore the app_main()
is single execution as a normal function so if you need an infinite loop as in Arduino place it there.
//file: main.c or main.cpp
#include "Arduino.h"
extern "C" void app_main()
{
initArduino();
// Arduino-like setup()
Serial.begin(115200);
while(!Serial){
; // wait for serial port to connect
}
// Arduino-like loop()
while(true){
Serial.println("loop");
}
// WARNING: if program reaches end of function app_main() the MCU will restart.
}
- For both options use command
idf.py -p <your-board-serial-port> flash monitor
- The project will build, upload and open the serial monitor to your board
- Some boards require button combo press on the board: press-and-hold Boot button + press-and-release RST button, release Boot button
- After a successful flash, you may need to press the RST button again
- To terminate the serial monitor press [Ctrl] + [ ] ]
If you are writing code that does not require Arduino to compile and you want your ESP_LOGx macros to work in Arduino IDE, you can enable the compatibility by adding the following lines:
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
You might notice that Arduino-esp32's delay() function will only work in multiples of 10ms. That is because, by default, esp-idf handles task events 100 times per second. To fix that behavior, you need to set FreeRTOS tick rate to 1000Hz in make menuconfig -> Component config -> FreeRTOS -> Tick rate.
As commits are made to esp-idf and submodules, the codebases can develop incompatibilities that cause compilation errors. If you have problems compiling, follow the instructions in Issue #1142 to roll esp-idf back to a different version.