Skip to content

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:

  1. Install arduino-cli to ~/local/bin via the install script, as described in the Arduino CLI documentation:

    curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=~/local/bin sh
    
  2. Add ~/local/bin to PATH by adding the following to ~/.bashrc:

    PATH=$PATH:~/local/bin
    

    Then run the updated ~/.bashrc file:

    source ~/.bashrc
    
  3. Create a configuration file for arduino-cli located at ~/.arduino15/arduino-cli.yaml containing the following:

    board_manager:
      additional_urls:
        - https://espressif.github.io/arduino-esp32/package_esp32_index.json
    

    For more information, see the Arduino CLI documentation and the ESP32 documentation.

    Alternatively, you can create the configuration file with the following arduino-cli command, as described in the Arduino CLI documentation:

    arduino-cli config init
    

    Then edit it to include the following, as before:

    board_manager:
      additional_urls:
        - https://espressif.github.io/arduino-esp32/package_esp32_index.json
    
  4. Install the ESP32 core for Arduino:

    arduino-cli core install esp32:esp32
    

    Alternatively, run the following to clone the ESP32 respository and install the required tools, as described in the ESP32 documentation:

    mkdir -p ~/Arduino/hardware/espressif
    cd ~/Arduino/hardware/espressif
    git clone https://github.com/espressif/arduino-esp32.git esp32
    cd esp32/tools
    python3 get.py
    
  5. Add the current user to the dialout group in order to read data from the serial connection:

    sudo adduser <username> dialout
    

    where <username> is the username of the current user.

  6. Install git (documentation) and pyserial (documentation) if not already installed:

    sudo apt install git
    sudo apt install python3-serial
    

Compile, Upload, and Monitor the Program

  1. Connect the microcontroller to your computer.

  2. Compile the program:

    arduino-cli compile --fqbn <fqbn> <program_directory>
    

    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 .ino extension. For example, if the program directory is hello_world, the program file must be hello_world.ino.

    To find the FQBN, run:

    arduino-cli board listall
    

    For a generic ESP32 microcontroller, the FQBN esp32:esp32:esp32 can often be used.

  3. Upload the binary:

    arduino-cli upload -p <serial_port> --fqbn <fqbn> <program_directory>
    

    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:

    arduino-cli board list
    

    The serial port will be listed under Port.

  4. Monitor the serial output, as applicable:

    arduino-cli monitor -p <serial_port> --fqbn <fqbn> --config <baudrate>
    

    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 is 9600). Both the --fqbn and --config options are optional.