Run a Program on an ESP32 Microcontroller
This is a guide on how to run a program on an ESP32 microcontroller using ESP-IDF on Linux (Debian 13). For more information, see the Getting Started documentation, the IDF Frontend documentation, and the Build System documentation.
Install ESP-IDF and Required Tools
These steps only need to be completed once:
-
Install the dependencies required by ESP-IDF:
-
Install ESP-IDF:
mkdir ~/esp cd ~/esp git clone -b <stable_release> --recursive https://github.com/espressif/esp-idf.gitwhere
<stable_release>is a stable release of ESP-IDF, which is noted in the ESP-IDF documentation. -
Install the required tools for all ESP32 targets:
To install the required tools for individual targets, see the ESP-IDF documentation.
Setup Environment and Configure Project
-
Connect the microcontroller to your computer.
-
Export the required environment variables:
-
Ensure that the program file is located in a subdirectory of the project directory named
main. For example, if the program file ishello_world.cand the project directory is~/hello_world, the program file must have as its path~/hello_world/main/hello_world.c. -
Ensure that the required
CMakeLists.txtfile exists in the project directory. The file must contain the following (see the documentation):cmake_minimum_required(VERSION 3.5) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(<project_directory>)where
<project_directory>is the name of the project directory (e.g.hello_world). -
Ensure that the required
CMakeLists.txtfile exists in themainsubdirectory. The file must contain the following (see the documentation):where
<version>is the minimum CMake version number (for example,VERSION 3.5) and<program_file>is the name of the program file (for example,hello_world.c).The project structure will look like this:
Note that there are two different
CMakeLists.txtfiles, one in the project directory and the other in themaindirectory, each with different requirements. -
Within the project directory, set the target and run the project configuration utility:
where
<project_directory>is the name of the project directory and<target>is the hardware target. To obtain a list of all possible targets, run:Note that the
--style monochromeoption and argument pair above are optional, used to change the default whitemenuconfigdisplay to black.In the
menuconfigsettings, choose the desired settings. Then presssto save,EscorEnterif necessary to remove the save confirmation dialogue, andqto quit.
Build, Flash, and Monitor the Program
-
Build the project within the project directory:
where
<project_directory>is the name of the project directory. -
Flash the binaries onto the device:
where
<port>is the port on which the device is connected and<baud_rate>is the baud rate. Specifying the port and the baud rate is optional. By default, ESP-IDF will use a USB port and a baud rate of460800, per the documentation. The port on which the device is connected can be found by running:For example, the port may be
/dev/ttyUSB0. -
Monitor the serial output, if applicable:
where
<port>is the port on which the device is connected.Exit the monitor by pressing
Ctrl + ].