Helpful? spread the word!

In this tutorial, we will learn how to program NodeMCU boards. The first step is to connect with them installing the appropriated drivers. After that, we will learn how to use Arduino IDE to deploy our first program (sketch).

Is this guide only for NodeMCU boards?

No, the steps described here should be the same or quite similar for any ESP8266 development board. For a quick guide to all this jargon, have a look at An Introductory Guide to ESP8266, ESP-XX modules and NodeMCU.

Hardware

Amazon: NodeMCU V2
Amazon: NodeMCU V3

These are affiliate links. This means if you click on the link and purchase the promoted item, we will receive a small affiliate commission at no extra cost to you, the price of the product is the same. We would really appreciate your support to our work and website if this is fine for you.

Outline

  • Connecting to the NodeMCU
  • Programming Languages and Tools
  • Arduino Integrated Development Environment (IDE)
  • Writing and Deploying a Sketch
  • Managing Libraries
  • Further Reading

Connecting to the NodeMCU

Most NodeMCU boards are equipped with a micro USB port. You just need to use a USB to micro USB cable to connect your NodeMCU to the computer. When your NodeMCU is powered on for the first time, the build-in LED will quickly blink, then it will turn off. Bear in mind that some NodeMCU boards are preloaded with a program that makes the LED blink all the time.

Note: Make sure that your USB cable supports data transmission, not only power supply, especially if your are using a charging USB cable from another device. If you connect your NodeMCU with this kind of cables, you will see the LED blink when the device is powered on but you will be not able to communicate with it.

If you are using Linux, you will not need to install drivers, since they are already installed in most major Linux distributions. However, it is likely that you have to install drivers if your are using Windows or macOS. To install the appropriate drivers, you need to know the family of the USB bus convert chip of your NodeMCU board. This information is commonly given by the manufacturer. There are mainly two families: CP210X (NodeMCU V2) and CH340G/CH341 (NodeMCU V1 and V3).

Note: If you don’t know the version of your board or the chip family, have a look at An Introductory Guide to ESP8266, ESP-XX modules and NodeMCU to identify your board version and chip.

Hint: The CP210X chip family has a square shape, whereas the CH340G has a rectangular one.

NodeMCU V2 - CP2102 chip
NodeMCU V2 – CP2102 chip

NodeMCU V3 - CH340G chip
NodeMCU V3 – CH340G chip

You can find the drivers in the following links.

Programming Languages and Tools

There are mainly four options: Lua, MycroPython, c++ toolchain, and Arduino programming language (similar to c/c++). In this tutorial will be using Arduino programming language since it is powerful, easy to learn and all the programming and deploying tasks can be performed in the Arduino Integrated Development Environment (IDE). For Lua programming there are also some IDEs, such as ESplorer. General purpose IDEs can be used for deploying MycroPython programs to NodeMCU, such as the Pymakr Atom Package for Atom. Programming using the c++ toolchain is commonly done from the Command-Line Interface (CLI).

Arduino Integrated Development Environment (IDE)

Arduino IDE is available for Linux, Windows and macOS. The first task to do is to download and install the latest version according to your operating system. Once Arduino IDE is installed, we need to add a set of ESP8266 board definitions. To do that, go to File -> Preferences, then click in the Additional Board Manager URLs button in the Settings tab.

Arduino IDE - Preferences
Arduino IDE – Preferences

Next, add the following URL in one line.

http://arduino.esp8266.com/stable/package_esp8266com_index.json 
Arduino IDE - Additional Boards Manager URLs
Arduino IDE – Additional Boards Manager URLs

Now, you should see a list of ESP8266 boards in Tools -> Board in a menu section entitled ESP8266 Boards.

Select the appropriate board configuration from the list for your device. Two common options are the following.

  • NodeMCU 0.9 (ESP-12 module)This option is for NodeMCU V1 boards
  • NodeMCU 1.0 (ESP-12E module)This option is for NodeMCU V2 and V3 boards

When a board is chosen, its default configuration is loaded. The default configuration for NodeMCU 1.0 boards is shown below. You don’t need to know each particular detail, but if you are interested a brief description of each parameter is given in the following list.

  • Upload Speed: 115200 – This is the baud rate and determines the communication speed between the computer and the development board
  • CPU Frequency: 80 MHz – This is MCU frequency
  • Flash Size: 4M (no SPIFFS) – This is the NodeMCU flash size
  • Debug port: Disabled – Port for debugging messages
  • Debug Level: None – Type of messages you want to show when debugging
  • lvIP Variant: v2 Lower Memory – Version of the TCP/IP stack
  • VTables: Flash – Location of the virtual table
  • Exceptions: Disabled – Enable or disable exceptions
  • Erase Flash: Only Sketch – By default, when the flash is erased, the sketch is deleted but keeping the remaining content, for example the firmware.

