Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266

Learn how to program the ESP32 and ESP8266 NodeMCU boards using VS Code (Microsoft Visual Studio Code) with PlatformIO IDE extension. We cover how to install the software on Windows, Mac OS X or Ubuntu operating systems.

Getting Started with VS Code and PlatformIO IDE for ESP32 and NodeMCU ESP8266 boards: Windows, Mac OS X, Linux Ubuntuhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Getting-Started-Visual-Studio-VS-Code-PlatformIO-IDE-ESP32-ESP8266-NodeMCU-Windows-Mac-OS-X-Linux-Ubuntu.jpg?resize=300%2C169&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Getting-Started-Visual-Studio-VS-Code-PlatformIO-IDE-ESP32-ESP8266-NodeMCU-Windows-Mac-OS-X-Linux-Ubuntu.jpg?resize=1024%2C576&quality=100&strip=all&ssl=1 1024w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Getting-Started-Visual-Studio-VS-Code-PlatformIO-IDE-ESP32-ESP8266-NodeMCU-Windows-Mac-OS-X-Linux-Ubuntu.jpg?resize=768%2C432&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="eager" data-reader-unique-id="4" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

The Arduino IDE works great for small applications. However, for advanced projects with more than 200 lines of code, multiple files, and other advanced features like auto completion and error checking, VS Code with the PlatformIO IDE extension is the best alternative.

In this tutorial, we’ll cover the following topics:

  • Installing VS Code (Visual Studio Code):
    • A) Windows
    • B) Mac OS X
    • C) Linux Ubuntu
  • Installing PlatformIO IDE Extension on VS Code
  • Visual Studio Quick Interface Overview
  • PlatformIO IDE Overview
  • Uploading Code using PlatformIO IDE: ESP32/ESP8266
  • Changing the Serial Monitor Baud Rate – PlatformIO IDE
  • Installing Libraries on PlatformIO IDE

A) Installing VS Code on Windows (Visual Studio Code)

Go to https://code.visualstudio.com/ and download the stable build for your operating system (Windows).

Microsoft Visual Studio Code VS Code Download Page for Windows PChttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/1-Download-VS-Studio-Code.jpg?resize=300%2C202&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/1-Download-VS-Studio-Code.jpg?resize=768%2C518&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="eager" data-reader-unique-id="33" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Click on the installation wizard to start the installation and follow all the steps to complete the installation. Accept the agreement and press the Next button.

Microsoft Visual Studio Code VS Code Installation wizardhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/2-Install-VS-Code.png?resize=300%2C233&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 598px) 100vw, 598px" loading="eager" data-reader-unique-id="38" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Select the following options and click Next.

Microsoft Visual Studio Code VS Code Installation wizard step 2https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/3-Install-VS-Code.png?resize=300%2C233&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 598px) 100vw, 598px" loading="eager" data-reader-unique-id="43" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Press the Install button.

Microsoft Visual Studio Code VS Code Installation wizard step 3https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/4-Install-VS-Code.png?resize=300%2C233&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 598px) 100vw, 598px" loading="lazy" data-reader-unique-id="48" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Finally, click Finish to finish the installation.

Microsoft Visual Studio Code VS Code Installation wizard final stephttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/5-Install-VS-Code.png?resize=300%2C233&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 598px) 100vw, 598px" loading="lazy" data-reader-unique-id="53" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.

Microsoft Visual Studio Code VS Code Installation wizard welcome screen on a Windows PChttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/VS-Code-Welcome-Screen.png?resize=300%2C224&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/VS-Code-Welcome-Screen.png?resize=1024%2C763&quality=100&strip=all&ssl=1 1024w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/VS-Code-Welcome-Screen.png?resize=768%2C572&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="57" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

That’s it. Visual Studio Code was successfully installed.

Installing Python on Windows

To program the ESP32 and ESP8266 boards with PlatformIO IDE you need Python 3.5 or higher installed in your computer. We’re using Python 3.8.5.

Go to python.org/download and download Python 3.8.5 or a newest version.

