NodeMCU PlotformIO IDE LED blink

 Here it is illustrated how you can use the PlatformIO IDE to write program code for NodeMCU, upload the program and how to use the serial monitor in Platform IDE. For this basic tutorial a LED blink program that blinks a LED connected to GPIO4 pin or D2 of NodeMCU ESP8266.

PlatformIO & its advantages

PlatformIO is a new integrated development enviroment(IDE) for writing firmware/program for microcontrollers and microcontroller boards. It has advanced features which is not available in Arduino IDE. It has for example automatic code completion which makes it easier to write functions and variables. As soon as known functions and variable name is detected it gives you hint and you can just use space key on the keyword for auto completion if that's the function or variable you were about to type. So this feature helps to write code faster. Also it gives you the function prototype: what arguments it takes, the data type of the parameter. So if you for example forgot the name of the function you get help which can be useful in complex and large program with lots of libraries. Another advantages is that the PlatformIO gives you error if you typed in wrong function name, declaration, variable not found and other typo. There are more other features such as debugger that will assist you in writing your program. The PlatformIO supports over thousands of microcontroller boards and microcontrollers and comes with popular pre-installed libraries so that you don't have to download it. For example, when you write your first program for NodeMCU ESP8266 in Arduino IDE you need to set up the library and install the library as shown in the tutorial  but with PlatformIO the library is pre-installed. 

NodeMCU PlotformIO IDE LED blink Circuit

Here we will blink a LED connected to GPIO pin 4 or D2 of NodeMCU ESP8266. The following shows the circuit which shows the LED connected to the pin aforementioned.

NodeMCU LED Blink

PlatformIO IDE Installation

To install PlatformIO IDE you need to have Visual Studio Code(VSCode) which is open source and free to download from the following site.

https://code.visualstudio.com

Next you have to open VSCode and go to extension and search and install PlatformIO extension. 

vscode extention

install platformio

 After first installation you need to restart Visual Studio Code. When you reopen VSCode go and open the PlatformIO.

new platformio project

NodeMCU project in PlatformIO

 Click on the New Project to create a new create NodeMCU project in Platform IO. Name the project with any name you. Here the project name is NodeMCU_LED_BLINK. In the Board section, type in NodeMCU in the board selection field and then select the NodeMCU 1.0(ESP-12E Module) or the NodeMCU you are using. In the Framework field select Arduino. Then choose either default location to save the project to default directory or uncheck the location field and browse to the folder where you want to save the project.

create NodeMCU project in Platform IO

Now the new NodeMCU project in Platform IO will be created.

Writing NodeMCU program in PlatformIO

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().

new program code file

NodeMCU LED blink Program

In the code editor, write the following NodeMCU LED blink program.

#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");
}

This is similar to led blink Arduino code. This is shown below.

LED blink program

Compile, Upload and Serial Monitor

The next step is to compile only, compile and upload and watching any messages in serial monitor. The compile, upload and serial monitor buttons in PlatformIO are located at the bottom as shown in the picture below.

PlatformIO compile upload and serial monitor buttons

The board com port is automatically detected by the PlatformIO if there is only one NodeMCU is connected to your PC/Laptop with USB. The video down below shows this board configuration step if you encounter problem.

Now you can click compile and upload code to your NodeMCU. Once the upload is completed you should see "Hard resetting via RTS pin ..." which is build completed message in the terminal.

compile and upload code to NodeMCU with PlatformIO

To view the message on the PlatformIO serial monitor click on the serial monito button as shown in the previously shown picture above. You should see the message HIGH and LOW alternatively displayed on the PlatformIO IDE serial monitor as shown below.

PlatformIO Serial Monitor Animation

At this point the LED connected to the NodeMCU should be blinking. See NodeMCU LED blink tutorial for basic of blinking a LED with NodeMCU. 

So in this tutorial we showed how how to install PlatformIO IDE in Visual Studio Code, how to create a new NodeMCU project, how to write NodeMCU code in the platformIO code editor, how to compile and upload code, and how to use the serial monitor in PlatformIO IDE(Integrated Development Ennvironment).

Video on NodeMCU PlotformIO IDE LED blink

Watch the following video that shows step by step how to create a new NodeMCU project, how to write NodeMCU code in the platformIO code editor, how to compile and upload code, and how to use the serial monitor in PlatformIO IDE.


 For Arduino programming in PlatformIO see the tutorial Arduino LED blink with PlatformIO.

Post a Comment

Previous Post Next Post