Table of Contents
Complete Beginner’s Guide to Platform IO
Platform IO is a platform that allows core developers to compile the same code with different platforms using only one command i.e. platform io run. It is an extension in VSCode and hence can be used with our favorite code editor. It has many features that allow core developers to have a smooth development experience.
Also it allows you to export and edit the same project in windows as well as windows or any other systems without any changes required.
Installing Platform IO
It is a VSCode extension and can be simply installed by adding Platform IO from the extensions center of VSCode
Creating a Project in Platform IO
- Click on Platform IO Logo in VS Code
- In PIO Home 🡺 Go to Open 🡺 Home Screen of PlatformIO is visible
- Select New Project 🡺 Give a name and select board for project 🡺 Select Location 🡺 Done
- New Project will be created
- You can write code in main.cpp under the src folder, we can edit the platform under platformio.ini to upload to different boards.
Different Folders and files in Platform IO
Platform IO consists of many folders to support embedded development on multiple systems and compiling information.
Platform IO consists of:
- .pio: This folder consists of complete debug and compiler information of the project. This folder need not be uploaded and is included in gitignore as well to not upload on Github. <br>
We don’t have any work in this folder and we don’t need to edit this folder.
- .vscode: This folder consists of JSON files required to run platform io and its services in VS Code editor. This folder also doesn’t need to be touched or edited to do our work.
- include: This folder is present to allow you to add “.h” header files into the PlatformIO project that are not added as libraries in your project. You can place libraries defined by you with .h extension to use them. Also, this code is not compiled and there is no use of putting a .c/.cpp file in this folder.
- lib: This folder is used to define complete libraries that are private that are made by you. In include, we only add .h files but this folder can consist of complete details like documentation, examples, source files, JSON, etc. Basically, it contains complete information related to a library.
- src: This folder consists of main.CPP and will contain the code that we want to write. We can add more files in this folder to integrate into our code. This is the main folder we will be working on. Code will start executing from main.CPP.
- test: This folder is used to write tests that can be used to automate the testing of the code. These tests can be used to see the efficiency of code and how code behaves on boundary conditions. These are generally useful in bigger projects.
- .gitignore: This folder is a part of the git family and consists of the name of the folder that needs to be and will not be uploaded on git while uploading the project.
It generally includes the .pio folder and some files of the .vscode folder.
- platformio.ini: It is a kind of settings folder where you can decide the platform on which code to be uploaded, add libraries, control monitor speed, select framework, and many more things.
Visit the given link to explore multiple options and their application in platform io : https://docs.platformio.org/page/projectconf.html
Adding Libraries in PlatformIO project
Platform IO provides multiple easy ways for you to add libraries to a project. We are going to discuss one of the easiest and most useful ways of adding libraries in Platform IO:
- Go to Platform IO symbol in the left pane of extension 🡺 Under PIO Home select Open.
- In the main window select Libraries 🡺 Search the library you need 🡺 Click on that library 🡺 Click on Add to Project 🡺 Select project in which you want to add library and click add.
Now the library is added to your project. There are also other ways to add libraries in Platform IO by editing the platformio.ini folder.
See the below link under ‘Project Dependencies’ to explore those methods: https://docs.platformio.org/en/latest/librarymanager/quickstart.html
Uploading code to hardware using PlatformIO
Platform IO provides a very easy way to upload code on the hardware. The following steps should be followed to upload the code:
- After writing the code, verify the details and environment in platformio.ini. You can select different hardware by removing the lines that look like the following and adding the setting for the required board. Setting can be googled to find the exact lines:
platform = Atmel Avr
board = pro16MHzatmega328
framework = Arduino
You can also select multiple boards at one time using the method mentioned in the following link: https://community.platformio.org/t/changing-destination-boards/4751/2
- Click on Platform IO Icon in left sidebar of VS Code
- Under Project Tasks 🡺 General, you have multiple options and operations that can be done for the project.
- Build 🡺 Compiles the code to see if any errors
- Upload 🡺 Compiles and upload the code to the hardware connected.
- Monitor 🡺 To open serial monitor in VS Code
- Upload and Monitor 🡺 Compiles uploads the code and open the serial monitor just after uploading code
- Clean and Clean All 🡺 Used to remove and delete any dangling pointer or function that is left in the stack and not deleted or removed.
** You can also have few options present in the bottom bar to compile, upload and monitor the code.
In the end I want to say that platform Io is an amazing platform that is used to develop embedded systems code, and has integrated most capabilities like writing, uploading, and even debugging of code in one place.
It is a great platform to work on and will help you guys a lot.