Welcome to our tutorial on how to blink an LED using the Arduino platform and the VS Code PlatformIO IDE. In this guide, we will walk you through the process of setting up your development environment, connecting your Arduino board, and writing the code to blink an LED. We will also cover some basic concepts of digital electronics and the Arduino programming language. By the end of this tutorial, you will have a solid foundation to build upon and create more advanced projects with the Arduino and VS Code PlatformIO IDE.
In this tutorial it is shown how to install PlatformIO IDE in Visual Studio Code, how to write LED blink program in VS Code PlatformIO IDE, how to compile and upload to an Arduino board using PlatformIO IDE, and how to use the serial monitor in PlatformIO. The LED that is used here is connected to digital pin 7 of Arduino Uno.
PlatformIO and VS Code(Visual Studio Code)
VS Code(Visual Studio Code) is a popular and an open source source-code editor made by Microsoft. It supports almost all coding language from high language such as C/C++, web code languages, JavaScript, html,css etc to assembly languages. It has rich inbuilt features like intellicode(intelligent code completion), find in files is faster, live previews, auto-save, extentions, .net support etc.
PlatformIO is a an open source IDE(Integrated Development Environment) for rapid embedded software development with intelligent code completion, advanced debugging, terminals, supporting over 50 development platforms() supporting over 1000 boards and development kits(Arduino boards, espressif32, atmel avr, stm32 etc). The PlatformIO IDE is used as extension of VS Code source code editor and is useful for large scale and complex embedded firmware development for microcontrollers.
Installing PlatformIO in VSCode
First we need to install PlatformIO in VSCode from the extension.
Then search for PlatformIO and install it.
The PlatformIO installation process will start.
This may take sometime. After installation restart visual studio code IDE.
After restart look for the PlatformIO IDE icon as shown below and click on it to start the IDE.
Then from the PlatformIO IDE, click on open and then New Project as shown below.
Then type in some name for your project like "Arduino_LED_Blink", select Arduino Uno(or your board by typing in for search) as your board, the framework(which is selected automatically) and then save in default location or uncheck the location box and provide the directory where you want to save the project. Then click Finish and the project will be created.
Once the project is created you will see the project tree. From the project tree, click on the src folder icon and then the main.cpp. The main.cpp file will open in the code editor. The main.cpp contains minimal code that are required to write program with include <Arduino.h> statement and the standard Arduino sketch functions setup() and loop().
Then write the LED blink program.
The LED blinking program code is provided below.
#include <Arduino.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(7,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(7, HIGH);
delay(200);
Serial.println("HIGH");
digitalWrite(7, LOW);
delay(200);
Serial.println("LOW");
}
The next step is to compile and upload the code to the Arduino. The Arduino is to be connected to the PC/Laptop with the USB cable at this point. The following shows where the compile, upload and the serial monitor buttons are located in the PlatformIO IDE.
Click on the compile or compile and upload button to compile and upload the LED blink program code into the Arduino board. Once you have uploaded the code you will see "avrdude done. Thank you." message as shown below.
You can also see the message displayed on the serial monitor. The location of button to open the serial monitor in PlatformIO IDE was shown earlier. Click on it and you should see the message printed on the Arduino PlatformIO IDE serial monitor as shown below.
As can be seen in the above picture, the PlatformIO serial monitor is showing HIGH and LOW as coded in the LED blink program.
You should see the LED connected to Arduino pin 7 blinking. The following video shows all the process of installing the PlatformIO IDE in Visual Studio Code(VS Code), write Arduino program, compile and upload firmware to Arduino Uno, use the Arduino PlatformIO serial monitor and the LED blink on Arduino Uno board.
Tutorial Summary
In this tutorial, we covered the process of setting up an Arduino LED blink project using the VS Code PlatformIO IDE. We discussed how to install the PlatformIO extension in VS Code, how to connect an Arduino board, and how to write the code to blink an LED. We also explained the basic digital electronics concepts and the Arduino programming language used in this project. By the end of the tutorial, readers should have a solid understanding of how to set up and execute an Arduino LED blink project using the VS Code PlatformIO IDE and have a foundation to build more complex projects.
If you are interested in using using PlatformIO IDE as your programming IDE for NodeMCU see the tutorial NodeMCU PlotformIO IDE LED blink.