NodeMCU Tutorial
NodeMCU Tutorial
Note: All IO devices, sensor modules of Ardunio All-In One board can be used in this IoT
board without any changes.
S- Signal
V – 5V
G - Ground
NodeMCU Board from Third Party
NodeMCU ESP8266
D0 GPIO 16
D1 GPIO 5
D2 GPIO 4
D3 GPIO 0
D4 GPIO 2
D5 GPIO 14
D6 GPIO 12
D7 GPIO 13
D8 GPIO 15
D9 GPIO 3
D10 GPIO 1
LED_BUILTIN = D0 (GPIO 16)
Details about On-Board LEDs and Switch:
So users can make use of theses On-Board Switch and LEDs for exercises.
Please, note that, To Switch ON LED, give Logic LOW and to Switch OFF LED, give logic
HIGH. (Negative Logic is used)
When Flash Button is not Pressed (IDLE state), D3 will be HIGH and when Flash Button is
Pressed (Active State), D3 will be LOW.
Program to Blink LED_BuiltIN
void setup() {
void loop() {
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(2000); // Wait for two seconds (to demonstrate the active low LED)
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
Instructions to install NodeMCU in Arduino:
https://www.youtube.com/watch?v=cNqNhR_2mp0
Exercises:
#include "ESP8266WiFi.h"
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
void loop() {
Serial.println("scan start");
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
} else {
Serial.print(n);
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
delay(10);
Serial.println("");
delay(5000);
Sample output (Note that COM Port is configured to work at baud rate of 115200 )
Digital IOs
Exercise 2: LED Blinking
/* to
void setup() {
void loop() {
digitalWrite(D0, HIGH);
delay(1000);
digitalWrite(D0, LOW);
delay(1000);
}
Exercise 3: Blinking of Bi-Color LEDs (Alternate blinking of Red and Green LEDs.)
Program:
void setup() {
void loop() {
digitalWrite(D0, HIGH);
digitalWrite(D1, LOW);
delay(1000);
digitalWrite(D0, LOW);
digitalWrite(D1,HIGH);
delay(1000);
Sample Output: Red LED connected to port D0 is ON, Green LED connected to port D1 will be OFF and
Green LED will be ON for 1 second. This process will be repeated till power is applied.
Exercises 4: Switch and LED (INPUT and OUTPUT Concept)
When Switch (Push Button) is pressed, LED will be switched ON and when Switch (Push
Button) is released, LED will be switched OFF.
Program:
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
} else {
digitalWrite(ledPin, LOW);
Sample Output:
When Switch is pressed, LED gets switched ON and the LED will be switched OFF when the switch gets
released.
Exercises 5: Sensor Interface to ADC Channel and sending equivalent digital data through
serial interface.
When POT is rotated, analog value applied to Analog Channel A0 will be varied from 0V to
5V and its equivalent digital value (in decimal form) will be displayed in the serial monitor..
Program:
void setup() {
Serial.begin(9600);
void loop() {
Serial.println(sensorValue);
RS D0
EN D1
Data 4 D2
Data 5 D5
Data 6 D6
Data 7 D7
/*
LiquidCrystal Library - Hello World Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many
of them out there, and you can usually tell them by the 16-pin interface. This sketch prints "Hello
World!" to the LCD and shows the time.
The circuit:
* 10K resistor:
#include <LiquidCrystal.h>
void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
void loop() {
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
Output:
Hello World
1
Exercise 7:
Digital Voltmeter
Potentiometer POT is connected to Analog Channel A0 and LCD wiring as per the previous exercises.
Program:
#include <LiquidCrystal.h>
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
void loop() {
// (note: line 1 is the second row, since counting begins with 0):
lcd.print("Digital Voltmeter!");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(volt);
Serial.println(volt);
delay(100);
Output:
Digital Voltmet
1.25
Exercise 8:
Digital Therometer
Temperature Sensor LM35 is connected to Analog Channel A0 and LCD wiring as per the previous
exercises.
Program:
#include <LiquidCrystal.h>
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
void loop() {
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,0);
lcd.print("Digital Thermometer!");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(TempinC);
Serial.println(TempinC);
delay(1000);
Output:
Digital Thermome
38.78
Exercise 9: BlueTooth
Analog Value at A0 will be sent to mobile phone through Blue Tooth. LED connected to Digital Output DO
will be controlled (ON/OFF) by sending a Character (A to ON and a to OFF) from Mobile phone through
Blue Tooth
Program:
#include <SoftwareSerial.h>
char c;
#define LED D4
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
pinMode(LED,OUTPUT);
Serial.println("Goodnight moon!");
mySerial.println("Hello, world?");
//Serial.println("HI...");
int a =analogRead(A0);
mySerial.println(a);
delay(200);
// if (mySerial.available()) {
// Serial.write(mySerial.read());
// }
if (mySerial.available()) {
c = mySerial.read();
if(c == 'A')
digitalWrite(LED,HIGH);
if(c == 'a')
digitalWrite(LED,LOW);
In your Android Mobile phone, install BlueTooth terminal HC-05 available in Google play store.
Select the paired device RiyassaBT1. (BlueTooth module HC-05 is already renamed as RiyassaBT1 and it is
paired with our mobile phone – default pairing code is 1234)
Run the BlueTooth terminal HC-05 App and select RiyassaBT1 as the device, you will be able to see the
value of Analog POT in The terminal of BT HC-05 App.
Both Transmission and reception through Bluetooth interface of NodeMCU can be understood through
this program.
https://www.youtube.com/watch?v=CW0ZE_f2g30
Exercise 10
Connect + pin of 3.3 V of NodeMCU and – pin of DHT 11 to Gnd of NodeMCU and Out pin of DHT11 to
D4 (GPIO 2) of Node MCU.
Program
#include "DHT.h"
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
void loop() {
delay(2000);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
return;
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
ThingSpeak
#include <ESP8266WiFi.h>
float resolution=3.3/1023;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.disconnect();
delay(10);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
delay(500);
Serial.print(".");
Serial.println("");
Serial.println(ssid);
Serial.println();
void loop() {
if (client.connect(server,80)) {
tsData +="&field1=";
tsData += String(temp);
tsData += "\r\n\r\n";
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
Serial.print("Temperature: ");
Serial.print(temp);
client.stop();
Serial.println();
delay(15000);
}
https://www.youtube.com/user/master4hereever/search?query=NodeMCU
Riyasaatest
Drowpass1
Write API Key GY547B219LGNHOV6
Read API Key: C92CS35TPKYI2F3R
PROGRAM
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
char writeAPIKey[] = "GY547B219LGNHOV6"; // Change this to your channel Write API Key.
const unsigned long postingInterval = 20L * 1000L; // Post data every 20 seconds.
int connectWifi();
void updateTemp();
unsigned long lastConnectionTime = 0;
void setup()
Serial.begin(115200);
Serial.println("Start");
connectWifi();
Serial.println("Connected to wifi");
ThingSpeak.begin(client);
void loop()
// If interval time has passed since the last connection, write data to ThingSpeak.
updateTemp();
ThingSpeak.setField(1, Temp);
ThingSpeak.writeFields(channelID, writeAPIKey);
lastConnectionTime = millis();
int connectWifi()
WiFi.begin(ssid, pass);
Serial.println("Connecting to WiFi");
delay(2500);
Serial.println("Connected");
void updateTemp()
Serial.println(Temp);
}
% Read temperature data from a ThingSpeak channel over the past 24 hours
% to calculate the high and low temperatures and write to another channel.
% Channel 12397 contains data from the MathWorks Weather Station, located
% in Natick, Massachusetts. The data is collected once every minute. Field
% 4 contains temperature data.
% Read temperature data for the last 24 hours from the MathWorks weather
% station channel. Learn more about the thingSpeakRead function by going to
% the Documentation tab on the right side pane of this page.
'numDays',1,'ReadKey',readAPIKey);
% Select the timestamps at which the maximum and minimum temperatures were
measured
timeMaxTemp = timeStamp(maxTempIndex);
timeMinTemp = timeStamp(minTempIndex);
% Learn more about the thingSpeakWrite function by going to the Documentation tab
on
% the right side pane of this page.
%thingSpeakWrite(writeChannelID,maxTempF,'timestamp',timeMaxTemp,'Writekey',write
APIKey);
OUTPUT
40
34.0800
void setup() {
Serial.begin(115200);
delay(10);
digitalWrite( 5, LOW);
digitalWrite( 4, LOW);
digitalWrite( 0, LOW);
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(500);
Serial.print(".");
Serial.println();
server.begin();
Serial.println("WiFi connected");
void loop()
if (! client) { return; }
while(! client.available())
{ delay( 1); }
Serial.println( request);
client.flush(); // Match the request
if (request.indexOf("fan1on") > 0)
{digitalWrite( 5, HIGH);
if (request.indexOf("fan1off") > 0)
{ digitalWrite( 5, LOW);
Serial.println("FAN 1 off");
if (request.indexOf("light1on") > 0)
{ digitalWrite( 4, HIGH);
Serial.println("LIGHT 1 on");
if (request.indexOf("light1off") > 0)
{ digitalWrite( 4, LOW);
Serial.println("LIGHT 1 off");
if (request.indexOf("fan2on") > 0)
{ digitalWrite( 0, HIGH);
Serial.println("FAN 2 on");
if (request.indexOf("fan2off") > 0)
{ digitalWrite( 0, LOW);
Serial.println("FAN 2 off");
if (request.indexOf("light2on") > 0)
Serial.println("LIGHT 2 on");
if (request.indexOf("light2off") > 0)
client.println(" <html>");
client.println(" <head>");
client.println(" </head>");
else
client.println(" </center>");
client.println(" <center>");
else
client.println(" </center>");
client.println(" <center>");
else
client.println(" </center>");
client.println(" <center>");
else
client.println(" </center>");
client.println(" <center>");
client.println(" <tr>");
else
else
client.println(" </tr>");
client.println(" <tr>");
else
else
client.println(" </tr>");
client.println(" </table>");
client.println(" </center>");
client.println(" </html>");
delay( 1);
Serial.println("");