Arduino programming using Visual Studio Code.

The Arduino Integrated Development Environment (IDE) is a popular platform for programming Arduino microcontrollers. While it has numerous advantages, it also comes with some disadvantages. Here are some of the disadvantages of the Arduino IDE:

  1. Lack of Modern UI/UX: The user interface and user experience (UI/UX) of the Arduino IDE are often criticized for being outdated and not very intuitive. This can make it less user-friendly, especially for newcomers.
  2. Less Flexibility: The Arduino IDE enforces a specific project structure and workflow, which might not be ideal for all types of projects. More experienced programmers might prefer more flexibility in terms of project organization and build processes.
  3. Lack of Professional Development Tools: For large and complex projects, the Arduino IDE might not provide the level of professional development tools and project management features that other IDEs, like Visual Studio or Eclipse, offer.
  4. Less Integration with Version Control: The Arduino IDE has limited integration with version control systems like Git, which can make it challenging to collaborate on projects and manage changes effectively.

An alternative to Arduino IDE is PlatformIO.

PlatformIO is an open-source ecosystem for embedded development that offers an alternative to the Arduino IDE. It’s designed to provide a more powerful and flexible development environment for a wide range of microcontrollers and platforms. Here are some advantages of using PlatformIO over the Arduino IDE:

  1. Multi-Platform Support: PlatformIO supports a vast array of microcontrollers, development boards, and platforms beyond just Arduino. This means you can use the same development environment for different microcontroller architectures like ARM, AVR, ESP8266, ESP32, and more.
  2. Cross-Platform Compatibility: PlatformIO runs on various operating systems, including Windows, macOS, and Linux, ensuring consistent development experiences across different platforms.
  3. Advanced Project Management: PlatformIO provides advanced project management features, such as the ability to manage multiple projects simultaneously, better project organization, and integration with version control systems like Git.
  4. Integrated Development Platforms: PlatformIO integrates with popular code editors like Visual Studio Code, Atom, and CLion. This integration brings modern features like code autocompletion, syntax highlighting, and advanced debugging to your embedded development projects.
  5. Powerful Library Management: PlatformIO offers a library manager that allows you to easily browse, install, and manage libraries from different sources. This streamlines the process of adding functionality to your projects.
  6. Unified Build System: PlatformIO employs a unified build system that compiles code faster and more efficiently, often taking advantage of multi-core processors for quicker builds.
  7. Flexible Build Configurations: PlatformIO allows you to define custom build environments, enabling you to easily switch between different hardware configurations and platforms without changing your code.
  8. Advanced Debugging: PlatformIO provides enhanced debugging capabilities, allowing you to debug your code using both software and hardware debuggers. This is especially useful for complex projects and identifying subtle issues.
  9. Support for Different Languages: While the Arduino IDE is primarily focused on C/C++ programming, PlatformIO supports various programming languages like Python, making it more versatile for different types of projects.
  10. Community and Documentation: PlatformIO has an active community and comprehensive documentation, making it easier to find solutions to common problems and learn more about embedded development.
  11. Customization and Extensibility: PlatformIO’s architecture is designed to be modular and extensible. You can create and share your own platforms, boards, and libraries, making it suitable for various specialized applications.
  12. Professional and Production-Ready: PlatformIO is well-suited for professional development, especially for projects that may eventually go into production. It provides a more comprehensive and robust development environment.

How to install PlatformIO inside Visual Studio Code

  1. go to https://code.visualstudio.com/docs/cpp/config-mingw for installation instructions.
  2. Install platform io plugin from VS code extensions.
  3. go to https://github.com/msys2/msys2-installer/releases/download/2023-05-26/msys2-x86_64-20230526.exe to download MSYS2.
  4. Run MSYS2.exe
  5. In terminal, install the MinGW-w64 toolchain by running the following command:
    pacman -S –needed base-devel mingw-w64-x86_64-toolchain
  6. press enter to install all default toolchains.
    6.Add the path to your MinGW-w64 bin folder to the Windows PATH environment variable by using the following steps: 1.In the Windows search bar, type Settings to open your Windows Settings.
    2.Search for Edit environment variables for your account.
    3.In your User variables, select the Path variable and then select Edit.
    4.Select New and add the MinGW-w64 destination folder you recorded during the installation process to the list. If you used the default settings above, then this will be the path: C:\msys64\mingw64\bin.
    5.Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.
    6.Check your MinGW installation by running any of the following command in command prompt.
    gcc –version
    g++ –version
    gdb –version

PlatformIO Basics

When you first start Visual Studio Code with the PlatformIO extension you’ll be greeted by the PlatformIO Home screen. If you don’t see it look for a small icon on the bottom taskbar shaped like a “house” and click on it.

