Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 18 of 18

Thread: [Tutorial] How to use PlatformIO / Visual Code Studio for Teensy

  1. #1

    [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.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 03.53.15.png 
Views:	78 
Size:	85.4 KB 
ID:	24136

    PlatformIO main page
    PlatformIO adds a purple/blue toolbar to the bottom of the window. The home button opens PlatformIO.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 03.55.58.png 
Views:	65 
Size:	27.2 KB 
ID:	24137

    The main page allows to browse all your projects, create new ones (even import Arduino ones), browse libraries and find documentation about boards.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 03.58.18.jpg 
Views:	100 
Size:	79.5 KB 
ID:	24144

    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.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 03.58.46.jpg 
Views:	104 
Size:	73.5 KB 
ID:	24139

    Project
    The main file is underneath src and is called main.cpp. All other files on the projects sidebar don’t matter to us.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 04.48.39.png 
Views:	72 
Size:	122.7 KB 
ID:	24140

    To compile a project click the checkmark and to upload the arrow on the bottom toolbar.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 05.41.15.png 
Views:	73 
Size:	35.6 KB 
ID:	24145

    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.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 04.54.43.jpg 
Views:	141 
Size:	77.7 KB 
ID:	24141

    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“
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 04.53.36.jpg 
Views:	119 
Size:	112.7 KB 
ID:	24142

    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.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 04.06.28.png 
Views:	95 
Size:	90.6 KB 
ID:	24143

    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.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 03.58.18.jpg 
Views:	121 
Size:	70.7 KB 
ID:	24138  

  2. #2
    Senior Member
    Join Date
    Oct 2015
    Location
    Vermont, USA
    Posts
    321
    Thanks for the info.

    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.

  3. #3
    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.
    Click image for larger version. 

Name:	Bildschirmfoto 2021-03-22 um 14.41.22.png 
Views:	76 
Size:	283.4 KB 
ID:	24151

    I unfortunately can't update the original post anymore.

  4. #4
    Senior Member manicksan's Avatar
    Join Date
    Jun 2020
    Location
    Sweden
    Posts
    613
    Think you need the different USB flags as well:
    (i.e. when using build_flags = -D)
    Code:
    Build-flag               | Description (as in Arduino IDE)
    -------------------------|--------------------------
    USB_SERIAL               | Serial           
    USB_DUAL_SERIAL          | Dual Serial   
    USB_TRIPLE_SERIAL        | Triple Serial  
    USB_KEYBOARDONLY         | Keyboard
    USB_TOUCHSCREEN          | Keyboard + Touch Screen
    USB_HID_TOUCHSCREEN      | Keyboard + Mouse + Touch Screen
    USB_HID                  | Keyboard + Mouse + Joystick
    USB_SERIAL_HID           | Serial + Keyboard + Mouse + Joystick
    USB_MIDI                 | MIDI
    USB_MIDI4                | MIDIx4
    USB_MIDI16               | MIDIx16
    USB_MIDI_SERIAL          | Serial + MIDI
    USB_MIDI4_SERIAL         | Serial + MIDIx4
    USB_MIDI16_SERIAL        | Serial + MIDIx16
    USB_AUDIO                | Audio
    USB_MIDI_AUDIO_SERIAL    | Serial + MIDI + Audio
    USB_MIDI16_AUDIO_SERIAL  | Serial + MIDIx16 + Audio
    USB_MTPDISK              | MTP Disk (Experimental)
    USB_RAWHID               | Raw HID
    USB_FLIGHTSIM            | Flight Sim Controls
    USB_FLIGHTSIM_JOYSTICK   | Flight Sim Controls + Joystick
    USB_EVERYTHING           | All of the Above (only available @ teensy 3.1 3.5 3.6)
    USB_DISABLED             | No USB
    note. the flags cannot be mixed and only one can be active at a time.
    example:
    build_flags = -D USB_MIDI_SERIAL

    also the USB_EVERYTHING is not supported by teensy 4.x
    because of hardware limits.
    https://forum.pjrc.com/threads/66279...l=1#post270343
    Last edited by manicksan; 03-22-2021 at 02:38 PM. Reason: add notes

  5. #5
    Junior Member
    Join Date
    Mar 2021
    Posts
    17
    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)

  6. #6
    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:
    Click image for larger version. 

Name:	platini.png 
Views:	386 
Size:	93.9 KB 
ID:	24154
    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:
    Code:
    [platformio]
    default_builds = stock_lib
    [env]
    ; define board, framework, platform etc here
    [env:stock_build]
    src_filter = ${env.src_filter} -<../lib/new_lib_test_version/>
    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.

  7. #7
    Junior Member
    Join Date
    Mar 2016
    Posts
    5

    Talking

    Hello,

    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.

    Edit : Maybe found a solution to test : https://github.com/ftrias/TeensyDebug

  8. #8
    Senior Member
    Join Date
    Oct 2016
    Posts
    1,054
    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?

  9. #9
    Junior Member
    Join Date
    Mar 2016
    Posts
    5
    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).

  10. #10
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    1,244
    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.

  11. #11
    Senior Member
    Join Date
    Oct 2016
    Posts
    1,054
    Quote Originally Posted by BriComp View Post
    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.

  12. #12
    I am in VS + VM now and can not find Teensy Examples. Please help.

  13. #13
    Found it, everything arduino/teensy is under vMicro toolbar, of course.

  14. #14
    Senior Member
    Join Date
    Dec 2015
    Location
    LA
    Posts
    277
    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?

  15. #15
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,497
    @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.

  16. #16
    I'm trying out PlatformIO. My program compiles and loads to a Teensy 4.1 with no issues.

    Is there a serial monitor function in VS/PlatformIO that I can use or are people just using Putty or something?

  17. #17
    Senior Member
    Join Date
    Mar 2017
    Location
    Oakland, CA, USA
    Posts
    706
    I use Serial from Decisive Tactics (https://www.decisivetactics.com/products/serial/). PlatformIO does have a “monitor” task (or “upload and monitor”), but I prefer Serial.

  18. #18
    shawn, thanks for the reply. I'm using the built-in serial monitor in platformio, it's the icon near the home button that looks like a plug.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Capture.PNG 
Views:	92 
Size:	177.0 KB 
ID:	29744  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •