Run an Arduino Program on an ESP32 Microcontroller
This is a guide on how to run an Arduino program on an ESP32 microcontroller using the Arduino CLI on Linux (see the documentation for the Arduino CLI and ESP32).
Setup
The setup only needs to be completed once:
-
Install
arduino-clito~/local/binvia the install script, as described in the Arduino CLI documentation: -
Add
~/local/bintoPATHby adding the following to~/.bashrc:Then run the updated
~/.bashrcfile: -
Create a configuration file for
arduino-clilocated at~/.arduino15/arduino-cli.yamlcontaining the following:board_manager: additional_urls: - https://espressif.github.io/arduino-esp32/package_esp32_index.jsonFor more information, see the Arduino CLI documentation and the ESP32 documentation.
Alternatively, you can create the configuration file with the following
arduino-clicommand, as described in the Arduino CLI documentation:Then edit it to include the following, as before:
-
Install the ESP32 core for Arduino:
Alternatively, run the following to clone the ESP32 respository and install the required tools, as described in the ESP32 documentation:
-
Add the current user to the
dialoutgroup in order to read data from the serial connection:where
<username>is the username of the current user. -
Install
git(documentation) andpyserial(documentation) if not already installed:
Compile, Upload, and Monitor the Program
-
Connect the microcontroller to your computer.
-
Compile the program:
where
<program_directory>is the path to the directory containing the program file and<fqbn>is the FQBN (fully qualified board name) of the board. Note that the name of the program directory must match the name of the program file, excluding the.inoextension. For example, if the program directory ishello_world, the program file must behello_world.ino.To find the FQBN, run:
For a generic ESP32 microcontroller, the FQBN
esp32:esp32:esp32can often be used. -
Upload the binary:
where
<serial_port>is the serial port on which the ESP32 microcontroller is listening,<fqbn>is the FQBN of the board, and<program_directory>is the path to the directory containing the program file.The serial port is found by running:
The serial port will be listed under
Port. -
Monitor the serial output, as applicable:
where
<serial_port>is the serial port on which the ESP32 microcontroller is listening,<fqbn>is the FQBN of the board, and<baudrate>is the baud rate of the serial output (default is9600). Both the--fqbnand--configoptions are optional.