[Tutorial] How to use PlatformIO / Visual Code Studio for Teensy
I only recently discovered PlatformIO and I’m so happy about the improved workflow and features compared to the editor of Arduino. No more slow app launch, having auto complete, much better UI and being able to install useful editor extensions is awesome.
I stupidly always thought PlatformIO would be hard to install, configurate and therefor never tried it out. I think it’s mainly the websites fault. But turns out: The opposite is true. It couldn’t be easier to install. Here a small tutorial that hopefully motivates more people to try it out.
Installation
1) Get Visual Code Studio. It’s free and open source.
2) Go to the extension library (the 4th icon on the sidebar), search for PlatformIO and install it. Congrats you have PlatformIO installed.
PlatformIO main page
PlatformIO adds a purple/blue toolbar to the bottom of the window. The home button opens PlatformIO.
The main page allows to browse all your projects, create new ones (even import Arduino ones), browse libraries and find documentation about boards.
Creating a new project
To create a new project simply press on „New project“ on the main page. Select the board you want to use and it automatically creates a project.
Project
The main file is underneath src and is called main.cpp. All other files on the projects sidebar don’t matter to us.
To compile a project click the checkmark and to upload the arrow on the bottom toolbar.
Add libraries to projects
If you want to add additional libraries PlatformIO provides it’s own browser for libraries. Go to PlatformIOs main page via blue home button and click on libraries. It provides all libraries that Arduino also provides in it's editor plus additional.
The overview of each library is much more detailed and better compared to Arduino. You can already take a look inside the files before installing, see changelogs and take a look at examples. To install an extension simply click on „add to project“
Teensy USB type
Open the PlatformIO.ini file from the projects sidebar. And add this entry:
Code:
build_flags = -D USB_MIDI
The correct build_flag depends on the usb mode you want. The teensy platform documentation inside the platforms browser lists all.
Yes this step is unfortunately a bit less comfortable compared to Arduino. But at least the the selected usb type gets saved inside the projects.
Where are the code examples for boards and libraries?
Arduino editor provides examples for its boards and installed libraries can also provide examples.
In PlatfromIO examples for platforms/boards can be found by selecting the specific platform inside the platforms browser. Examples for libraries can be found by selecting a specific library in the libraries browser.
When i followed your instructions, just after installing the PlatformIO extension, it said that it needed Python. I followed the instructions (such as they were) and chose to install the most recent Python release from python.org.
The python installation asked a bunch of questions that I hope I got right.
Restarting VSCode, VSCode automatically started installing some additional Python stuff.
This is just a heads-up to document that the process may have additional steps for some users.
Last edited by chipaudette; 03-22-2021 at 01:44 PM.
Thanks for mentioning! I completely forgot that Windows doesn't have Python preinstalled. At least Visual Code Studio notifies during PlatformIO installation if Python is missing and offers to install it.
I unfortunately can't update the original post anymore.
Thanks for the nice tutorial. I can confirm that PlatformIo is really convenient.
I think that hardly anyone who has used it for a time will go back to the Arduino IDE.
Especially I like the Auto Complete function and the possibility of debugging, for some boards direct, for many other boards with e.g. a jlink-debugger (unfortunately not yet my Teensy 4.1).
Setting breakpoints and inspecting variables by positioning the cursor on them makes programming so much easier.
Worth to mention is that VS Code is available for MAC and Linux as well (even though I never tried)
What i find extremely useful in Platformio:
1. local to the project libraries placed in the /lib folder. Compiler will look into this folder first and if an included library is not found, only then look in the global path. This way i can have my customized versions of libraries per project, not interfering with the globally installed/stock ones. Packaging a complete project is much simpler if all required libs are stored locally.
2. things the plarformio.ini file can do, and there is a lot. As an example:
It could be very useful when working on new library versions, comparing them against the old, currently installed within the framework ones.
Simply add the new version in the /lib folder and create a conditional build, ie:
If the default_libs is set to stock_lib, the local library folder will not be included and the compiler will grab+link the global version.
3. The fact i can use the same VScode for many other non arduino projects.
One important fact not yet mentioned is platformio requires the functions to be declared, just as with normal c/c++. Simply copying the ino file into main.cpp will most likely fail to compile due to lack of function declarations.
Thanks for the tutorial, I have dropped Arduino IDE. (Maybe arduino 2.0 would be better)
Next step would be to have debugging available, to avoid "println" debug messages The PlatformIO single way to debug teensy costs more than I can afford.
Seams that Visual Micro already has debugging support.
I've tried PlatformIO a couple of times, but ended up going back to Arduino. Arduino has lots of issues, but I do understand how to install it and maintain it. Can you explain where in the PlatformIO process was TeensyDuino installed, and how can you update to a new or beta version of TeensyDuino?
I have installed latest stable Arduino IDE (1.8.19) and Teensyduino (1.56). The problems I have with Arduino IDE is productivity : code edition, selection, completion, navigation... For me, VSCode is really faster. I did not searched for an alternative, but I was curious what platformio was, so I tried. I saw vscode extension, with teensy integration. I installed the vscode extension and tried an empty project.
One feature I would love to see is debugging (breakpoints, see variables contents...).
For example, I am developping a small device based on teensy, that can be configured using USB. So I have a golang app that detects teensy serial ports, and then interract with it though commands. It acts as wrapper with teensy serial port and provides rest apis, for an angular front-end. I have some data exchanged as JSON. I can have vscode as debugger for backend and frontend, but no debugger for teensy (yet).
I would recommend Visual Micro with Visual Studio. It gives the stability/understanding of the Arduino system with the IDE of visual studio and Teensy Debugging.
I use it all the time and would not go back. I have used the Arduino IDE and PlatformIO but as you say not impressed. Any libraries or code added to Arduino fall straight through to VisualMicro/Visual Studio.
See here.
I would recommend Visual Micro with Visual Studio. It gives the stability/understanding of the Arduino system with the IDE of visual studio and Teensy Debugging.
I use it all the time and would not go back. I have used the Arduino IDE and PlatformIO but as you say not impressed. Any libraries or code added to Arduino fall straight through to VisualMicro/Visual Studio.
See here.
Thanks for pointing out that VM/VS uses what has already been installed for Arduino. I find that much preferable to the PlatformIO approach, and I'll try it again.
Attempting PlatformIO on some existing T3 code. Get:
Code:
C/C++ IntelliSense service does not support .INO files. It might lead to the spurious problems with code completion, linting, and debugging. Please convert .INO sketch into the valid .CPP file.
A link suggests to add declarations to all my functions, lots of them. Am I missing something?
@bicycleguy
if you wanted to use the advantage of VSCode (as I do), simply move all code from ino to a cxx file and let ino be empty.
Obviously, your new cxx file must be logically correct an include all declaration int the right order (better cxx programming anyway).
Arduino converts ino file to temporary ino.cxx files that are cxx correct, so ino cannot be understood by intellisense.
Do so you can use VSCode and if you want also Arduino for compiling. Arduino is happy if there is an ino file even if empty.