Getting started with NodeMCU
In this article, we will learn about the NodeMCU microcontroller. By the end of this article, you should have a basic understanding of what a microcontroller is, what NodeMCU is, and its use in the Internet of Things. You will also learn step-by-step procedures for building a simple program to control the blinking of LEDs using NodeMCU.
As a prerequisite, a little knowledge about the C programming language would be helpful in being able to follow this article.
Table of contents
Microcontroller
According to Wikipedia, a microcontroller (MCU for microcontroller unit) is a small computer on a single metal-oxide-semiconductor (MOS) integrated circuit (IC) chip. A microcontroller contains one or more CPUs (processor cores) along with memory and programmable input/output peripherals.
In simple words, a microcontroller can be called a mini-computer with an built-in CPU processor, memory, and other I/O peripherals.
Microcontrollers come with different configurations and architectures depending on the application it is used for. They are being used extensively in various industries like manufacturing, robotics, automation, the internet of things, and so on.
For example, microcontrollers are used as embedded devices in appliances like ACs and washing machines which enables humans to interact with the devices (digitally). They contain built-in programs used for simplifying human tasks.
NodeMCU
In this article, we will be focusing on the NodeMCU microcontroller. NodeMCU stands for Node MicroController Unit. The below image shows a NodeMCU microcontroller board.
According to this article, NodeMCU is an open-source Lua based firmware and development board specially targeted for IoT based applications. It includes firmware that runs on the ESP8266 Wi-Fi SoC from Espressif Systems, and hardware that is based on the ESP-12 module.
Features
NodeMCU ESP12 module contains Tensilica Xtensa® 32-bit LX106 RISC microprocessor.
Operates at 80 to 160 MHz.
Operating voltage of 3.3V.
128KB RAM and 4MB of Flash memory.
Contains built-in 802.11b/g/n WiFi transceiver.
Contains 17 GPIOs (General Purpose Input Output). GPIO is a standard interface that connects the microcontroller to other electronic devices. For example, LEDs, Buzzers and other sensors are GPIOs.
Provides serial communication via the UART (Universal Asynchronous Receiver-Transmitter) interface. UART is a physical interface that transmits the data between two nodes using a data bus. More about UART can be understood here.
All the above components with several additional features can be seen in this pin diagram.
When compared with other microcontrollers like Arduino, we see that:
NodeMCU comes with 128KB RAM, whereas Arduino has 2KB RAM. With better RAM memory, parallel processing of tasks can be achieved seamlessely.
NodeMCU has 4MB ROM, whereas Arduino has 32KB ROM. With bigger ROM size, NodeMCU allows us to store more information without any need of external storage devices.
NodeMCU comes with a MicroUSB port, but Arduino requires a USB-B type. Usage of MicroUSB is more extensive, and ensures better connectivity.
The development board for NodeMCU is much smaller when compared with others. The size of the board is a major factor to consider while building electronic appliances.
Step by step guide
Setup
Before we get started with building a simple project, it's required to set up the Arduino IDE for the NodeMCU board as mentioned in this tutorial.
Requirements
On following this tutorial, ensure that you have performed the below-mentioned tasks:
Installed Arduino IDE.
Added NodeMCU ESP2866 in the Arduino IDE board manager.
Connect the NodeMCU ESP2866 hardware to the computer using a USB cable.
Verify if the connection is established, by heading over to the
Device Manager
and check forCOM5
port availability under thePorts
section.
Code for blinking LEDs
NodeMCU ESP2866 board has 2 inbuilt LED's with pins GPIO2
and GPIO16
.
To assign the LEDs to the GPIO pins, we have to create a variable and assign the respective pin, as shown below.
1int LED1 = 2; // Assign LED1 to pin GPIO22int LED2 = 16; // Assign LED2 to pin GPIO16
Now, inside the setup()
method, we have to set both the LEDs as OUTPUT
pins using the pinMode()
function. This function maps the pin to the type of interface. The type of interface can either be INPUT
or OUTPUT
.
As a demonstration, we will use both the LEDs as OUTPUT
interfaces as shown below:
1void setup() {2 // Initialize GPIO2 and GPIO16 as output interfaces3 pinMode(LED1, OUTPUT); // Set pin 2 as OUTPUT4 pinMode(LED2, OUTPUT); // Set pin 16 as OUTPUT5}
Next, in the loop()
method, we write code that runs continuously until the exit conditions are met. For our example, it is expected to light up LEDs alternatively with a delay of 1 second each.
1// This function runs forever2void loop() {3 digitalWrite(LED1, LOW); // LED1 - OFF4 digitalWrite(LED2, HIGH); // LED2 - ON5 delay(1000); // Delay of 1 second6 digitalWrite(LED1, HIGH); // LED1 - ON7 digitalWrite(LED2, LOW); // LED2 - OFF8 delay(1000); // Delay of 1 second9}
Finally, our code is ready for compilation and upload. To compile your code, press Ctrl + R
. After compilation, the compiled binary codes are uploaded to the NodeMCU hardware by pressing Ctrl + U
. You will see the screen as shown below:
On following the above-mentioned steps correctly, you can see both the LEDs blinking alternatively with a delay of 1 second each.
Conclusion
To conclude, we have learned what microcontrollers are, what a NodeMCU is, and its usage in the Internet of Things. We also learned a step by step procedure for building a simple project for blinking LEDs.
To summarize:
We learned about microcontrollers.
We learned about NodeMCU and its uses in the Internet of Things.
We built a project for the blinking of LEDs.
Further reading
Peer Review Contributions by Saiharsha Balasubramaniam