Hands on the NodeMCU ESP8266 (Running your first sketch !)

Hamza Jeljeli
5 min readApr 4, 2020

Hello there ! my name is Hamza Jeljeli, I’am a Java, C# and Microsoft Certified Associate — Azure Developer. And recently, I’ve started working with devices like the NodeMCU ESP8266 for small personal projects. Today, we will be, together, discovering and writing our first lines of code to the NodeMCU ESP8266 Module.

Before beggining this tutorial, Being familiar with the basics of the programming using C or C++ is highly appreciated.

What is a NodeMCU ESP8266 ?

NodeMCU is a low-cost open source IoT platform. Sold at a price of 15$ (Depending on the country you are living in), it offers to you Arduino-like hardware. Some variants offers also embeeded Wi-Fi (IEEE 802.11b/g/n) capablilties.

The NodeMCU ESP8266 Module

Setting a Dev-Environment ?

Well, there are no special requirements, you just need to have a computer with a minimum of 2GB of RAM, 1GB hard disk space and an Internet connection for downloading additional packages.

We will just need to download and install Arduino IDE from here (or from the Windows Store) on your computer.

The Arduino IDE is an open-source tool for writing and uploading code to Arduino-Based boards painlessly.

Starting and configuring the Arduino IDE !

The Arduino IDE icon under Microsoft Windows

You just need to double click on the Arduino IDE icon, and voila ! you are ready to go !

Once the IDE is running, you will end up with something similar to this !

The Arduino IDE main interface.

Notice that there are two functions :

  • setup() : This function is called when the sketch starts. It can be used to initialize variables, pin modes, etc ..
  • loop() : This function is called after the execution of the setup() function. This function will do all the work that will be done repeatedly. For example, connecting to Wi-Fi, changing the state of a led, etc ..

First, we need to configure the IDE to recognize the NodeMCU ESP8266 module, To do so, click on File >> Preferences.

on the Additional Board Manager URLs field, please paste the following link : https://arduino.esp8266.com/stable/package_esp8266com_index.json

The Preferences Menu

These packages are managed by the ESP8266 Community Forum under their Github Repo, Please consider supporting them with a donation for their amazing work.

Now you may click on OK once the link is added.

Now we need to install the ESP8266 Board Tools, to do this, please click on Tools >> Board >> Board Manager. In the Board Manager interface, search for esp8266. Once found click on Install.

The boards manager installing the ESP8266 Board tools

Note: If you didn’t find it, please check your internet connectivity and if the above steps are done correctly.

When the download is finished. You may close the Board Manager Window.

Now, we need to indicate to the Arduino IDE that he will be dealing with an ESP8266 Module.

To do this, we must click on Tools >> Board and then choose Generic ESP8266 Module.

Now, if your ESP8266 Module is not plugged into your computer yet, you must plug it now so your computer can identify it and install the required drivers for it.

At this point, you must select the port of the ESP8266 Module in the Arduino IDE. You need to click on Tools >> Port and select the COM port which is associated to your ESP8266 Module (COM3 in my case).

Now, we are ready to write our first lines of code !

Writing and testing your first sketch !

In this tutorial, we will be testing this sample code :

#include <ESP8266WiFi.h>

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(“Setup Done.”);
}

void loop() {
// put your main code here, to run repeatedly:
int i = 0;
while (true)
{
i++;
Serial.print(“Running since “);
Serial.print(i);
Serial.println(“ seconds”);
delay(1000);
}
}

This sample code will print, every second, since how much the sketch is running in seconds.

Once the code has been written, you can test it by clicking on Sketch >> Upload.

The Arduino IDE will take some time compiling and uploading the Sketch to the module. you can check the compilation output and status in the log console.

The log console showing the compilation output

To check if the sketch is successfully running, Click on Tools >> Serial Monitor.

In the Serial Monitor Window, you must be able to see an output similar to the following image.

The Sketch output

In later tutorials, we will be building sketches which will be more complex, so stay tuned for part 2!

Final Words …

  • The source code of this tutorial can be found here.
  • Working with the NodeMCU ESP8266 module is very easy.
  • If you ever encountred a problem, you can visit the Arduino Forums or the ESP8266 forums !
  • Feel free to contact me through my Online Visit Card !

--

--