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.
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):
- 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).
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.
Select the following options and click Next.
Press the Install button.
Finally, click Finish to finish the installation.
Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.
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.
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.
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).
After downloading the Visual Studio Code application file, you’ll be prompted with the following message. Press the “Open” button.
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.
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:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Then, run the brew command to install Python 3.X:
$ brew install python3
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).
Save the installation file:
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.
Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.
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
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
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:
- Click on the Extensions icon or press Ctrl+Shift+X to open the Extensions tab
- Search for “PlatformIO IDE”
- Select the first option
- 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.
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:
- 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.
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.
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.
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”.
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:
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 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.
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.
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.
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/vs-code-platformio-ide-esp32-esp8266-arduino/
*********/
#include <Arduino.h>
#define LED 2
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, HIGH);
Serial.println("LED is on");
delay(1000);
digitalWrite(LED, LOW);
Serial.println("LED is off");
delay(1000);
}
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.
If the code is successfully uploaded, you should get the following message.
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.
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.
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:
- Hold-down the BOOT button in your ESP32 board
- Press the Upload button in the Arduino IDE to upload your sketch
- After you see the “Connecting….” message in your Arduino IDE, release the finger from the BOOT button
- 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. But if you want to solve this issue once for all without the need to press the BOOT button, follow the suggestions in the next guide:
2) If you get the error “COM Port not found/not available”, you might need to install the CP210x Drivers:
- Install USB Drivers – CP210x USB to UART Bridge (Windows PC)
- Install USB Drivers – CP210x USB to UART Bridge (Mac OS X)
If you experience any problems or issues with your ESP32, take a look at our in-depth ESP32 Troubleshooting Guide.
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
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.
Click on the library you want to include in your project. Then, click Add to Project.
Select the project were 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.
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.
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.
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 Manage icon and select Color Theme. You can then select from several different light and dark themes.
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:
How about asm?
Hi.
What are you referring to?
Can you debug the code under Microsoft Visual Studio Code?
Hi.
Yes. Learn more here: https://docs.platformio.org/en/latest/plus/debugging.html
Regards,
Sara
Great!
Troubleshooting, error syntax and useless code ! You have an resolve with Alien code ?
What do you mean?
Besides the link Sara mentions here are some other references:
A good video from Andreas Spiess demonstrating hardware debug in platformio:
youtube.com/watch?v=psMqilqlrRQ
Another good reference and video:
hackster.io/brian-lough/use-the-platformio-debugger-on-the-esp32-using-an-esp-prog-f633b6
Most guides show only Windows installation. To get the JTAG interface to work in Linux you need to install the 99-platformio-udev.rules so the USB ingterface recognizes the device as explained in:
docs.platformio.org/en/latest/plus/debug-tools/esp-prog.html
For MacOS, you apparently need to remove the default FTDI drivers (same reference above).
I recommend the ESP-PROG made by Espressif. Besides the usual Chinese suppliers, it is available from US mainstream distributors for $12-13 without shipping.
Hi.
Thank for sharing this 🙂
Great tutorial! Do you also have a tutorial on how to use esp idf in Platformio?
Hi.
Unfortunately, we don’t have any tutorial about that subject.
Regards,
Sara
Is there any experience, what is the speed difference on program run/code size VS coded vs. C coded? Is it possible to handle interrupts in VS?
pretty complex to setup microsoft visual studio and platformIO
What is the big advantage of using with VS Code and PlatformIO IDE over the Arduino-IDE that would be worth the hassle of this setup-process?
The wrapping up statement gives you the main benefits for your efforts, i will add that your programming skills will be taken to a higher level. I wish i had this when i installed it on my pc. This ia a great tutorial to get you started on what could be a career changing journey for some.
Stefan:
I was wondering the same thing! What is the advantage of using VS Code and PlatformIO! With the Arduino platform, I know, at time, there is issue. I have learned to deal with it. I don’t have any project that the Arduino platform doesn’t work.
Hi.
It is very useful if you’re setting up a web server with separated HTML, CSS and Javascript files. You can program all the files using the same software and you can open the files side by side. It also allows you to upload the files to the ESP32 or ESP8266 filesystem easily.
Additionally, if you’re working on a quite complex project, it can be useful because it highlights errors, helps with autocompletion, identation, etc.
Regards,
Sara
Thanks Brian and Sara,
I did see the benefit. I will try VS and PlantformIO. Hopefully, it does not break the Arduino IDE.
Thank you for the recommendation. I would love to get it running immediately
Great!
Try it on! 😀
Excelent Guide to start!
Can not wait for a book with some full projects to fully understand it maybe multi platform and some debugging tutorials?
Thanks for a great writeup! It was nice to have instructions for Windows, MacOS, and Linux.
I’ve always thought the code editing and management of VSCode was so much better than the Arduino. I used the old (not VSCode) Platformio before for ESP and STM, but as Stefan mentions, it’s a bunch of overhead. As you mention, for larger projects VSCode is way better.
The best reason to use platformio is to get interactive hardware debugging inside VSCode. That is a game-changer. It would be great to add another tutorial to the series on how to use hardware debug on Windows/Mac/Linux. I have an EDP-Prog (<$13) but I haven’t had time to play with it.
I mean’t ESP-Prog
Thanks for your comment.
We are definitely planning to add some more tutorials about this subject.
Regards,
Sara
Thank you for introducing here this new tool!
It showed very useful here when I used it to create a project using ESP32 FreeRTOS (ESP-IDF).
I already used the VS Code when I was studying FreeRTOS here, but without using the PlatformIO resources. Now it’s much better to use it!
Hi Sara,
wonderfull tutorial up to the Blink_LED tutorial. All is running ok and I thank you very much for your patience and precision.
I’ve a simple question : can I copy all Arduino ESP32 projects now running with Arduino compilation in PlatformIO without any changes ( I mean instruction and declaration define etc. ) and uploading it with PLatformIO on ESP32 board ? Or it’s a long way of conversion and test ?
Regards and thanks again.
Hi.
You can easily do that.
In PlatformIO Home, select the Option “Import Arduino Project”.
Tick the option “Use libraries installed by Arduino IDE”.
And that’s it.
Regards,
Sara
Thanks a lot. Very kind of you to answer to my question.
You’re welcome.
Regards,
Sara
Hi Sara, it seems to me very close to ESPhome platform used for Home Assistance. Am I wrong? Thanks again.
Hi,
Is it required to press the boot switch of the esp32 while downloading?
I am asking so as I got:
“*** [upload] Error 1
=========================================== [FAILED] Took 3.74 seconds ===========================================
The terminal process “platformio ‘run’, ‘–target’, ‘upload'” terminated with exit code: 1.”
Or I am missing something?
Zvika
Hi.
Yes, you may need to press the boot button while uploading.
Regards,
Sara
I was really waiting for this tutorial! THANKS! hope to see more about programming with VS Code.
Great!
I’m glad you liked it 😀
Regards,
Sara
I am mostly a hobbyist / tinkerer. I’ve played around with VSCode / Platformio a bit, alongside the Arduino IDE.
My take on it is that VSCode / Platformio is extremely convenient for those who are writing complex code that involves (for example) writing new libraries alongside the actual Arduino or ESP code, or working with files of different kinds all next to each other (as in the article summary). And the syntax error underlining and completion tools are helpful.
But I suspect that VSCode / Platformio is overkill for those who are comfortable with the Arduino IDE, are writing simple code for Arduino or ESP, and are largely following “recipes” online using pre-baked libraries.
VSCode / Platformio has a learning curve in terms of figuring out how folders are organized, where your code is, what the windows do, how to properly update libraries and board configuration files, etc. Hobbyists may find it’s not worth climbing that learning curve.
A short take on it would be that VSCode / Platformio is more “programmer” focused while the Arduino IDE is more “hobbyist” focused.
I myself bounce back and forth between the two — quite often switch back to the Arduino IDE when I find myself frustrated with a basic problem (often board compatibility) that I don’t know how to fix in VSCode / Platformio.
Hi Jordan.
That’s very true.
It may be overwhelming for beginners. For simpler and smaller programs Arduino IDE works just fine.
Thanks for sharing your experience.
Regards,
Sara
Have tried one of Your projects from book ordered from You. Works good in Windows, but
in Linux i have problem not directly pertaining Your lesson but with Arduino IDE has problem with serial. I’m not talking about connecting board to computer’s USB, that works fine. It’s rather whether You call for “Serial.print” in code or not, when compiling it gives error: No module named ‘serial’. I understand that ESP32 needs python to work with IDE
(or that I’m just assuming, correct me if I’m wrong). ESP8266 works fine. I have Python 3 installed ans also installed pyserial(with pip3 comm.) But no matter I tried it gives error again.
Hi.
If you’re one of our students, I suggest that you place your question on the RNTLAB forum: https://rntlab.com/forum/
That way, we can better follow your issue and ask you questions to better understand what’s going on.
Regards,
Sara
This is probably not a correct answer but you typed “Serial.print” works and “serial” does not. Might the problem be that we need to use a Capital S in Serial? I’ve made that mistake many times in Java and assume Python and Arduino are case sensitive too.
Thank you for this informative tutorial!
Another HUGE benefit from using PIO is the portability!
When using Arduino IDE, copying a project to another computer involves adding new boards or libraries using the board and library manager (often quite a hassle to satisfy all dependencies).
By using the lib_deps statement in the platformio.ini PIO just downloads missing libraries!
That’s true!
That’s another advantage of VS Code with PIO.
Regards,
Sara
Hi Sara,
Thanks for providing details and steps for that.
I am ESP8266 and ESP32 developer and I have total 2 to 2.5 years experience for same. Till now we are using ESP32 IDF behing our own SDK and developed almost 10 to 12 products.
I just want to know that VS will support for ESP32 IDF or just for Arduino?
Hi.
It also supports IDF.
Regards,
Sara
I am newbie for ESP32 and keen to know in detail how to interface it with AM2301 on vscode with platformio to monitor the values both on serial and web,(without arduino)
Hi Sara
Thanks for this tutorial !!
after som mistakes it works !!
Great!
I have just started using VS Studio with PlatformIO. How do I “update” external libraries specified in “lib_deps” to the latest version?
In the Arduino IDE, I just click on “Manage Libs” and it updates all my specified external libraries.
Hi.
If you want to use the latest version of the library, you can search for the library in the Libraries tab, select the installation tab and there should be several options for the lib_deps. Usually there’s something indicating the library version. So, you just need to copy the name of the latest version.
Regards,
Sara
I already followed the example in Windiws 10 without any problem. Only have a question: Why is required to load phyton?
REgards
I believe that vscode is a Python program. So to run vscode you must have Python loaded onto the computer being used. Similar to eclipse being a Java program that requires Java to run.
Can we do arduino assembly on VScode?
“Follow the next procedure if you need to install libraries in PlatformIO IDE.”
Do you need to do this if you already have a lot of installed libraries under the Arduino IDE?
Okay, yes. The libraries I’ve accumulated are within the Arduino file structure, not the PIO structure.
I don’t think I want to import many of my Arduino projects into PIO. Converting .ino files and backtracking its libraries is a little bit of a pain. Going forward, on new projects, I will start using PIO instead of the arduino IDE, and I’ll give it a shot. But, the LED_blink example is a big help, and you guys have made implementing PIO pretty easy.
Put me down as a PlatformIO convert. Intellisense, variable and code hinting for the most part are a big help. “Attaching” a board to a project (by way of pasting two or three lines of formatted text into a project’s platform.ini file) is a better board management practice, as are attaching libraries that may change over time.
You can import older sketches into PIO, you just have to make a few changes, like adding arduino include and changing the .ino file to .cpp.
Hi Donald.
That’s great!
I’m glad you liked using PlatformIO. It’s way easier to write code and manage libraries.
Once you get used to it, you won’t want to go back 🙂
Regards,
Sara
I received this error after I installed the BME280 library according to the tutorial, then uploaded:
“In file included from .pio\libdeps\esp32dev\Adafruit BME280 Library\Adafruit_BME280.cpp:31:0:
.pio\libdeps\esp32dev\Adafruit BME280 Library\Adafruit_BME280.h:27:17: fatal error: SPI.h: No such file or directory”
What is missing?
Do you have #include <Arduino.h> at the very top of the sketch?
Yes, it is the first line of the main.cpp sketch.
I have the same issue with that library.
Hi.
If you’re not using the libraries in your sketch, just remove them from the plaformio.ini file.
Regards,
Sara
After purchasing the “building Webservers” book, I installed the platform io extension into VSCODE which I’d had installed for a while. It seemed to work, but I didn’t have time to play with it. Today I opened vscode, received and update, and the PlatformIO home page doesn’t appear nor do the buttons on the status bar. I can’t click on “New Project” because it isn’t there, and no “Home” button on the status bar. I’ve uninstalled PIO, and reinstalled, I uninstalled VScode and reinstalled, nothing seems to work. I’d like to complete this course, but I’m dead in the water right now.
Any suggestions??
Hi John, I don’t know exactly what happened there, but did you check wether the PlatformIO plugin is enabled?
Go to the Extensions menu and check if the platformio extension is enabled. Sometimes it is installed, but is disabled.
Well, I don’t have any better clues for this.
There is an activateOnlyOnPlatformIOProject setting that disables PlatformIO on non PlatformIO projects. I don’t know if that is your problem, but I use the feature so all the platformio stuff doesn’t get included in my python, etc. projects. It was off when I installed originally, but maybe the default changed.
You can check your settings (gear in lower right), Extensions->Platformio to see if the activateOnlyOnPlatformIOProject is set.
https://dokk.org/documentation/platformio/v3.6.1/ide/vscode/#platformio-ide-activateonlyonplatformioproject
I don’t remember now how to create a new project if the UI isn’t activated, but maybe it needs an empty (or blank line) platformio.ini. If you have a platformio.ini, in your workspace directory, then the PIO extension should activate.
Thank you, yes I’ve checked all the settings, even turn that one on and off to no avail. But thanks for the reply.
Hello,
Do you have any keys or suggestions that help with importing Arduino files to Platformio? I have tried and there are some things that are asked by Platformio upon import that I do not understand?
Thanks.
DB
Hi.
What things are asked?
Can you be a little more specific?
Regards,
Sara
Hello Sara,
Thank you for your time with an enterprise such as yours I imagine that you are very busy spreading your knowledge of physical computing and networking science.
My issue was with importing Arduino IDE created files into Platformio. I have many many Arduino files and libraries and I was concerned with loosing or changing them to conform to Platformio and not being able to recover them.
Libraries are quite differently handled in Platformio, but since my first question to you I have solved that riddle.
I think that I answered my own questions. For now. I am gratefull to you for getting me started with Platformio.
Thanks
Dave
Great!
I’ll try to create more VS Code tutorials, so that it is easier to handle your projects.
Regards,
Sara
Hello Sara,
I am manually entering the post “Build an ESP8266 Web Server – Code and Schematics (NodeMCU)” in VS Code and PlatformIO, but I have a problem.
I enter:
const char* ssid = “XXXXXXXXXXXX”;
const char* password = “HHHHHHHH”;
and at the moment of saving it it becomes:
const char *ssid = “XXXXXXXXXXXX”;
const char *password = “HHHHHHHHH”;
Is there any way to solve it? If not, you will have to do everything with the Arduino IDE
Thank you for your time,
Carles
Hi Carles.
I’m sorry but I didn’t understand your issue.
Arduino IDE sketches do work on PlatformIO.
Regards,
Sara
Hi Sara,
I write “const char“, a space and “ssid”. When I save it it becomes “const char”, a space and “ssid”. The asterisk has passed from the end of the word “char” to the beginning of “ssid”. This is the problem.
Regards,
Carles
Sara,
I have taken out the quotes because the asterisks were lost when I answered. In the initial post you can check it.
I write const char*, a space and ssid. When I save it it becomes const char, a space and *ssid. The asterisk has passed from the end of the word char to the beginning of ssid.
Regards,
Carles
The VSCode editor has a formatting feature to reformat code to a consistent notation, “prettier”, and seems that it’s enabled. Google “How to disable prettier in VSCode”. Click the gear icon in the lower left corner and you’ll get the settings then look for prett and format. (OR just look at text editor settings.) You can also disable the extension.
There is a setting to format on save, which seems to be what is happening to you.
It could be a different extension than prettier, so you might need to check which extensions are enabled.
Hi Carl,
I have uninstalled all the extensions except C/C++ and PlatformIO. Continue changing the asterisk.
I use it in MacOS Mojave 10.14.6, Spanish version. Is there anyone who has the same configuration and it doesn’t happen the same?
Thanks
Thanks for the clear write-up.
I noticed many asking why VSCode over arduino….
As an old grey-beard I feel compelled to point out that one of the true great features of VSCode is the git integration. Having built-in source control is truely a blessing. being aware of and using source control is one of the things that separates professional developers from hobby coders.
For those unfamiliar with git or source control, git is the premier means of managing source code as it evolves; and it can save your bacon and it removes the fear of breaking your program when you want to experiment or are just learning a new platform. It truely is magic. I urge everyone to learn it, use it, live it. It will make your development life much easier. The third icon down (the branch one) is the gateway.
Happy Trails..
followed your instruction step-by-step (thank you) I got the message concerning
#include <Arduino.h>
“The include file not found in browse.path”
any idea what to change, what to add?
Thank you!
hi, my esp32 board was working fine. I was working in arduino. I followed your instructions for PlatformIO and when I uploaded my board got a fatal error.
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
*** [upload] Error 2
What can I do to fix this error and upload to my board?
I’m on Ubuntu 20.04.2 LTS.
Thank you
Hi.
Press the ESP32 on-board BOOT button when you start seeing a lot of dots on the debugging window.
Regards,
Sara
Hi Sara,
I am pushing the BOOT button repeatedly but I keep getting the same error…
I also used the capacitor technique I saw on the site’s instructions, but it did not work either…
When I did a search about this error, I found this: MD5 of file does not match data in flash!
What do you think is happening and how do you think it can be fixed ?
Thank you !
Will
Hi.
I’m not sure what is happening.
Do you have any peripherals connected to your board?
If you have, you should disconnect them before uploading the code.
Regards,
Sara
I am working with VSCode although I miss how to configure the partitions like the Arduino. I am using an ESP32 and would like to choose Minimal SPIFFS (1.9MB APP with OTA / 190KB SPIFFS) for OTA updates. Thanks, great job greetings.
Hi.
I think this is what you must be looking for: https://docs.platformio.org/en/latest/platforms/espressif32.html#partition-tables
Regards,
Sara
That’s it!! well thank you very much. All the best.
Hi! Thanks for the tutorial. I was able to follow this and get the LED flashing, but I’m a bit confused as to why Python was needed, seeing as the code is written in C++. I found this article because I was hoping to program an ESP32 with MicroPython via VSCode. Is there a way to do this?
Hi.
Yes, there is an extension to write MicroPython code in VS Code.
However, there isn’t an “interface” with buttons to upload the code and so on. You’ll have to do it using the Terminal.
Regards,
Sara
Thanks for your reply 🙂 So why is Python necessary for this tutorial?
By the way, I found RT-Thread which is a MicroPython extension for VSCode, works really well and has full UI
Hi.
Here’s the reason: https://docs.platformio.org/en/latest/faq.html?highlight=python#install-python-interpreter
I didn’t know about RT-Thread. I have to take a look.
Regards,
Sara
Hi Sara:
can you tell me about this error:
____could not open port ‘COM5’: could not open port ‘COM5’: PermissionError(13, ‘Accès refusé.’, None, 5)_____
Thak you
Hi.
That means that VS Code can’t establish a serial connection with your board.
That may be because you have the board connected on another program like Arduino IDE. Close Arduino IDE.
It may also be the case that VS Code can’t find the right COM port.
Check that it is connecting to the right port. If it isn’t, you can manually set a COM port on the platformio.ini file. Add the following to the platformio.ini file:
upload_port = COMX
Replace X with the number of the COM port.
I hope this helps.
Regards,
Sara
Ciao
come posso modificare la libreria dei display Nextion per usarla con PIO?
vedi tutorial rnd per usare nextion e esp32
Hi
Is ota programming supported in PlatformIO?
BR Jesper
Yes.
Hi
I use a TTGO T-Journal board, but I can not find this board in the list of PIO boards.
Do you know what board I should use?
Hi.
You can select ESP32 Wrover Module.
We have a guide for that board: https://randomnerdtutorials.com/ttgo-t-journal-esp32-camera-getting-started/
Regards,
Sara
Hi Sara
Yes I know I can select this module with the Arduino IDE, but in VS code with platformio plugin I can not select this board.
It is not in the boards list.
Any other suggestions?
Hi again.
Try the Espressif ESP32 Dev Module.
Tell me if it works.
Regards,
Sara
Hi There,
How to build a project for a board using the Arduino framework that has the ESP32-S2 like the Saola-1 from Espressif?
TIA
Jorge
Hi.
Take a look at this discussion: community.platformio.org/t/esp32-s2-esp32s2-saola-support/13467/8
Regards,
Sara
Hi
Interesting exercise this VScode, which I was introduced to in your Build Web Servers book, but rather a steep learning curve. One thing that puzzles me is that running an esp8266 program on the Arduino IDE I get the following results on memory usage:
Sketch uses 441937 bytes (46%) of program storage space. Maximum is 958448 bytes.
Global variables use 31384 bytes (38%) of dynamic memory, leaving 50536 bytes for local variables. Maximum is 81920 bytes.
Yet when I run it on VScode I get:
RAM: [===== ] 52.4% (used 42956 bytes from 81920 bytes)
Flash: [======== ] 84.7% (used 645001 bytes from 761840 bytes)
So do I conclude the VScode compiler is less efficient on memory usage? To be honest I not too sure if the two measurements are referring to the same thing although Global Variables seems to correspond to RAM at 81920 bytes
Hi.
To be honest, I’m not sure why that happens.
Maybe they are using different versions of the ESP8266 boards’ installation??
Regards,
Sara
Could be Sara.
Further test are that the compiled bin file is 446kB for the Arduino IDE and 649kB for the VS code. The source file is identical in each case and will OTA update from the Arduino IDE version but either fails to complete or completes with HTTP error in the VScode case.
I will try a few board variations but in each test it was for a sonoff S20
Will let you know if I nail it – otherwise I am sticking with the Arduino IDE
I really wanted to go Linux but no matter which distro I tried, I don’t seem to get it right with VS CODE. Every time there is an error concerning the pins_arduino.h . I have even given up on using Linux though I could quickly install if someone showed me how to properly install VS CODE. I have followed this tutorial to the letter but still nothing doing.
I’ve tried everything I can think of, but PlatformIO is not able to get an input com port.
When it failed to discover automatically I tried –
upload_port = /dev/ttyUSB*
upload_port = Com[1]
upload_port = Com[3]
All failed, I am using the “esp32doit-devkit-v1”, Amazon description is “DOIT DEVIT V1 ESP32-WROOM-32 Development Board ESP32 ESP-32S WiFi+Bluetooth Dev Module CP2102 for Arduino”
I’m using a Windows 10 operation system.
When I plug the board in the LED lights up solid. I do not see anything in the drive letters, or devices that indicate an ESP32 is connected. I don’t know it I should, this is all new to me.
Any help would be greatly appreciated.
Thanks.
Hi.
Were you able to program your ESP board with Arduino IDE before?
Maybe you don’t have the ESP32 USB drivers installed?
Most ESP32 use the CP2102 USB drivers. See bullet number 6 of our troubleshooting guide: https://randomnerdtutorials.com/esp32-troubleshooting-guide/
Regards,
Sara
Thanks Sara, I’m not sure if it was missing drivers or a bad cable. I manually installed the drivers first, but still was having problems but wiggled cable and the
ESP32 powered up. I uploaded Blink_LED again, SUCESS!! Throwing away cable now!!
Great!
I’m glad you found the issue.
Regards,
Sara
Hi,
Having purchased your Build Web Servers with ESP32 & ESP8266, and had great fun and learnt a great deal from the tutorials.
I have started a new project to use the ESP8266 based on the knowledge gained from the tutorials.
I would like to add a function that gets called in the main.cpp but I do not want the function to be within the main.cpp as the program is growing fast and I would like to have this called function as a separate file.
The problem I have is how to include a function so that the main.cpp calls this function, and how to be able to declare variables in this function that it will be declared in the main.cpp (so the function will use the data in the variables and then save the modified data into another variable name but to be used by the main.cpp or later by another function), and in which directory do I save the function or functions too?
Thankiny you in advance
Victor
Hi!
When I remove all serial/print statements the LED is not blinking at all on my ESP-WROOM-32.
Have you ever tried if the onboard LED is really pinned to GPIO2? From the schematics I do not see an onboard LED either, maybe I got an old /cheap one from Aliexpress even it is black?
How to deal with variables(globals too) and function calls over different .h or .cpp files?
I want to use my code splitted up like the tabs functionality allows it in the Arduino IDE.
Could you show me how to do it?
Thx & Best, thorsten
Hi.
At the moment, I don’t have any tutorial about that subject.
Regards,
Sara
Hi Thorsten, usually, when you declare a global variable in an .h/.hpp file it will be available in your main script. You just need to create your .h/.hpp file and save it in the same folder where your main.cpp file is located. Then, you need to include this .h/.hpp file in your main script with
#include "your_file.h"
. It’s the same thing that you do with Arduino.Is that it your question? Correct me if it’s wrong.
Hi, I have a problem to build any program in Visual Studio (I have the same error in Arduino IDE): I only use the Build option (it’s suposed it doesn’t matter if you have the board connected; anyway, I’ve tested it with and without several ESP32 connected) and I get several warnings: “No se esperaba & en este momento”. Finally I get the error “El proceso del terminal “C:\Users\josel.platformio\penv\Scripts\platformio.exe ‘run'” finalizó con el código de salida 1″.
Do you have any idea to solve this?
If I try to finish the projct-setup I get this error
Could not initialize project
PIO Core Call Error: “The current working directory F:\myData\PlatformIO\Projects\MyFirstProjekt will be used for the project.\r\n\r\nThe next files/directories have been created in F:\myData\PlatformIO\Projects\MyFirstProjekt\r\ninclude – Put project header files here\r\nlib – Put here project specific (private) libraries\r\nsrc – Put project source files here\r\nplatformio.ini – Project Configuration File\r\nPlatform Manager: Installing espressif32\r\n\n\nError: HTTPSConnectionPool(host=’api.registry.ns1.platformio.org’, port=443): Max retries exceeded with url: /v3/search?query=type%3A%22platform%22+name%3A%22espressif32%22 (Caused by ProtocolError(‘Connection aborted.’, ConnectionResetError(10054, ‘Eine vorhandene Verbindung wurde vom Remotehost geschlossen’, None, 10054, None)))”
So what am I doing wrong???
I followed this guide using a Rock 3A SBC and a Wemos D1 ESPduino board. Esptool flashed successfully when the ESPduino was on a USB2.0 port finishing with Hard resetting via RTS pin.
When the ESPduino was on a USB3.0 port esptool still autodetected the board but the output messages:
Connecting……..________……..________……..________
failed to connect to ESP32: Timed out waiting for packet header
Hi.
press the on-board BOOT button when the dots start showing up on the debugging window.
Regards,
Sara
Yes. Holding down the boot button did work.
I just had to hold it down longer. Then the upload completes successfully.
Thanks. Obrigado.
Great!
Hi Sara. Thanks for this very informative post. Been referring to it while installing VS Code with PIO extension. Any tip on how to point the compiler to the proper location of libraries so I do not get “fatal error: ESPAsyncWebServer.h: No such file or directory” upload statements when compiling? I am 100% sure the libraries are in the ../include folder together with the header files. My .cpp file did not have any problems anymore meaning the includePath is in order. thanks.
Hi.
How are you installing the libraries on VS Code?
Regards,
Sara
I am having an issue using pinMode in one of my projects. It seems that pinMode can’t be found by platform IO. The Ardunio.H file is included but no joy when trying to build. The code builds just fine without the calls to pinMode. The blink code example builds and runs with no issues. I suspect a path problem but just can’t nail it down.
Why would Platform IO be unable to fine pinMode in one project yet find it in the blink example? In both cases Arduino.h is included. I suspect a path problem. Any clues you can give would be great.
Hi Mike.
What is exactly the issue you’re getting?
Regards,
Sara
Thanks for getting back to me. I solved the issue, cockpit error.
Hola
yo intente de programar un board esp32 hicé la tarrea como enseñe en la guia pero en la carga me dice falla
Hi Sara,
I am using Arduino IDE to develop software for ESP8266/ESP32 boards. I intend to try PIO: if I upload to these boards with PIO, can I reuse these boards should I revert to Arduino IDE ?
Hi Sara! How do I use the WiFiManager library in Visual Code with PlatformIO IDE? Thank you very much
Hi.
Search for the library in the Libraries tab in the PIO Home menu.
Then, there should be a button to add the library to your project.
Regards,
Sara
Thank you. This got me started with VS Code and the ESP32. The only instructions missing were the for the ESP USB driver. I think some mention needs to be made before the running of VS Code because people that are new to the ESP or doing new installs of VS Code or on new computer are likely to run into the problem.
Of course the other place to mention drivers could be under Troubleshooting. My particular ESP32 required me to hold down Boot to load firmware.
Thanks again.
You’re right.
Thanks for pointing that out.
REgards,
Sara
Hi Sara,
I have started using platformio and am facing the issue that when I create new project my project wizard is too slow and is taking too much time and stuck in the project initialization. Kindly guide what can I do to fix this?
Thanks,
Imran
Not her but I had this same issue and I fixed it by running Visual Studio Code as admin.
Thanks I am going to try this solution and let you know if it works which I hope it will.
I installed VS CODE, PlatformIO IDE, I’m recording on DOIT ESP32 DEVKIT V1, everything works perfectly.
But I’m looking for simple codes like the use of if, else, etc… and I can’t find examples.
Please where can I find sample codes to use in VS CODE\PlatformIO ?
You can use the same code you use in Arduino IDE on VS Code.
Regards,
Sara
Thank you for turning the lights on!
Hello,
every time i tryed to upload the code i get a Error at the
” Linking .pio\build\lolin32_lite\firmware.elf ”
Im not using the esp32 lolin32_lite and im not that good with that compiler stuff
pls help 😉
Followed the installation guide – but when I create a project
[env:d1_wroom_02]
platform = espressif8266
board = d1_wroom_02
framework = esp8266-nonos-sdk
monitor_speed = 115200
no src/main.cpp is auto injected
when I create one
#include <Arduino.h>
#define LED 2
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, HIGH);
Serial.println(“LED is on”);
delay(1000);
digitalWrite(LED, LOW);
Serial.println(“LED is off”);
delay(1000);
}
it cannot find <Arduino.h>
Is there some prerequisites not mentioned ?
Hi.
When creating the project, the framework should be arduino.
You selected a different one.
Regards,
Sara
Hi Sara
Arduino Platform – also when the target is an ESP32 D1 Live Wroom
Yes – it works – thanks for the article and hint.
Great tutorial! It was really helpful!
I have Ubuntu and python 3.10.12, and there was just one more step to get platformIO going, for anyone getting an interpreter error
sudo apt install python3-venv
Found it on
https://community.platformio.org/t/platformio-ide-cant-find-python-interpreter-on-linux/24262/3
Perfect tutorial! Thanks a lot.
The only thing I’ve modified in the code is instead of “LED” I used “LED_BUILTIN” because It did not blink otherwise.
Really great, worked first time. Thanks