Open the downloaded file to start the Python installation wizard.

The following window shows up.

Installing Python 3.8.5 on a Windows PC and Add to Pathhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Install-Python-Add-to-path.png?resize=300%2C185&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 666px) 100vw, 666px" loading="lazy" data-reader-unique-id="67" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

IMPORTANT: Make sure you check the option Add Python 3.8 to PATH. Then, you can click on the Install Now button.

When the installation is successful you’ll get the following message.

Python Installation successful on Windows PChttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Python-Installation-successful.png?resize=300%2C185&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 666px) 100vw, 666px" loading="lazy" data-reader-unique-id="74" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

You can click the Close button.

Now, go to this section to install PlatformIO IDE extension.


B) Installing VS Code on Mac OS X (Visual Studio Code)

Go to https://code.visualstudio.com/ and download the stable build for your operating system (Mac OS X).

Microsoft Visual Studio Code VS Code Download Page for Mac OS Xhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/3-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-download-Mac-OS-X.png?resize=300%2C152&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/3-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-download-Mac-OS-X.png?resize=1024%2C519&quality=100&strip=all&ssl=1 1024w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/3-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-download-Mac-OS-X.png?resize=768%2C390&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="85" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

After downloading the Visual Studio Code application file, you’ll be prompted with the following message. Press the “Open” button.

Microsoft Visual Studio Code VS Code Downloading application file for Mac OS Xhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/5-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-save-file.png?resize=300%2C97&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 487px) 100vw, 487px" loading="lazy" data-reader-unique-id="90" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Or open your Downloads folder and open Visual Studio Code.

After that, you’ll be greeted by a Welcome tab with the released notes of the newest version.

Microsoft Visual Studio Code VS Code Installation wizard welcome screen on Max OS Xhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/6-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-installed.png?resize=300%2C225&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/6-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-installed.png?resize=768%2C575&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="98" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

That’s it. Visual Studio Code was successfully installed.

Installing Python on Mac OS X

To program the ESP32 and ESP8266 boards with PlatformIO IDE you need Python 3.5 or higher installed in your computer. We’re using Python 3.8.5.

To install Python I’ll be using Homebrew. If you don’t have the brew command available, type the next command:

Microsoft Visual Studio Code VS Code installing homebrewhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/1-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-install-brew.png?resize=300%2C38&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/1-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-install-brew.png?resize=768%2C98&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="107" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Then, run the brew command to install Python 3.X:

$ brew install python3
Microsoft Visual Studio Code VS Code installing Python 3 with brew commandhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/2-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-install-Python3.png?resize=300%2C100&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 632px) 100vw, 632px" loading="lazy" data-reader-unique-id="113" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Now, go to this section to install PlatformIO IDE extension.


C) Installing VS Code on Linux Ubuntu (Visual Studio Code)

Go to https://code.visualstudio.com/ and download the stable build for your operating system (Linux Ubuntu).

Microsoft Visual Studio Code VS Code Download Page for Linux Ubuntuhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/3-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-downloading-VS-Code-Linux-Ubuntu.png?resize=300%2C137&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/3-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-downloading-VS-Code-Linux-Ubuntu.png?resize=768%2C350&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="122" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Save the installation file:

Microsoft Visual Studio Code VS Code Save installation file for Linux Ubuntuhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/4-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-downloading-VS-Code-Linux-Ubuntu.png?resize=300%2C212&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 495px) 100vw, 495px" loading="lazy" data-reader-unique-id="126" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

To install it, open a Terminal windows, navigate to your Downloads folder and run the following command to install VS Code.

$ cd Downloads
~/Downloads $ sudo apt install ./code_1.49.1-1600299189_amd64.deb

When the installation is finished, VS Code should be available in your applications menu.

Microsoft Visual Studio Code VS Code Open application Linux Ubuntuhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/5-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-run-VS-Code-Linux-Ubuntu.png?resize=300%2C190&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 516px) 100vw, 516px" loading="lazy" data-reader-unique-id="133" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.

