RobotLabs
  • Welcome
  • ESP32
    • PlatformIO
      • 1.1 Arduino Basics
      • 1.2 Install PlatformIO
      • 2.1 GPIO - LED Blink
      • 2.2 GPIO - LED Multi
      • 2.3 GPIO - LED Button
      • 3.1 PWM - LED Fade
      • 3.2 ADC - Analog Input
      • 4.1 IIC - OLED
    • MicroPython
    • ESP32 Projects
  • ROS
    • ROS2 Jazzy
      • 1.1 Install ROS 2 Jazzy
      • 1.2 Install VS Code
      • 1.3 Save ROS code on GitHub
      • 2.1 Create a Mobile Robot with URDF and Visualize it
      • 2.3 Create Launch Files to Display the URDF in RViz
      • 2.4 Simulation with Gazebo
      • 2.5 Gazebo sensors
      • 2.6 Sensor fusion and SLAM
    • MoveIt2
      • Moveit 2 joint position control with keyboard
      • Moveit2 joint velocity control for Panda robot arm
    • Perception
      • PCL-ROS
  • FOC
    • SimpleFOC
      • Install simpleFOC Lib
  • Template
    • Markdown
    • Interactive blocks
    • OpenAPI
    • Integrations
Powered by GitBook
On this page
  • Install VS Code and Python
  • Installing PlatformIO IDE Extension on VS Code
  • PlatformIO IDE Quick Overview
  • Create a New Project
  • platformio.ini file
  • src folder
  • Detect COM Port
  • Troubleshooting
  • Installing ESP32 Libraries on PlatformIO IDE
  • Install Library way 1
  • Install Library way 2
  • Summary
  1. ESP32
  2. PlatformIO

1.2 Install PlatformIO

Previous1.1 Arduino BasicsNext2.1 GPIO - LED Blink

Last updated 3 months ago

Install VS Code and Python

  1. Go to and download the stable build for your operating system.

  2. Go to and download Python 3.8.5 or a newest version.

IMPORTANT: Make sure you check the option Add VS Code and Python to PATH.

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)

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

After that, the PlatformIO icon should show up on the left sidebar as well as an Home icon that redirects you to PlatformIO home. Restart VS code for the changes to take effect.

At the bottom of the IDE, there’s a blue bar with PlatformIO commands. 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. You may need to click on the three dot icon at the top and enable PlatformIO tasks as shown below.

PlatformIO IDE Quick Overview

Create a New Project

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

Give your project a name (for example Blink_LED) and select the board you’re using. The Framework should be “Arduino” to use the Arduino core.

The default location is in this path Documents >PlatformIO >Projects. Finally, click “Finish”. The Blink_LED project should be accessible from the Explorer tab.

VS Code and PlatformIO have a folder structure that is different from the standard .ino project. If you click on the File 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.ini file. 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.

  • 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.

  • monitor_speed: set the baud rate. The default baud rate used by PlatformIO is 9600. However, it is possible to set up a different value.

In this file, you can also include the identifier of libraries you’ll use in your project using the lib_deps directive, save that file if you make any changes.

src 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.

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

After writing code, 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.

If the code is successfully uploaded, you should get the following message. Now, click on the Serial Monitor, and 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.

Troubleshooting

  1. If you try to upload a new sketch to your ESP32 and you get this error message “A fatal error occurred: Failed to connect to ESP32: Timed out… Connecting…“. It means that your ESP32 is not in flashing/uploading mode. Having the right board name and COM por selected, follow these steps:

    1. Hold-down the BOOT button in your ESP32 board

    2. Press the Upload button in the Arduino IDE to upload your sketch

    3. After you see the “Connecting….” message in your Arduino IDE, release the finger from the BOOT button

    4. After that, you should see the “Done uploading” message You’ll also have to repeat that button sequence every time you want to upload a new sketch.

  2. If you get the error “COM Port not found/not available”, you might need to install the CP210x Drivers

Installing ESP32 Libraries on 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. Click on the library you want to include in your project.

Install Library way 1

Click Add to Project. Select the project that you want to use the library.

This will add the library identifier using the lib_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.

Install Library way 2

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.

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.ini using 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.

Summary

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.

  • 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 boards, VS Code with the PlatformIO IDE extension is a great option.

https://code.visualstudio.com/
python.org/download
Check the option Add VS Code and Python to PATH during installation