Slides from my beginners workshop on the great Open Source extension for Visual Studio Code called PlatformIO. Covers getting started using an Arduino or ESP32 as well as the GIT integration.
4. Download and set up PlatformIO
Click Extension Manager icon on the left toolbar
1. Open Extension
Manager
2. Search for
“PlatformIO”
3. Install the
PlatformIO IDE
1.
2.
3.
5.
6. Lets get started
1.
2.
3.
1. Click the PlatformIO icon
2. Click the “Open” item to open the
start page
3. Click the “New Project” button
7. Configure your project
Select your microcontroller in the Boards menu
Select Arduino UNO, ESP32 or any
other microcontroller you have
brought with you.
Make sure to select the exact variant
if there are multiple versions (such
as for ESP32 based boards)
10. edit the file src/main.cpp
Hello World
2.
1.
1. Click the Explorer icon to show
the files generated
2. Double-click the pre-defined
source file to open it in the editor
This is how you’ll start every project
created with the Arduino framework.
12. Does it compile?
1.
1. Click the PlatformIO icon to
show the commands
2. Click the Build option to compile
the code
If there are no errors, you typed it all
correctly. If you get an error - read it
carefully and raise your hand if you
don’t understand how to fix it
There are many shortcuts for the
command:
- Control-Option-B / Ctrl-Alt-B
- Control-P, PlatformIO: Build
2.
15. Uploading
To the board
1.
1. Click the PlatformIO icon to
show the commands
2. Click the Upload option to
compile the code
A terminal windows should open and
lots of text should scroll by -
hopefully with a [SUCCESS]
message at the end.
There are many shortcuts for the
command:
- Control-Option-U / Ctrl-Alt-U
- Control-P, PlatformIO: Upload
2.
16. • Click the PlatformIO icon to show the
commands
• Click the Monitor option to open a
terminal window that shows the
“Hello world” text from the
microcontroller
• It will print out the text once every
second - or rather every 1000
milliseconds.
• Microcontrollers are very precise, so
you could even ask it to wait a
microsecond, but that would produce
a massive stream of “Hello world”
messages.
Did it work?
see it talk back to us
1.
2.
17. Workflow
• Create a Project
• Either in a New > Window, or in the same window
• Setup the project for your development board
• Adjust platformio.ini (if required)
• Write code
• Upload to test
• Use monitor to see the debug output
21. with your micro
1. Click the PlatformIO icon
2. Click the “Libraries” item to
open the library search page
3. Type “Adafruit Dotstar” in the
search field and press enter
Using libraries
1.
2.
3.
22. (as an example)
Once you find the correct library
("Adafruit Dotstar”), click to view
the details and examples.
Use the dropdown under
Examples to change it to
“ItsyBitsyM4Onboard”.
Copy the example code (to
memory) and then click the Add
to Project button.
Adding Dotstars
23. to the project
In the popup - select the Hello World
project and click Add
Adding Dotstars
24. What happened?
What happened under the hood is that
this text file was updated.
platformio.ini is the main setup of your
project, so let’s take a look at it?
Adding Dotstars
26. what’s wrong?
When we build the library, we get an
error telling that Wire.h is missing.
This is a library that is used by the
Adafruit Dotstar library.
This is a bug in the example we’re
using. The library is already on our
machine, but we need to include it.
Can anyone suggest how we fix
this?
Errors?
27. added to the top
In this case, it was one of the
common libraries that were
needed.
wire.h
28. what’s wrong?
The error says that 'rainbow' was
not declared in this scope. But we
do have a method called
“rainbow”, don’t we?
The problem is that this method is
being used BEFORE the compiler
has reached the part of the code
it’s in.
Let’s move our loop-method to
the bottom of the file to solve this.
More errors?
We’ll start with everyone downloading and installing Visual Studio Code. Some of you will likely have that installed already, so help the person next to you (if required)? Lets have a raise of hands here. How many have VS Code installed already?
Microsoft has changed quite a bit. In general, they have changed most of the negative buzz they had some years ago. I’m not saying this because Microsoft is a Bitraf sponsor (they are!) but I’m saying it sine that’s how I perceive them these days. They no longer try to be the guy in class that is always bragging. From what I’ve heard, they no longer charge money for the basic version of their software and OS. In general, they do a lot for good and one of these things are VS Code. I really like it and I use it for many things. The software is open and extendable so let’s start by having a look around the interface?
PlatformIO is a plugin for VS Code that makes it possible to program more than 800 different microcontrollers.
While that is downloading and installing - lets have a look at PlatformIO? Gary Explains… (Here we’ll show up until about 6 minutes into the video - if needed) Gary mentions Arduino and microcontrollers. That’s hardware, so let’s look closer at the hardware we’ll be using!
During setup, you may encounter this dialogue. You should obviously trust yourself…
Anyway - that’s a bit of background for why the hardware part of this elective is easy to use. It owes a lot to the Arduino project.
Go through the main source file (the #include at the top and then the setup and loop methods) as well as the other files generated
PlatformIO takes this further and provides a professional development experience.
Build option in the Project Tasks menu,
Build button in PlatformIO Toolbar,
Task Menu: Tasks: Run Task... > PlatformIO: Build, or in the PlatformIO Toolbar,
Command Palette: View: Command Palette > PlatformIO: Build, or
via hotkeys control-option-b / ctrl-alt-b
If you type something incorrectly, it’ll show up in the Problems-tab at the bottom. The text will also be highlighted. In this case, I’ve used a big B rather than a small, and since there is no such command - I’ll get this error.
Here is another typical error. Here I’m missing a semicolon on line 5.
When there are no errors, we can upload to the ESP32 microcontroller.
If anything goes wrong, it’s at this step…
Upload option in the Project Tasks menu,
Upload button in PlatformIO Toolbar,
Command Palette: View: Command Palette > PlatformIO: Upload,
using the Task Menu: Tasks: Run Task... > PlatformIO: Upload, or
via hotkeys: control-option-u / ctrl-alt-u:
But how can we tell that this worked? Show clearing and closing terminal windows
Monitor option in the Project Tasks menu,
Serial Monitor button in the PlatformIO Toolbar,
Command Palette: View: Command Palette > PlatformIO: Monitor, or
Task Menu: Tasks: Run Task... > PlatformIO: Monitor:
Note that you do not need to do the Build step. It will be done automatically if anything is changed. Just upload the code and you’re done.
You can also note that the RGB LED is not longer fading through colors. This is because we have now replaced the software running on the device. It only has one program and it starts every time we apply power.
It’s not a proper Hello World for hardware unless we blink a LED? Let’s extend our code a bit to add this. We’ll use two new commands: digitalRead and digitalWrite.
After uploading, the builtin LED will blink.
You can find more details on the build process here: https://docs.platformio.org/en/stable/tutorials/espressif32/arduino_debugging_unit_testing.html We’ll get back to parts of this at a later time in the course.
So blinking a LED is fun, but how do we make a beautiful multi-colour LED work?
In my case, it’s not the right library that is shown when we do this search, so I’ll have to scroll down to find the exact match. Click the name of the library
We need to update the Clock and Data pin numbers. The Data pin should be 33, the Clock pin should be 21
At Bitraf, we love Open Source and sharing. Sharing is caring! Let’s save the file to GitHub! Do you all have a GitHub account?
If you don’t have one, we’ll set it up now. If you already have one, you can just kick back and relax while some of us create a new account (JensaKristiania). Open GitHub.com to create an account.
Use your permanent email address.
When you have filled out the form and done the quiz, you’ll need to confirm the email
Once that is in place, you might get to this screen. Just click through and go fo the free account.
When all is done, you should get to this screen and the account should be working. Now we can integrate it into VS Code!
Start by clicking the Source Control icon on the left. Then click the “Publish to Github” button to see this dialogue open
Start by clicking the Source Control icon on the left. Then click the “Publish to Github” button to see this dialogue open
VS Code will now ask to open GitHub in your favourite browser. Press the “Allow” button.
Next, you authorise the access. Make sure you use the Github account you want to integrate (if you have multiple)
When that is done, you’ll be asked to open VS Code. Be sure to do that.
This is the last part of the authentication process. Once you’ve pressed “Open”, we are ready to upload the files to Github!
The name of the repository will be the same as the name of your project. Publish it to a Publicly available repository. If you do client work or do not want to show off what you do, you can also use a Private repository but for now we’ll keep it Public.
Once the upload completes, you can go to the Github website and see the files you just backed up. Now lets do something fun that you might not know about. With your Github repository open - press the Punctuation key on your keyboard and wait a little…
This will open a quite functional version of VS Code - directly in your browser! Set the defaults and browse around a bit. It’s just like on your computer, right?
There’s even an Extension installer for PlatformIO, but it cannot be installed. In general - only browsing and editing code will work, but that can certainly be useful. Let’s add a comment to our src file - directly in the browser
When we press CTRL+S to save the file, you’ll see an “M” next to the changed file. This means that we have Modified a file. Let’s commit that file from the Github website and back to our desktop application. Click the Source Control button and write a message in the field at the top Left corner. Now you can press the Checkmark at the top or the keyboard shortcut CTRL+Enter to commit and push the files. Now let’s go back to the VS Code application and let’s go grab the changes we just made?
Since the desktop app does not know that the file has changed, we can click the three dots menu and select the “pull” option to pull down the change we just made. As you can see - most of the normal git commands are there for you. You could of course also use the command line in the terminal window also if you prefer that. I usually do all my git work on command line myself, but its really nicely integrated into VS Code, so now you’ve seen that.
One last feature that is useful - you have s simple Diff-tool here as well. Make a change in your file and then click it in the list to the left (where you see the “M”). This will bring up the file last pulled from Github as well as your file. Here you’ll easily see what you changed. Imagine you work on a project and then head off into a meeting or lunch. When you come back, you’ll want to get into the code quickly and with this you can see all the changes made since last commit.
To learn more about Git in VS Code, check out this tutorial