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

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:	8 
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:	7 
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:	6 
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:	9 
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:	6 
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:	6 
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:	7 
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:	7 
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:	7 
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:	5 
Size:	70.7 KB 
ID:	24138  

  2. #2
    Senior Member
    Join Date
    Oct 2015
    Location
    Vermont, USA
    Posts
    288
    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:	6 
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
    278
    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
    6
    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
    Junior Member
    Join Date
    Jul 2017
    Posts
    18
    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:	28 
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.

Posting Permissions

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