0% found this document useful (0 votes)
7 views16 pages

NodeMCU ESP8266 IoT Platform Guide

The document provides an overview of the NodeMCU ESP8266, an open-source IoT platform based on the ESP8266 Wi-Fi module, detailing its specifications, communication ports, and programming capabilities. It covers the NodeMCU firmware, Wi-Fi functionalities, and how to set up the Arduino IDE for programming, including examples of code for connecting to Wi-Fi and interfacing with LEDs and switches. Additionally, it includes assignment questions to reinforce understanding of the material.

Uploaded by

ujwalsali93
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

NodeMCU ESP8266 IoT Platform Guide

The document provides an overview of the NodeMCU ESP8266, an open-source IoT platform based on the ESP8266 Wi-Fi module, detailing its specifications, communication ports, and programming capabilities. It covers the NodeMCU firmware, Wi-Fi functionalities, and how to set up the Arduino IDE for programming, including examples of code for connecting to Wi-Fi and interfacing with LEDs and switches. Additionally, it includes assignment questions to reinforce understanding of the material.

Uploaded by

ujwalsali93
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IoT APPLICATIONS[315341]

UNITWISE NOTES BY: [Link]

UNIT 2: Fundamental of NodeMcU

2.1: NodeMCU ESP8266

The NodeMCU ESP8266 is a low-cost, open-source IoT platform based on the


ESP8266 Wi-Fi module. It includes firmware that runs on the ESP8266 Wi-Fi SoC
from Espressif Systems and hardware based on the ESP-12 module. It's widely
used in home automation, wireless sensors, and smart devices.

ESP8266 Microcontroller:

It is a low cost board NodeMCU board uses ESP8266 E-12 microcontroller. Like
Arduino, NodeMCU also has GPIO pins for interfacing sensors, actuators and
other devices.
The ESP8266 specifications include:

 17 GPIOs
 SPI
 I2C (implemented on software)
 I2S interfaces with DMA
 UART
 10-bit ADC
 8-bit DAC

Pin-Mapping at a glance:
2.2 NodeMCU communication port: UART, I2C,SPI

NodeMCU has one UART, One I2C and one SPI port in which pins are as follows:

UART Rx, Tx
D5 : SCLK
D6 : MISO
SPI
D7 : MOSI
D8 : CS
D1: SCLK
I2C
D2 : SDA

UART (Universal Asynchronous Receiver/Transmitter):- The ESP8266,


commonly used in NodeMCU boards, has two UART (Universal Asynchronous
Receiver/Transmitter) interfaces: UART0 and UART1. UART0 is a full-duplex
serial communication interface, allowing for both sending and receiving data,
while UART1 is typically used for transmitting data (e.g., logging) due to
limitations in receiving capability.

UART0: Pins: TXD0 (GPIO1), RXD0 (GPIO3). Function: Full-duplex serial


communication (send and receive). Default Baud Rate: 115200.

UART1: Pins: TXD1 (GPIO2). Function: Primarily used for transmitting data (e.g.,
printing logs). UARTs are used for sending and receiving data between devices,
enabling various applications like data logging, sensor data acquisition, and remote
control.
2.3 NodeMCU ESP8266 Development board and its pin configuration:
2.3.1 NodeMCU Firmware: The NodeMCU firmware is a lightweight operating
system that runs on the ESP8266 chip. It allows you to program the ESP8266 using
the Lua scripting language or other tools like Arduino IDE or MicroPython.

1. NodeMCU Lua Firmware:

It is based on the eLua project which is a Japanese language and It Uses Lua
scripts to control hardware

2. Arduino-Compatible Firmware: ESP8266 runs code written in the Arduino


programming language Use the Arduino IDE with the ESP8266 board package

3. MicroPython Firmware Python 3-like scripting environment and it is Excellent


for quick testing and prototyping.

2.3.2 NodeMCU 8266 WiFi: The NodeMCU ESP8266 is a Wi-Fi-enabled


microcontroller board based on the ESP8266 SoC. It allows IoT devices to connect
to the internet wirelessly and can be programmed using Lua, Arduino (C/C++), or
MicroPython. Node MCU 8266 uses wifi 802.11b/g/n standard.