Microsoft Visual Studio Code VS Code Installation wizard welcome screen on Linux Ubuntuhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/6-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-downloading-VS-Code-Linux-Ubuntu-Testing.png?resize=300%2C185&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/6-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-downloading-VS-Code-Linux-Ubuntu-Testing.png?resize=768%2C474&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="137" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

That’s it. Visual Studio Code was successfully installed.

Installing Python on Linux Ubuntu

To program the ESP32 and ESP8266 boards with PlatformIO IDE you need Python 3.5 or higher installed in your computer. We’re using Python 3.8.

Open the Terminal window and check that you already have Python 3 installed.

$ python3 --version
python 3.8.2
Linux ubuntu installing Python3 testing versionhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/1-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-Python3-installed.png?resize=300%2C111&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 389px) 100vw, 389px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="146" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

As you can see in the preceding figure, Python 3.8.2 is already installed. 

If you don’t have Python 3.8.X installed, run the next command to install it:

$ sudo apt install python3

Whether you already have Python installed or not, you need to run the following command to install Python utilities.

$ sudo apt install python3-distutils
Installing python3-distutils Linux Ubuntu for PlatformIO IDEhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/2-Getting-Started-VS-Code-PlatformIO-IDE-ESP32-ESP8266-Python3-installed-distutils.png?resize=300%2C60&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 452px) 100vw, 452px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="156" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Now, go to this section to install PlatformIO IDE extension.


Installing PlatformIO IDE Extension on VS Code

It is possible to program the ESP32 and ESP8266 boards using VS Code with the PlatformIO IDE extension. Follow the next steps to install the PlatformIO IDE extension.

Open VS Code:

  1. Click on the Extensions icon or press Ctrl+Shift+X to open the Extensions tab
  2. Search for “PlatformIO IDE
  3. Select the first option 
  4. Finally, click the Install button (Note: the installation may take a few minutes)
Install PlatformIO IDE Extension on VS Codehttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Install-Platformio-Extension-VS-Code.png?resize=300%2C166&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Install-Platformio-Extension-VS-Code.png?resize=768%2C424&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="179" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

After installing, make sure that PlatformIO IDE extension is enabled as shown below.

PlatformIO IDE Extension Enabled on VS Codehttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Platformioextension-enabled.png?resize=300%2C89&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Platformioextension-enabled.png?resize=768%2C229&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="183" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

After that, the PlatformIO icon should show up on the left sidebar as well as an Home icon that redirects you to PlatformIO home.

PlatformIO Extension Installed Successfullyhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-extension-installed-successfully.png?resize=300%2C193&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-extension-installed-successfully.png?resize=768%2C495&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="188" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

That’s it, PlatformIO IDE extension was successfully added to VS Code.

If you don’t see the PIO icon and the quick tools at the bottom, you may need to restart VS code for the changes to take effect.

Either way, we recommend restarting VS Code before proceeding.

VS Code Quick Interface Overview

Open VS Code. The following print screen shows the meaning of each icon on the left sidebar and its shortcuts:

VS Code Visual Studio Application Interface Overviewhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/VS-Code-Interface-Overview.png?resize=300%2C166&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/VS-Code-Interface-Overview.png?resize=768%2C424&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="197" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
  • File explorer
  • Search across files
  • Source code management (using gist)
  • Launch and debug your code
  • Manage extensions

Additionally, you can press Ctrl+Shift+P or go to View > Command Palette…to show all the available commands. If you’re searching for a command and you don’t know where it is or its shortcut, you just need to go to the Command Palette and search for it.

At the bottom, there’s a blue bar with PlatformIO commands.

Here’s the what icon does from left to right:

  • PlatformIO Home
  • Build/Compile
  • Upload
  • Clean
  • Serial Monitor
  • New Terminal

If you hover your mouse over the icons, it will show what each icon does.

Alternatively, you can also click on the PIO icon to see all the PlatformIO tasks. 

PlatformIO IDE extension Tasks menuhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Tasks.png?resize=300%2C183&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Tasks.png?resize=768%2C470&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 820px) 100vw, 820px" loading="lazy" data-reader-unique-id="226" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