The Home screen displays the version of PlatformIO and also has a Quick Access section, which we will use to start our first project.

Before we get to that let’s examine some of the other icons down the side of the PlatformIO screen.

  • Home – You have already seen this, it has the current version and the Quick Access box that allows you to create new projects.
  • Projects – A list of all the projects you have created. You can edit these to add descriptions.
  • Inspect – This allows you to inspect a project for statistics like memory utilization.
  • Libraries – This is the Library Manager, which we will describe in detail later on in this article.
  • Boards – A list of the boards supported by PlatformIO. As of this writing, there are over 900.
  • Platforms – Platforms like the ArduinoAVR, Espressif ESP32, and others are listed here. The list will grow as you build projects with new boards.
  • Devices – A list of the boards that are currently attached to your computer. This is built up automatically so you don’t need to select the port, unlike the Arduino IDE.

Creating Your First Project

In PlatformIO your “sketches” are actually part of a “project”, the term “sketches” is not used here.

All of the resources required for your project are contained in one place, this includes libraries and code files.

Go back to your PlatformIO Home screen and click the New Project button. This will launch the Project Wizard.

The Project Wizard makes it very easy to create all of the initial files required for a PlatformIO project. You will need to supply the following information:

  • Project Name – Obviously the name of your project!
  • Board – The type of microcontroller board you are using.
  • Framework – The framework must match the board, and PlatformIO will determine this automatically for you once you select a board.
  • Location – Where you want your files stored. You can just leave this checked to accept the default location or uncheck it to choose a specific one.

Selecting a board may appear to be quite daunting, as PlatformIO has over 900 of them! But you certainly don’t need to scroll through the list manually, just start typing the bard name and the search will be narrowed down to one or more matching selections.

After you make the selections for your board PlatformIO will set up the files for your new project. If this is the first time you have used this type of board it will need to grab several files from the Internet, so you may have to wait a minute for it to finish. Subsequent selection of the same board will be much quicker, as it only has to do this once per board.

main.cpp File

One of the first big differences between PlatformIO and the Arduino IDE is the type of files you’ll be coding.

In the Arduino IDE, most of your files use the .ino extension. When you start a new project in the Arduino IDE you typically name the main file something like “mycode.ino”. When you first save it the IDE will place it in a folder called “mycode”.

In PlatformIO there are a number of files created for a project. The principal one is called main.cpp. The .cpp extension means “C++”, the language you are coding in.

You will keep all of your code files in a subdirectory called src, which is an abbreviation for “source”.

On the left side pane, you should see your project listed, and beneath it, you’ll see a number of subdirectories. Expand the src subdirectory by clicking on it and you should see a main.cpp file. Click on this file to open it in the editor window.

You will notice that the main.cpp file already has a line in it that reads as follows:

#include “Arduino.h”

This line must be present in every program you write for microcontrollers using the Arduino framework, including non-Arduino boards like the ESP32. PlatformIO will automatically insert this line into the main.cpp file for you. If you are copying some existing Arduino code you can paste it underneath it.

Arduino Uno Blink Test

Let’s start with the “Hello World” program for microcontrollers – the infamous Blink! Connect an Arduino Uno to one of your USB ports to follow along.

Here is Blink, modified for PlatformIO with the inclusion of the Arduino.h library:

#include “Arduino.h”

/*
Simple Blink sketch
simple-blink.cpp
Use for PlatformIO demo

From original Arduino Blink Sketch
https://www.arduino.cc/en/Tutorial/Blink

// Set LED_BUILTIN if undefined or not pin 13
// #define LED_BUILTIN 13

void setup()
{
// Initialize LED pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
// Set the LED HIGH
digitalWrite(LED_BUILTIN, HIGH);

// Wait for a second
delay(1000);

// Set the LED LOW
digitalWrite(LED_BUILTIN, LOW);

// Wait for a second
delay(1000);
}

The code hardly needs any description, it’s basically the Blink sketch you know and love (or at least know), using the constant LED_BUILTIN to represent the Arduino’s onboard LED, which is connected to pin 13. Note that the definition of LED_BUILTIN is remarked out, as the Arduino Uno framework already knows it.

As with the Arduino IDE, you’ll need to compile the code, and then upload it to your Arduino Uno.

Compiling the code is done using the PlatformIO Build button, which is a checkmark on the lower toolbar. Click on the checkmark and observe the progress in the terminal window. The code should compile successfully.

The next step is to upload it to the Arduino. Click on the key next to the Build key, the one shaped like an arrow. This is the Upload key. Once again I’ll point out that we never had to tell PlatformIO which USB port we had our Arduino connected to, it figures it out by itself.

PlatformIO will upload the compiled code to the Arduino, and you should see the familiar flashing LED as a result.

Leave a comment

Your email address will not be published. Required fields are marked *