PlatformIO Tutorial for Arduino and ESP – First Steps with Visual Studio Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the arduino development environment can do nothing and what better alternative i'm using i will show you after the intro [Music] the arduino development environment can do nothing and that's a good thing because this development environment was meant to keep the entry barrier into microcontroller development as simple as possible and not to overwhelm the newbie well i admit i'm a professional developer and i'm missing some features in the arduino development environment that make programming more fun for me platform io is a free tool especially for working with microcontroller systems like esps or arduino it is installed as a plugin in the visual studio code editor vs code for short which is also free so by working with vs code we have a very powerful development environment that offers many more features than the arduino ide by the way visual studio code is not to be confused with visual studio without the code the big software development platform i think these very similar names are a bit of an unfortunate choice we will now on star vs code then i will show you how to install the platform i o extension in vs code and parallel to my video where i compare arduinos and esps i will show how to use platform io to load a program on an arduino and on an esp okay first of all download vs code for your operating system the link is in the description below i would recommend the stable version of vs code to install platform io we go to the extensions section sort of an app store for vs code add-ons there we search for the official platform i o extension click install and wait for the installation to be finished recommendations may pop up that you should install additional recommended add-ons these are add-ons for the c c plus plus programming language since the arduino framework uses c plus the installation makes sense so now we create a first project and platform io for the arduino uno board for this we go to the platform io home page in vs code and then click on new project in the form we choose a project name select the arduino uno as board and arduino as framework for location i always leave the default value this is the easiest for now if you move the mouse over the question mark the default location is shown for me this is my user folder documents platform io projects click ok and the new empty project will be created and platform i o the projects are structured a little bit different than in the arduino development environment let's have a look at the folder structure of the empty project we can neglect the folder test for now you only need it if you want to work with unit tests src for source contains all source code files here's also our main dot cpp file includes can be used to store header files here and lib is used to store project specific libraries the platform io.ini is the configuration file for platform io projects but we will get back to that one later for now we are only interested in the src the source folder because this is where the main cpp is located which we will now open with a double click platform i o already fills the main cpp with the bare setup and loop methods furthermore we have to include the arduino.h library at the top in the arduino development environment this happens automatically in the background but platform io fills in this line for us i'm including the same code here that i used in my video arduino versus esp a feature comparison now let's take a look at these little but important buttons down here to the left these are the buttons that go with platform io the house icon opens the platform i o page in bs code the check mark icon checks the current code for errors the arrow icon also checks the code and if no error is found it loads the current code onto the board so this will be our main upload button the trash icon deletes any compiled libraries these would then be recompiled on the next check or upload this is how you can force a recompilation of your whole project the plug icon opens the serial console and the last icon opens a shell if you want to enter platform io or shell commands manually the two areas on the far left are only interesting for git and version control and this button in between shows errors and warnings and presents them in a clickable list so you can quickly move to a specific arrow in the code at the bottom right this is the position of the cursor and here you can set the size and type of indentation and the code if you change this value you should execute a format document afterwards to do so right click in the source code and select format document well now we know how to upload the code then let's do it the code is compiled and uploaded and we should see our led blinking yay now we repeat the whole thing with an esp32 before we move the arduino now let's go to the devices section of platform io here all boards are shown which platform io has discovered my arduino uno is always shown as dev cu usb modem 14101 now we connect the esp32 and click refresh two new usb ports appear in the list of devices don't ask me why there are two but i know that dev cu slab underscore usb to uart is the correct port for the esp32 these two ports will be a problem in a moment that's why i will show you how to deal with it now we create a new project first we choose a project name and as board i take the generic espressif esp32 def module if you're using a different board that is listed right here then of course i would take that as framework we take arduino again of course we open the main cpp and copy our blinky code into it click on upload and we get an error in the error message you can see what happened platform i o tried to upload the code to this usb port but this is the wrong port platform i o doesn't know which of the two ports is the right one and of course takes the wrong one but we can solve this problem with the project settings go to the projects section of platform io find the right project in the list and click on configure you should see a tab called esp32 dev we need to add an option called upload port to do this i type the word port in the field at new option platform io presents a list of all options with the word port i choose upload port a new field upload port is edit and the cursor jumps to this new field and now we can just click into this new one and we get a list with all usb ports as we have already seen in the devices section here we select the dev cu slab underscore usb to uart port and very important click on save in the upper right corner i always forget this one under windows this will be a com port there you have to watch the devices list before and after connecting the boards by the way if the devices list does not change after connecting a new device try to restart vs code after restart the new device should be listed if we upload our code now everything should work because we told platformio to always use this port dev cu slab underscore usb to uart when we click upload platform io sticks to the proper coding standards while the arduino ide has taken some of the work off of us adding the arduino.h header file for example is one such case another important point is the declaration of functions let's take the code from the loop section put it into a function and call this new function name in the loop section instead now once we trigger the code check we get an error the problem is that our new function has not been declared yet in cc plus you have to declare functions as sort of registering that this function even exists in the arduino ide this was not a problem because there it automatically looked for functions in the main file and declared them for us under the hood if you still want to work with only one main cpp there are two easy ways either declare the functions correctly before using them to do this just take the function name including the return value and parameters and write it runs in the top section before using the function or move setup and loop to the very end in the code below all function definitions the code of the function itself is the function definition registering a function before using it for the first time is the function declaration now of course the legitimate question why do we actually do this if the arduino ide makes it more comfortable for us experience is when a project reaches a certain size or complexity working with the arduino ide becomes very confusing as soon as several functions or custom classes over several files are added it is no fun working in the arduino ide any longer here are a few helpful tools that the arduino ide does not have at the moment intellisense well where exactly intellisense starts and stops is still not clear to me for me these are the helpful suggestions when i start typing a command or function name and also the parameter suggestions that are displayed to me also the function descriptions go to declaration and definition you can right click on a function and go directly to a function with go to definition or go to declaration this also works with variables and go to references lists or usages of a function or variable peak that's basically the same but does not leave the current file instead it shows a little preview window at the cursor's position rename symbol rename symbol is very useful if you are not satisfied with a variable or function name all declarations definitions and also calls can be changed in one go in other development environments this is called refactoring and one last tip how to get your function descriptions display with intellisense if you provide a documentation for a function with certain keywords and a certain syntax then these are displayed and the intellisense pop-ups you can see if you hover with the mouse over a function name the whole thing works according to a standard called oxygen i put a link to further doxygen keywords in the list at the description below hey this is my clumsy attempt to remind you of liking the video if you found valuable informations or just feel entertained the right way please give the video a thumbs up and even better if you subscribe to my channel i do get the right indicators so that i know i did a good job thank you alright and this brings us to the end of this platform io intro tutorial we have installed visual studio code and the platform io extension we have looked at the project structure and take a look over a few important differences from the arduino development environment we looked at how to use platform io's project settings to fix upload issues with the usb ports we did a quick blinky test with an arduino and an esp32 board and in the end we learned about helpful features of platform i o that make programming much easier i hope you could convince you know i could convince one or the other of platform io have fun tinker around stay healthy bye [Music]
Info
Channel: raydiy
Views: 1,539
Rating: undefined out of 5
Keywords: platformio, platformio arduino, platformio vscode, platformio esp8266, platformio esp32, platformio serial monitor, diy, microcontroller, esp, esp32, arduino, programmieren, c++, vs code, tutorial, platformio library install, platformio debugging, visual studio code
Id: SAVczh7W1Oc
Channel Id: undefined
Length: 14min 2sec (842 seconds)
Published: Tue Sep 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.