If the tasks don’t show up on your IDE when you click the icon, you may need to click on the three dot icon at the top and enable PlatformIO tasks as shown below.

Enable PlatformIO IDE extension taskshttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Enable-PlatformIO-Tasks.png?resize=300%2C184&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Enable-PlatformIO-Tasks.png?resize=768%2C471&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="230" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

PlatformIO IDE Overview

For you to get an overview on how PlatformIO works on VS code, we’ll show you how to create, save and upload a “Blinking LED” sketch to your ESP32 or ESP8266 board.

Create a New Project

On VS Code, click on the PlartfomIO Home icon. Click on + New Project to start a new project.

Create New Project PlatformIO VS Codehttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Create-New-Project.png?resize=300%2C129&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Create-New-Project.png?resize=768%2C330&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="239" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Give your project a name (for example Blink_LED) and select the board you’re using. In our case, we’re using the DOIT ESP32 DEVKIT V1. The Framework should be “Arduino” to use the Arduino core. 

You can choose the default location to save your project or a custom location.

The default location is in this path Documents >PlatformIO >Projects. For this test, you can use the default location. Finally, click “Finish”.

PlatformIO with VS Code Create New ESP32 Projecthttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Create-New-Project-ESP32.png?resize=300%2C236&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 598px) 100vw, 598px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="252" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

For this example, we’ll be using the DOIT ESP32 DEVKIT board. If you are using an ESP8266 NodeMCU board the process is very similar, you just need to select your ESP8266 board:

PlatformIO IDE extension with VS Code Create New ESP8266 Projecthttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Create-New-Project-ESP8266.png?resize=300%2C235&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 602px) 100vw, 602px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="258" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

The Blink_LED project should be accessible from the Explorer tab.

PlatformIO IDE Extension Project Folder Structurehttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatfomIO-project-created-folder-structure.png?resize=300%2C123&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatfomIO-project-created-folder-structure.png?resize=768%2C314&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="262" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

VS Code and PlatformIO have a folder structure that is different from the standard .ino project. If you click on the Explorer tab, you’ll see all the files it created under your project folder. It may seem a lot of files to work with. But, don’t worry, usually you’ll just need to deal with one or two of those files.

Warning: you shouldn’t delete, modify or move the folders and the platformio.inifile. Otherwise, you will no longer be able to compile your project using PlatformIO.

platformio.ini file

The platformio.ini file is the PlatformIO Configuration File for your project. It shows the platform, board, and framework for your project. You can also add other configurations like libraries to be included, upload options, changing the Serial Monitor baud rate and other configurations.

PlatformIO IDE extension Configuration File for ESP32https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Config-init-file-ESP32.png?resize=300%2C121&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Config-init-file-ESP32.png?resize=768%2C310&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 820px) 100vw, 820px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="273" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
  • platform: which corresponds to the SoC used by the board.
  • board: the development board you’re using.
  • framework: the software environment that will run the project code.

With the ESP32 and ESP8266, if you want to use a baud rate of 115200 in your Serial Monitor, you just need to add the following line to your platformio.ini file.

monitor_speed = 115200

After that, make sure you save the changes made to the file by pressing Ctrl+S.

In this file, you can also include the identifier of libraries you’ll use in your project using the lib_deps directive, as we’ll see later.

src folder

The src folder is your working folder. Under the src folder, there’s a main.cpp file. That’s where you write your code. Click on that file. The structure of an Arduino program should open with the setup() and loop() functions.

PlatformIO IDE extension main.cpp file under src folderhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-scr-folder-main-file.png?resize=300%2C103&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-scr-folder-main-file.png?resize=768%2C265&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="301" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

In PlatformIO, all your Arduino sketches should start with the #include <Arduino.h>.

Uploading Code using PlatformIO IDE: ESP32/ESP8266

Copy the following code to your main.cpp file. 



#include <Arduino.h>

#define LED 2

void setup() {
  
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
}

