In this post
An AI coding assistant writes ESP32 firmware fast, and it is as confident about its wrong calls as its right ones. You make it useful with the workflow embedded work needs anyway: a quick build, flash and monitor loop, prompts that pin the framework and its version, and a review that expects mistakes.
Set up the loop first
ESP-IDF is Espressif's framework for the ESP32 family. You install it with the ESP-IDF Installation Manager, which pulls the compiler toolchain plus CMake and Ninja, and then you work from the command line or through the VS Code extension (ESP-IDF get started guide). Start from the hello_world example in the framework's examples directory and learn four commands (ESP-IDF guide, start a project on the command line):
idf.py set-target esp32
idf.py menuconfig
idf.py -p PORT flash
idf.py -p PORT monitor
The first command selects the chip and clears stale build state, menuconfig edits the project configuration, flash builds and programs the board, and monitor attaches to the serial output. Ctrl+] exits the monitor, and idf.py -p PORT flash monitor chains the last two steps.
This loop catches most of what an assistant gets wrong. Arduino-only calls do not compile, wrong pin macros fail the build, and a program that crashes on boot says so in the monitor. Without the loop, assistant output is worth little. With it, the model can iterate against real feedback, and each round costs you a rebuild.
What the assistant is good at
GitHub's guidance for Copilot splits the tools by task, and the split holds for any assistant: inline completion for finishing a line or a repetitive block, and chat for generating a larger section and iterating on it. Its prompt advice carries over too. Break complex tasks into smaller ones, state requirements specifically, and show examples of inputs and expected outputs (GitHub Copilot best practices).
For firmware, an assistant reliably helps with:
- Scaffolding: the main file, CMakeLists, component dependencies, Kconfig entries.
- Boilerplate: peripheral init sequences, FreeRTOS task creation, protocol and packet handling.
- Error decoding: paste a build error or a monitor traceback and get the likely cause in plain language.
- Tests: host-side unit tests for the parsing and state machine code that never touches hardware.
The same guidance is blunt about the limit: validate everything the tool suggests, with automated tests and tooling where you can. On a microcontroller that tooling is the build, the monitor, and eventually a meter.
A first task that exercises the loop
Blink an LED, then sleep for ten seconds. Most battery firmware has this shape, and it takes about twenty lines.
#include "driver/gpio.h"
#include "esp_sleep.h"
#define LED_PIN GPIO_NUM_2
void app_main(void)
{
gpio_config_t io = {
.pin_bit_mask = 1ULL << LED_PIN,
.mode = GPIO_MODE_OUTPUT,
};
gpio_config(&io);
for (int i = 0; i < 3; i++) {
gpio_set_level(LED_PIN, 1);
vTaskDelay(pdMS_TO_TICKS(200));
gpio_set_level(LED_PIN, 0);
vTaskDelay(pdMS_TO_TICKS(200));
}
esp_sleep_enable_timer_wakeup(10ULL * 1000000ULL);
esp_deep_sleep_start();
}
gpio_config sets a pin's mode, pulls and interrupt behavior in one call and overwrites the pin's current configuration, so call it early in setup (ESP-IDF GPIO driver). esp_sleep_enable_timer_wakeup takes a duration in microseconds, and esp_deep_sleep_start powers down the CPUs, most RAM and the digital peripherals; the application restarts on wake (ESP-IDF sleep modes documentation). vTaskDelay and pdMS_TO_TICKS come from FreeRTOS, the system ESP-IDF builds on.
Assistants write short, documented, mechanical code like this well, and this is also where most of their errors turn up.
Where it will be confidently wrong
Framework mixing. The most common failure is Arduino code in an ESP-IDF project. pinMode and digitalWrite do not exist in ESP-IDF. They belong to the Arduino core for ESP32, a different framework, and the build stops with undeclared identifiers. The model has read code for both and does not always keep them apart, so name the framework in every prompt.
Pin traps. On the ESP32, GPIO34 to GPIO39 are input only and have no software pull-ups. GPIO6 to GPIO11, and GPIO16 and GPIO17 on modules that use them for flash or PSRAM, are not free for your circuit. ADC2 channels stop working while Wi-Fi is active, and some pins are strapping pins that change boot behavior (ESP-IDF GPIO driver). If a model puts your sensor on GPIO36 and your LED on GPIO9, the code compiles and cannot work.
Version drift. ESP-IDF changes across major versions. In current releases the GPIO driver lives in the esp_driver_gpio component behind the driver/gpio.h header, and tutorials written for older versions show different structures and names (ESP-IDF GPIO driver). A snippet the model remembers from an old forum thread may call an API your version has renamed. Pin the version, and when a call looks unfamiliar, look it up in the documentation for that version.
Sleep fine print. Before entering a sleep mode, the application must disable Wi-Fi and Bluetooth, and connections are not kept across sleep even then (ESP-IDF sleep modes documentation). The flash on your module keeps drawing current while the chip sleeps, so a battery-life estimate that ignores it will be wrong. When a model skips these details, the firmware it hands you draws more current than it looks like it should.
Pinning the prompt
Every prompt should name the framework and version ("ESP-IDF v6 for ESP32, not Arduino"), the board or module, and the pins you intend to use. Two more rules keep the answers checkable:
- Ask for the header file each call comes from, then spot-check two or three of them in the documentation for your version.
- Ask for the smallest example that compiles, and add features in follow-up turns instead of requesting the whole application at once. That is the Copilot guide's advice to break tasks down, applied to firmware.
Review like a compiler, then like an engineer
The build and the monitor catch everything the compiler can see. Three things they cannot:
- Whether the API usage matches the current documentation for your version. Look up unfamiliar calls before you accept them.
- Whether the code matches the board. If the monitor prints garbage right after flashing, the guide's first suspect is a 26 MHz crystal on a board configured for the 40 MHz default, fixed with the crystal setting in menuconfig (ESP-IDF guide).
- What the firmware costs in current. A sleep-current claim needs a measurement on your own hardware with a meter.
The assistant can't do any of those, since it doesn't have your board, your board revision or your multimeter. Let the model draft and explain, let the toolchain decide what compiles, and check on the bench whatever only the bench can show.
Sources
- Get started with ESP-IDF on ESP32
- Start a project on Linux and macOS from the command line, ESP-IDF
- ESP32 GPIO and RTC GPIO, ESP-IDF documentation
- ESP32 sleep modes, ESP-IDF documentation
- Best practices for using GitHub Copilot
Written with AI from the linked sources.