You may need to manually select communication port in Tools -> Port if Arduino IDE can’t automatically detect it.

You can try to retrieve some information about your board (Tools -> Get Board Info), although sometimes not much information is given.

Arduino IDE - Board info
Arduino IDE – Board info

Now, let’s have a look at the main elements of Arduino IDE.

  • Menu – Different menus for managing sketches, boards and documentation
  • Button bar – Quick access to the most important actions
  • Serial monitor – Access to the information printed by our board
  • Text editor – The place where the code of our sketches is written
  • Messages – Main Arduino IDE messages, such as uploading sketch, etc.
  • Console – Messages in detail given by Arduino IDE
Arduino IDE
Arduino IDE

The available actions in the button bar are the following.

  • Verify – Check that the sketch code is correct
  • Upload – Deploy the sketch to the board
  • New – Create a new sketch
  • Open – Open a sketch
  • Save – Save the current sketch

Writing and Deploying a Sketch

A sketch in Arduino IDE always has two main functions: setup and loop.

The code inside the setup function is executed only once when the board is powered on or after pressing the reset button. The code inside the loop function is executed indefinitely in loops.

Let’s now see a simple sketch that blinks the built-in ESP-12E LED in our NodeMCU board and print the current state of the LED in the serial monitor.

First, create a New sketch and paste the following code.

unsigned int state = LOW; // Variable for the state of the LED

void setup() {
  Serial.begin(115200);         // Set the communication speed
  pinMode(LED_BUILTIN, OUTPUT); // Set the GPIO pin as an output
}

void loop() {
  digitalWrite(LED_BUILTIN,state);   // Set the LED state
  Serial.print("LED state: ");       // Print "LED state: "
  Serial.println(state);             // Print the state variable 
                                     // plus a carriage return 
  state = state == LOW ? HIGH : LOW; // Change state LOW <-> HIGH
  delay(1000);                       // Wait for a second
}

The example is easy to follow, additionally all the lines are commented. The following list provides a brief explanation about the code.

  • Line 1. The state of the LED is stored in the state variable.
  • Line 5. There is a constant that define the pin of the built-in LED (LED_BUILTIN), so we don’t have to remember the pin number.
  • Line 5. It is required to set the pin mode as output (OUTPUT) in the setup (pinMode function) to write on it (digitalWrite function, Line 9).
  • Line 13. This code is a compact way to define an if block.
  • Line 13. Pin levels can be described by the LOW and HIGH constants.

To deploy this sketch press the Upload button. Once the code is loaded in your board, you will see the LED blinking and the LED state printed in the serial monitor.

Arduino IDE - Example results
Arduino IDE – Example results

You can inspect more example for your board in File -> Examples.

Managing Libraries

The Arduino IDE has many useful libraries that you can use in your sketches. Go to Tools -> Manage Libraries to search, install and update your libraries.

Arduino IDE - Library manager
Arduino IDE – Library manager

If you want to use a library in your sketch, select it in Sketch -> Include Library. This action will include the library header files references in your sketch. For example, including the ESP8266WiFi library will add the following header file references. You can keep only those you need.

#include <BearSSLHelpers.h>
#include <ESP8266WiFiType.h>
#include <WiFiClientSecureAxTLS.h>
#include <WiFiUdp.h>
#include <ESP8266WiFiGeneric.h>
#include <WiFiServerSecureBearSSL.h>
#include <ESP8266WiFi.h>
#include <CertStoreBearSSL.h>
#include <WiFiClient.h>
#include <WiFiServerSecure.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFiSTA.h>
#include <WiFiServerSecureAxTLS.h>
#include <WiFiClientSecureBearSSL.h>
#include <WiFiServer.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiAP.h>

You can also use libraries in your sketches that are not available in the Arduino library manager. They must be in ZIP format. You can use import them in Sketch -> Include Library -> Add .ZIP Library.

Further Reading

If you want to keep learning about programming ESP8266 boards, the Arduino programmin laguage and ESP8266 libraries, I recommend the following content.

Leave a Reply

avatar
2000
  Subscribe  
Notify of
shares