WiFi Capabilities of NodeMCU ESP8266

Wi-Fi can be used as Station,Access point or both.


1. Wi-Fi As station:

In WiFi, station mode (STA) refers to a device acting as a client and connecting to
an existing WiFi network, like a smartphone or laptop connecting to a home router.
It's the most common mode for WiFi devices and allows them to join and
communicate with other devices on the same network.

Mode 1:Connect to WiFi Network (STA Mode)

Program to connect NodeMCU to WiFi Network (STA Mode)

#include <ESP8266WiFi.h>

const char* ssid = "YourSSID";

const char* password = "YourPassword";

void setup() {

[Link](115200);

[Link](ssid, password);

[Link]("Connecting");

while ([Link]() != WL_CONNECTED) {

delay(500);

[Link](".");}

[Link]();

[Link]("Connected! IP address:");

[Link]([Link]());

void loop() {

// Your code here

}
2. Wi-Fi as an access point:

In access point (AP) mode, a NodeMCU device creates its own Wi-Fi network,
allowing other devices to connect to it. This is different from station mode, where
the NodeMCU joins an existing Wi-Fi network.

Mode 2. Create an Access Point (AP Mode)

Program to connect NodeMCU to WiFi Network (AP Mode)

#include <ESP8266WiFi.h>

void setup() {

[Link](115200);

[Link]("ESP8266_AP", "12345678");

[Link]("Access Point Started");

[Link]("IP: ");

[Link]([Link]());

void loop() {

// Your code here

}
Arduino Integrated Development Environment (IDE) Setup for NodeMCU

Install Arduino IDE:

Download and install the latest version of the Arduino IDE from the official
Arduino website.

Add ESP8266 Board Manager URL:

Open the Arduino IDE.

Go to File > Preferences.

In the "Additional Boards Manager URLs" field, add the following URL:
[Link]

Click "OK".

Install ESP8266 Board Package:

Go to Tools > Board > Boards Manager....

Search for "ESP8266" and install the "ESP8266 by ESP8266 Community" package.

Select NodeMCU Board:

Go to Tools > Board > ESP8266 Boards.

Select your specific NodeMCU board (e.g., "NodeMCU 1.0 (ESP-12E Module)").

Select Serial Port:

Connect your NodeMCU board to your computer via a micro USB cable.

Go to Tools > Port and select the correct COM port assigned to your NodeMCU
(you can find this in your system's Device Manager).

Creating, Compiling, and Uploading Programs

Create a New Sketch:

Open a new sketch in the Arduino IDE by going to File > New.

Write your Arduino code (sketch) in the editor. For example, a basic blink sketch:

C++
void setup() {

pinMode(LED_BUILTIN, OUTPUT);

void loop() {

digitalWrite(LED_BUILTIN, HIGH);

delay(1000);

digitalWrite(LED_BUILTIN, LOW);

delay(1000);

Compile the Program:

Click the "Verify" button (checkmark icon) in the toolbar. This will compile your
code and check for any syntax errors. The output window at the bottom will
display compilation progress and any errors.

Upload the Program:

Click the "Upload" button (right-arrow icon) in the toolbar. This will compile the
code (if not already done) and then upload it to your NodeMCU board.

The output window will show the upload progress. Once complete, it will display
"Done uploading."

The built-in LED on your NodeMCU should now behave according to your
uploaded program (e.g., blinking if you uploaded the blink sketch).
2.5 Applications using NodeMCU ESP8266 and Arduino IDE. (Use of

functions, string, array, timer, I/O function, PWM, interface LED & switch)

2.5.1 Arduino C/C++ program functions:

setup() : This function is used to execute the instructions which has to be run only
once . for example Setting a particular pin as OUTPUT we have
pinMode(D4,OUTPUT)

loop() : After setup() function exits (ends), the loop() function is executed
repeatedly in the main program. It controls the board until the board is powered off
or is reset. It is analogous to the function while(1). For example blinking LED
continuously with one second on off delay.

2.5.2 I/O Functions:

pinMode() Function

The pinMode() function is used to configure a specific pin to behave either as an


input or an output.

Syntax:

pinMode(pin, mode);

pin: Specifies the GPIO pin number (e.g., D0, D1, etc.).

mode: Defines the pin mode, either INPUT or OUTPUT.

Eg.

pinMode(D0, OUTPUT);

pinMode(D1, INPUT);

digitalWrite() function

The digitalRead() function reads the digital input from a GPIO pin.

Syntax:

int value = digitalRead(pin);

pin: Specifies the GPIO pin number. Returns HIGH or LOW depending on the
pin's state
Analog Input

NodeMCU has one analog input pin (A0) and analogRead() function reads the
analog voltage.

Syntax:

int analogValue = analogRead(A0);

A0: Specifies the analog input pin.

It Returns an integer value representing the voltage level (0-1023) since it is having
10 bit ADC so resolution is 210 =1024 but from 0-1023.

2.5.3 Time functions:

Arduino includes functions for measuring elapsed time and also for pausing the
sketch.

unsigned long millis()

Returns the number of milliseconds that have passed since the sketch started.

Example:

duration = millis()-lastTime; // computes time elapsed since "lastTime"

delay(ms)

Pauses the program for the amount of milliseconds specified.

2.5.4 String In arduino:

String creates an instance in arduino which is like a datatype. For creating any
string it can be created by following ways:

1. Character Arrays (C-style strings):

These are arrays of characters terminated by a null character (\0). They are
lightweight and efficient but require manual handling.

char myString[] = "Hello, Arduino!";

[Link](myString);
2. String Objects (Arduino String Class):

These are objects of the String class, which provide more flexibility and built-in
functions for string manipulation. However, they consume more memory and can
lead to memory fragmentation in long-running programs:

String myString = "Hello, Arduino!";

[Link](myString);

Reading string using Serial monitor:

ReadString()function:

Using a readstring() function the string which is entered by keyboard from the user
to the serial monitor can be read and stored. As we know that [Link] only
stores a byte or character, we cannot accept a collection of characters I.e. string. In
order to achieve this the readString() function is used.

For example:

Void setup()

[Link](9600);

Void loop(){

String mystring;

If([Link]()>0)

Mystring = [Link]();

[Link](Mystring);

}
2.5.3 NodeMCU interfacing with switch & LED

D2
2 D5
2

NodeMCU

3.3V

Program:

int ledPin = D2;

int buttonPin =D5;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

void loop() {

int buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn the LED ON

} else {

digitalWrite(ledPin, LOW); // Turn the LED OFF

2.5.5 PWM Functions: Pulse Width Modulation (PWM) is a technique by which


the width of a pulse is varied while keeping the frequency of the wave constant.

Arduino function for NodeMCU PWM

analogWrite(pin, dutycycle): Enables software PWM on the specified pin. duty


cycle is in the range from 0 to PWMRANGE, i.e. 255 by default.

Example:

// Pins

const int led_pin = D2;

void setup() {

pinMode(led_pin, OUTPUT);

void loop() {

// Set LED brightness

analogWrite(led_pin, 255);

Pins are D2,D5,D6,D8


Assignment No 2 Questions:

Two Marks Questions:

1. List any four specifications of NodeMCU board


2. State the use of analogWrite() and pinMode() function
3. State the function of Void setup() with example
4. State the function of Void Loop() with example
5. List the UART and PWM pins of NodeMcU board
6. State the use of analogRead() and digitalRead() function
7. Enlist SPI and I2C pins available in nodeMCU.
8. State the use of readString() function with example.

Four Marks Questions:

1. Explain the steps for installation of arduino IDE and NodeMCU package.
2. Draw interfacing diagram of LED and switch with NodeMCU and write the
program to turn ON the LED when switch is pressed.
3. Draw and explain the architecture of NodeMCU with pin configuration
4. Explain with syntax for the following :- a. AnalogRead() b.
[Link]().
5. Explain the time functions and strings used in arduino IDE for programming
nodeMCU
6. Explain the following functions: 1. AnalogWrite()
2. pinMode()
3. Millis()
4. digitalWrite() with syntax and examples

You might also like