2. ESP32 Development with Visual Studio Code
2. ESP32 Development with Visual Studio Code

Introduction #

In the last tutorial we installed the ESP-IDF Tools and everything needed for ESP32 development on Windows. If you haven’t already been through these step then go back before continuing on with this tutorial.

In this tutorial we will be installing Visual Studio Code on Windows along with the ESP-IDF extension so that we can develop and program our code using a nice IDE rather than through the command line. For this tutorial you’ll need:

Install Visual Studio Code #

Let’s start by installing Microsoft Visual Studio Code, download it from here:

Visual Studio Code Download

Once downloaded follow the instructions in the installer until complete; we can then open up Visual Studio Code and install the ESP-IDF extension.

Click on the Settings icon in the bottom right and click Extensions:

VS Code Extensions

From there we need to search for Espressif:

ESP-IDF Extension

Click on Espressif IDF and Install:

Once done, we can close Visual Studio Code.

Building an Example Project #

The easiest way to use Visual Studio Code for Espressif development is to launch it from the ESP-IDF command prompt. So let’s start by opening this:

ESP-IDF Command Prompt

And type:

code
Visual Studio Code Launch

Once Visual Studio Code opens, click File > Open Folder:

Visual Studio Code - Open Folder

Navigate to C:\esp-idf\examples\get-started\hello_world and click Select Folder.

Project Configuration #

Once opened, click View > Command Palette and type Edit Configurations (UI):

Visual Studio Code - Edit Configurations (UI)

We now need to alter the compiler path to use our ESP32-S2 GCC compiler:

C:\esp-idf-tools\tools\xtensa-esp32s2-elf\esp-2020r3-8.4.0\xtensa-esp32s2-elf\bin\xtensa-esp32s2-elf-gcc.exe

Note that if you’re not using the ESP32-S2, but the ESP32, your compiler path should be set to:

C:\esp-idf-tools\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe
Visual Studio Code - C/C++ Configuration - Compiler Path

Your compiler version may differ from the examples presented here (esp-2020r3-8.4.0), so you’ll need to check the directories on your system to ensure you set the correct compiler path.

We also need to set the IntelliSense mode to:

windows-gcc-arm
Visual Studio Code - C/C++ Configuration - IntelliSense Mode

and finally we need to set our include paths to:

${workspaceFolder}/**
${config:idf.espIdfPath}/components/**
Visual Studio Code - C/C++ Configuration - Include Path

We can now close the C/C++ Configurations tab. We don’t strictly need to do any of this as our project will compile without any issue if we didn’t, however if we skip this step, Visual Studio Code will continually show us false errors that make finding real errors in our code much harder.

Open hello_world_main.c and once again go to View > Command Palette and type Set Espressif device target:

Visual Studio Code - Set Espressif Device target

then select the either ESP32 or ESP32-S2 depending on the device you’re using:

Visual Studio Code - Set Espressif Device target

Build #

We’re now ready to build the example project, so click the build button in the bottom left of the window:

Visual Studio Code - Build

or open the Command Palette and type Build your project:

Visual Studio Code - Build

Flash #

Once the build has completed we can flash the example project to our device. Open the Command Palette and type Select port to use:

Visual Studio Code - Select port with ESP32 device

and then select the COM port that your ESP32 device is attached to, we can then flash our example project to the selected device. Either click on the flash button in the bottom left of the window:

Visual Studio Code - Flash

and then select UART when prompted. Alternatively, open the Command Palette and type Flash (UART) your project:

Visual Studio Code - Flash

Monitor #

Once our example project has been flashed to the device, let’s open the serial terminal to see the results. We can either click on the monitor button in the bottom left of the window:

Visual Studio Code - Monitor

or open the Command Palette and type Monitor your device:

Visual Studio Code - Monitor

This will then open a serial terminal from within Visual Studio Code and show you the output of our example project:

Visual Studio Code - Monitor Output

Conclusion #

In this tutorial we setup Visual Studio Code as an IDE for developing Espressif projects and we looked at how to compile and flash them. If you had any issues getting your board up and running then get in touch and we’ll help out.