Cheat Sheet Library

ESP-IDF Cheat Sheet

Espressif's official framework for ESP32 chips (C, FreeRTOS, CMake). Run the commands in an ESP-IDF terminal (Windows: the ESP-IDF CMD/PowerShell shortcut, or VS Code with the ESP-IDF extension). On Linux/macOS run . $HOME/esp/esp-idf/export.sh first.

  • 36 entries
  • 7 sections
  • Updated
  • Click a command to see all its options

36 entries

On this page

idf.py Commands #

CommandAction
New project
Choose the chip (esp32, esp32s2, esp32s3, esp32c3, esp32c6…)
Configuration menu (writes sdkconfig)
Build
Flash (/dev/ttyUSB0, /dev/ttyACM0 or /dev/cu.usbserial-*)
Serial monitor
Flash, then monitor (the usual loop)
Flash only the app (faster)
Wipe the whole flash
Delete the build folder
/ size-componentsMemory usage
Add a component from the registry
Save minimal config to sdkconfig.defaults
Installed IDF version

Monitor Keys #

KeyAction
Ctrl + ]Exit the monitor
Ctrl + T then Ctrl + HHelp
Ctrl + T then Ctrl + RReset the chip
Ctrl + T then Ctrl + FBuild and flash the project
Ctrl + T then Ctrl + ABuild and flash only the app
Ctrl + T then Ctrl + PPause the app (hold in reset)

Project Layout #

my_app/
├── CMakeLists.txt          # project file
├── sdkconfig               # generated by menuconfig
├── partitions.csv          # optional custom partitions
└── main/
    ├── CMakeLists.txt      # idf_component_register(SRCS "main.c" INCLUDE_DIRS ".")
    ├── idf_component.yml   # registry dependencies
    └── main.c              # app_main()

Code Snippets #

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "nvs_flash.h"

static const char *TAG = "app";
#define LED GPIO_NUM_2

static void blink_task(void *arg) {
    gpio_reset_pin(LED);
    gpio_set_direction(LED, GPIO_MODE_OUTPUT);
    while (1) {
        gpio_set_level(LED, 1);
        vTaskDelay(pdMS_TO_TICKS(500));
        gpio_set_level(LED, 0);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

void app_main(void) {
    ESP_ERROR_CHECK(nvs_flash_init());          // needed for Wi-Fi and storage
    ESP_LOGI(TAG, "Starting, free heap: %lu", esp_get_free_heap_size());
    xTaskCreate(blink_task, "blink", 2048, NULL, 5, NULL);
}

Logging #

MacroLevel
ESP_LOGE(TAG, ...)Error
ESP_LOGW(TAG, ...)Warning
ESP_LOGI(TAG, ...)Info
ESP_LOGD(TAG, ...) / ESP_LOGVDebug / verbose (enable in menuconfig)
esp_log_level_set("wifi", ESP_LOG_WARN)Change the level per tag at runtime

FreeRTOS Basics #

FunctionUse
xTaskCreate(fn, "name", stack, arg, prio, &handle)Start a task
xTaskCreatePinnedToCore(..., core)Start a task on core 0 / 1
vTaskDelay(pdMS_TO_TICKS(ms))Sleep without blocking other tasks
xQueueCreate / xQueueSend / xQueueReceivePass data between tasks
xSemaphoreCreateMutexProtect shared resources
vTaskDelete(NULL)End the current task

Troubleshooting #

ProblemFix
Port permission denied (Linux), then log out and back in
"Failed to connect"Hold BOOT, tap EN/RST, release BOOT
Stack overflow / Guru MeditationIncrease the task stack size; read the decoded backtrace in the monitor
Wrong chip errors (this resets the config)
Strange build errors after an update and build again