void loop() {
  
  digitalWrite(LED, HIGH);
  Serial.println("LED is on");
  delay(1000);
  digitalWrite(LED, LOW);
  Serial.println("LED is off");
  delay(1000);
}

View raw code

This code blinks the on-board LED every second. It works with the ESP32 and ESP8266 boards (both have the on-board LED connected to GPIO 2). 

We recommend that you copy this code manually, so that you see the autocompletion and other interesting features of the IDE in action. Additionally, if you have a syntax error somewhere in your program, it will underline it in red even before compiling.

After that, press Ctrl+S or go to File Save to save the file.

Now, you can click on the Upload icon to compile and upload the code. Alternatively, you can go to the PIO Project Tasks menu and select Upload.

Upload Code ESP32 board PlatformIO VS Code Visual Studiohttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Upload-Code-ESP32-PlatformIO-VS-Code.png?resize=300%2C137&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Upload-Code-ESP32-PlatformIO-VS-Code.png?resize=768%2C352&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="387" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

If the code is successfully uploaded, you should get the following message.

Upload Code to ESP32 PlatformIO VS Code Successhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Upload-Code-ESP32-PlatformIO-VS-Code-Success.png?resize=300%2C215&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Upload-Code-ESP32-PlatformIO-VS-Code-Success.png?resize=768%2C552&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="391" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

After uploading the code, the ESP32 or ESP8266 should be blinking its on-board LED every second.

Now, click on the Serial Monitor icon and you should see it printing the current LED state.

PlatformIO IDE VS Code Visual Studio Serial Monitor ESP32 boardhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Platformio-VS-Code-Serial-Monitor-ESP32.png?resize=300%2C155&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/Platformio-VS-Code-Serial-Monitor-ESP32.png?resize=768%2C397&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="399" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Note: if you don’t see the Terminal window, go to the menu Terminal > New Terminal.

Detect COM Port

PlatformIO will automatically detect the port your board is connected to. To check the connected devices you can go to the PIO Home and click the Devices icon.

PlatformIO IDE extension VS Code Visual Studio Connected Devices COM Port Serialhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-VS-Code-Devices-COM-port.png?resize=300%2C199&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-VS-Code-Devices-COM-port.png?resize=768%2C510&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="408" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Troubleshooting

If when trying to upload code you get the following error: “Failed to connect to ESP32: Timed out waiting for packet header” it usually means that your board is not in flashing mode when you’re uploading the code. 

When this happens you need to press the ESP32 on-board BOOT button when you start seeing a lot of dots in the debugging window.

If you don’t want to have to press the BOOT button every time you upload new code, you can follow this guide: [SOLVED] Failed to connect to ESP32: Timed out waiting for packet header.

Changing the Serial Monitor Baud Rate – PlatformIO IDE

The default baud rate used by PlatformIO is 9600. However, it is possible to set up a different value as mentioned previously. On the File Explorer, under your project folder, open the platformio.ini file and add the following line:

monitor_speed = baud_rate

For example:

monitor_speed = 115200
PlatformIO IDE extesion Change Serial Monitor Baud Rate 115200https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-change-serial-monitor-baud-rate.png?resize=300%2C117&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-change-serial-monitor-baud-rate.png?resize=768%2C299&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="428" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

After that, save that file.

Installing ESP32/ESP8266 Libraries on PlatformIO IDE

Follow the next procedure if you need to install libraries in PlatformIO IDE.

Click the Home icon to go to PlatformIO Home. Click on the Libraries icon on the left side bar.

Search for the library you want to install. For example Adafruit_BME280.

PlatformIO IDE extension VS Code Visual Studio Search for BME280 Libraryhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-VS-Code-Search-BME280-library.png?resize=300%2C199&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-VS-Code-Search-BME280-library.png?resize=768%2C510&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="439" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Click on the library you want to include in your project. Then, click Add to Project.

PlatformIO with VS Code Add Library to Projecthttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-add-library-to-project.png?resize=300%2C98&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 745px) 100vw, 745px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="444" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Select the project were you want to use the library.

PlatformIO with VS Code Add Library to Project Choose Projecthttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-VS-Code-Add-Library-to-Project-Choose-Project.png?resize=300%2C179&quality=100&strip=all&ssl=1 300w" sizes="(max-width: 701px) 100vw, 701px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="448" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

This will add the library identifier using the lid_deps directive on the platformio.ini file. If you open your project’s platformio.ini file, it should look as shown in the following image.

Add Library Identifier to Configuration File PlatformIOhttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PIO-Configuration-File-Add-Library.png?resize=300%2C140&quality=100&strip=all&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PIO-Configuration-File-Add-Library.png?resize=768%2C357&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="455" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Alternatively, on the library window, if you select the Installation tab and scroll a bit, you’ll see the identifier for the library. You can choose any of those identifiers depending on the options you want to use. The library identifiers are highlighted in red.

PlatformIO IDE Extension Library Registry menuhttps://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Library-registry.png?resize=300%2C234&quality=100&strip=all&ssl=1 300w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-Library-registry.png?resize=768%2C599&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="460" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Then, go to the platformio.ini file of your project and paste the library identifier into that file, like this:

lib_deps = adafruit/Adafruit BME280 Library@^2.1.0

If you need multiple libraries, you can separate their name by a coma or put them on different lines. For example:

lib_deps =
  arduino-libraries/Arduino_JSON @ 0.1.0
  adafruit/Adafruit BME280 Library @ ^2.1.0
  adafruit/Adafruit Unified Sensor @ ^1.1.4

PlatformIO has a built-in powerful Library Manager, that allows you to specify custom dependencies per project in the Project Configuration File platformio.iniusing lib_deps. This will tell PlatformIO to automatically download the library and all its dependencies when you save the configuration file or when you compile your project.

Open a Project Folder

To open an existing project folder on PlatformIO, open VS Code, go to PlatformIO Home and click on Open Project. Navigate through the files and select your project folder.

VS Code with PlatformIO Open Project Folderhttps://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-open-project-folder.png?resize=300%2C152&quality=100&strip=all&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/PlatformIO-open-project-folder.png?resize=768%2C388&quality=100&strip=all&ssl=1 768w" sizes="(max-width: 828px) 100vw, 828px" loading="lazy" data-reader-unique-id="476" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

PlatformIO will open all the files within the project folder.

VS Code Color Themes

VS Code lets you choose between different color themes. Go to the Manageicon and select Color Theme. You can then select from several different light and dark themes.

VS Code Visual Studio Change Color Themes Dark Lighthttps://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/09/VS-Code-Change-Color-Theme.png?resize=222%2C300&quality=100&strip=all&ssl=1 222w" sizes="(max-width: 318px) 100vw, 318px" data-recalc-dims="1" loading="lazy" data-reader-unique-id="484" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

Shortcuts’ List

For a complete list of VS Code shortcuts for Windows, Mac OS X or Linux, check the next link: 

Wrapping Up

In this tutorial you’ve learned how to install and prepare Visual Studio Code to work with the ESP32 and ESP8266 boards. VS Code with the PlatformIO IDE extension is a great alternative to the classical Arduino IDE, especially when you’re working on more advanced sketches for larger applications.

Here’s some of the advantages of using VS Code with PlatformIO IDE over Arduino IDE: 

  • It detects the COM port your board is connected to automatically; 
  • VS Code IntelliSense: Auto-Complete. IntelliSense code completion tries to guess what you want to write, displaying the different possibilities and provides insight into the parameters that a function may expect;
  • Error Highlights: VS Code + PIO underlines errors in your code before compiling;
  • Multiple open tabs: you can have several code tabs open at once;
  • You can hide certain parts of the code;
  • Advanced code navigation;
  • And much more…

If you’re looking for a more advanced IDE to write your applications for the ESP32 and ESP8266 boards, VS Code with the PlatformIO IDE extension is a great option.

We hope you’ve found this tutorial useful. If you like ESP32 and ESP8266, check the following resources: