Iot Rec
Iot Rec
Iot Rec
Aim:
Introducing NodeMCU on Arduino IDE and Serial Monitor interfacing, understanding its role
as an ESP8266-based WiFi module, and learn to code and upload programs using the Arduino IDE.
Apparatus:
Theory:
NodeMCU is an open source platform based on ESP8266 which can connect objects and let
data transfer using the Wi-Fi protocol. In addition, by providing some of the most important features
of microcontrollers such as GPIO, PWM, ADC, and etc. The general features of this board are as
follows:
Easy to use, programmability with Arduino IDE or IUA languages, available as an access point or
station, practicable in Event-driven API applications, having an internal antenna ,containing 13 GPIO
pins, 10 PWM channels, I2C, SPI, ADC, UART, and 1-Wire.
While developing an application using an Node MCU sometimes the developer is required to
look in to the execution of code while the code is being executed.
This is done in the form of input and output taken from the Node MCU. The Node MCU IDE has an
inbuilt serial monitor that allows ton take a serial input and output from an Node MCU while the
Node MCU is in action.
Circuit Diagram:
No additional hardware is required other than a USB cable and NodeMCU. Plug the cable to
NodeMCU and computer/laptop USB.
1
2
Procedure:
1. In order to use Arduino IDE to program the NodeMCU, you have to introduce it to the software
at first.To do this copy the following code and follow the steps below:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
2. Choose Preferences in the File menu and enter the copied code in Additional Board Manager
URLs part. Then press OK.
3. Search the word ESP8266 in Boards>boards manager from Tools menu. Then install ESP8266
boards. After complete installation, you will see the INSTALLED label on ESP8266 boards.
4. After these two steps, you can see ESP8266 based boards such as NodeMCU in your Arduino
IDE boards list, and you can choose your desired board to upload the code.
5. Compile the program and upload it to run the program.
Program:
void setup() {
Serial.begin(9600);
Serial.print("Hello World");
}
void loop() {
}
Output:
open the serial monitor and select 9600 baud rate. As the serial monitor opens “Hello World” will
be seen on output panel.
3
EXPERIMENT 2
DIGITAL OUTPUT TAKING ON LED
Aim:
To showcase turning an LED on and off using NodeMCU on Arduino, illustrating basic digital
output control.
Apparatus:
Theory:
4
Schematic Diagram:
Circuit Layout:
5
Procedure:
Programming:
Output:
After successful uploading of the code, the execution of the code makes the LED on for one second
and off for another one second. This happens in an infinite loop. Thus the LED keeps blinking.
6
EXPERIMENT 3
NODEMCU ANALOG OUTPUT ON ARDUINO IDE
Aim:
To showcase turning an LED on and off using NodeMCU on Arduino, illustrating basic analog
output control.
Apparatus:
Theory:
A signal with respect to time has some value. In case of an analog signal it may has any value. In
the Node MCU it may has values between GND and VCC. VCC can be 3.3V. If a Node MCU is
operating on 3.3V then an analog output can have any value between 0 volt to 3.3 volt. An
analog signal may have 1.2v or 2.3v or any other value between 0 and 3.3 unlike digital output
which has only two options 0 or 3.3V.
If an LED is connected to an analog output it glows maximum on a signal of 3.3V and glows
minimum at 0V. The minimum glow of an LED is zero (LED off). When output varies from 0 to
3.3V the LED shows fed in and fed out effects.
Schematic Diagram:
7
Circuit Layout:
Procedure:
8
5. Click the "Upload" button in Arduino IDE to upload the code to NodeMCU.
6. Observe LED Blinking:
7. After uploading, observe the LED connected to D2 pin blinking with 1-second
intervals.
Programming:
const int LED_ao = 4;
void setup() {
pinMode(LED_ao, OUTPUT);
}
void loop() {
for (int brightness=1; brightness<=255; brightness++)
{
analogWrite(LED_ao, brightness);
delay(10);
}
for (int brightness=255; brightness>0; brightness--)
{
analogWrite(LED_ao, brightness);
delay(10);
}
}
Output
The execution makes fed out effects on the LED. To vary speed of fed in and out effect try changing
delay.
9
EXPERIMENT 4
NODEMCU DIGITAL INPUT WITH OUTPUT ARDUINO IDE
Aim:
To showcase turning an LED on and off using NodeMCU on Arduino, illustrating basic digital
input control.
Apparatus:
Theory:
A discrete signal (digital signal) supplied to the NodeMCU is known as digital input. This signal
can be generated manually using a push button switch.
Push button switch is a switch which provides connectivity between its terminals when pressed.
When the button is released terminals get disconnected. Utilizing a NodeMCU board and Arduino
IDE to control the state of an LED using a push button as an input. NodeMCU, based on the ESP8266
module, is programmed through the Arduino IDE. The LED is connected to the D1 pin, accompanied
by a resistor for current limitation, while a push button is linked to the D2 pin. To prevent floating
input, a pull-up resistor is connected between the D2 pin and 3.3V on NodeMCU. The Arduino code
configures D1 as an output for the LED and D2 as an input with a pull-up resistor. In the loop, the
code continuously checks the state of the push button; if pressed (LOW state), the LED is turned on,
and if released (HIGH state), the LED is turned off. Upon uploading the code to NodeMCU, the push
button's state dictates the LED's on/off behavior, illustrating fundamental principles of digital input
and output control in microcontroller-based projects.
10
Schematic Diagram:
Circuit diagram:
11
Procedure:
PROGRAM:
const int BUTTON = 4; // Naming switch button pin
const int LED = 0; // Namin LED pin
int BUTTONstate = 0; // A variable to store Button Status / Input
void setup(){
pinMode(LED, OUTPUT);
pinMode (BUTTON, INPUT); }
void loop() {
BUTTONstate = digitalRead(BUTTON); // Reading button status / input
if (BUTTONstate == HIGH) // Condition to check button input
{ digitalWrite(LED, HIGH);
}
else
{ digitalWrite(LED, LOW);
}
}
Output:
After successful uploading of the code, try pressing and releasing the button, the LED glows if
the button is pressed and gets off when the button is released.
12
EXPERIMENT 5
ANALOG INPUT TAKEN FROM PRESET
Aim:
To showcase turning an LED on and off using NodeMCU on Arduino, illustrating basic analog
input control.
Apparatus:
Theory:
To read an analog signal through the NodeMCU, Analog to Digital conversion is required. A
NodeMCU has 10 bit ADC which means it scales an analog signal in a range of 0-1023.
In this example an analog input is taken and it displayed on an LED and the serial monitor.In
order to show the input result on LED, Mapping of input value is need. Thus mapping is done by
dividing input values by 4.
This experiment involves utilizing a NodeMCU board in conjunction with the Arduino IDE to
interface with a preset potentiometer, providing an analog input. The NodeMCU, powered by the
ESP8266 module, is programmed to interpret analog signals. The preset potentiometer serves as a
variable resistor, generating analog voltages corresponding to its position. The NodeMCU, using the
analogRead function, captures these analog values, typically ranging from 0 to 1023, facilitating real-
time adjustments based on the potentiometer's setting. This experiment showcases the capability
of NodeMCU to handle analog inputs, enabling applications where variable resistances or sensor
readings are crucial.
13
Schematic diagram:
Circuit diagram:
14
Procedure:
PROGRAM:
const int analog_ip = A0; //Naming analog input pin
void setup() {
void loop() {
Serial.println (inputVal);
delay(1000);
Output:
The analog input is printed on the serial monitor. LED brightness is adjusted as per the
analog input value. Try rotating preset and see the effect on the LED and serial monitor.
15
EXPERIMENT 6
NODEMCU LDR ON ARDUINO IDE
Aim:
To showcase turning an LED on and off using NodeMCU on Arduino, illustrating basic digital
output control.
Apparatus:
Theory:
Schematic diagram:
16
Circuit diagram:
Procedure:
17
8. Copy and paste the provided code.
9. The code initializes the analog pin for the LDR, sets up serial communication, and
continuously reads and prints LDR values to the serial monitor.
10. Click the "Upload" button in Arduino IDE to upload the code to NodeMCU.
11. Open the Serial Monitor in Arduino IDE (Tools -> Serial Monitor).
12. Observe the changing analog values representing the light-dependent resistance of
the LDR.
Program:
Output:
Values of LDR depend upon the light exposed on it. These values are displayed on the serial
Monitor.
18
EXPERIMENT 7
DHT11 TEMPERATURE & HUMIDITY SENSOR ON NODEMCU USING ARDUINO IDE
Aim:
To measure and monitor temperature and humidity using a DHT11 sensor interfaced with
NodeMCU, programmed with Arduino IDE.
Apparatus:
Theory:
The DHT11 sensor is employed to measure ambient temperature and humidity levels. This
digital sensor communicates with the NodeMCU microcontroller, utilizing a single data wire.
The DHT11 provides accurate and real-time readings, making it ideal for environmental
monitoring applications. NodeMCU, powered by the ESP8266 module, inerprets the sensor
data, and the Arduino IDE is used for programming and data analysis.
The DHT11 is chosen because it is lab calibrated, accurate and stable and its signal output is
digital. Most important of all, it is relatively inexpensive for the given performance. Below is
the pinout of the sensor.
The DHT11 sensor communicates using a one-wire protocol, where it sends a start signal to
initiate communication and transmits 40 bits of data comprising temperature and humidity
information. The DHT library in the Arduino IDE simplifies the interface with the sensor,
making it accessible for developers. The library abstracts the low-level details of the one-
wire communication, allowing programmers to focus on extracting temperature and
humidity values.
DHT11 sensor's integration with NodeMCU and Arduino IDE facilitates a straightforward
and accessible means to incorporate temperature and humidity sensing capabilities into
diverse electronic projects. This combination provides a versatile platform for both
hobbyists and professionals to create applications ranging from home automation to
industrial monitoring, contributing to the broader landscape of IoT and sensor-based
systems.
19
Circuit diagram:
Procedure:
1. Connect the DHT11 sensor to NodeMCU: VCC to 3.3V, GND to GND, and data pin to
D2
2. Set up NodeMCU on Arduino IDE: Open Arduino IDE, install necessary platform files for
NodeMCU, and select "NodeMCU 1.0 (ESP-12E Module)" under "Tools" -> "Board."
3. Write Arduino Code: Create a new sketch, paste the code, utilize the DHT library for
NodeMCU, initialize the sensor, and read temperature and humidity values.
4. Upload the Code: Click the "Upload" button in Arduino IDE to load the code onto
NodeMCU.
5. Monitor Temperature and Humidity: Open the Serial Monitor in Arduino IDE (Tools ->
Serial Monitor), and observe real-time temperature and humidity readings from the
DHT11 sensor.
20
Program:
#include "DHT.h" // including the library of DHT11 temperature and humidity sensor
#define DHTTYPE DHT11 // DHT 11
#define dht_dpin 0
DHT dht(dht_dpin, DHTTYPE);
void setup(void)
{
dht.begin();
Serial.begin(9600);
Serial.println("Humidity and temperature\n\n");
delay(700);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Current humidity = ");
Serial.print(h);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(t);
Serial.println("C ");
delay(800);
}
Output:
Upload the above code to the NodeMCU and open serial monitor to check the temperature
and humidity at the instant.
21
EXPERIMENT 8
. NODEMCU CONNECTING TO INTERNET ON ARDUINO IDE
Aim:
To establish internet connectivity on NodeMCU using Arduino IDE for IoT applications.
Apparatus:
Theory:
NodeMCU, powered by the ESP8266 module, can connect to the internet, enabling Internet
of Things (IoT) applications. The Arduino IDE facilitates programming NodeMCU to interface
with Wi-Fi networks. By integrating the Wi-Fi library and providing network credentials in
the code, NodeMCU can connect to a specified Wi-Fi network. Functions such as
WiFi.begin() are employed to initiate the connection, and WiFi.status() allows monitoring the
connection status. Successful execution allows NodeMCU to access the internet, providing a
gateway for various IoT functionalities and data exchange.
Connecting NodeMCU to the internet involves integrating it with a Wi-Fi network. The
Arduino IDE simplifies this process by providing libraries and functions for Wi-Fi
configuration. In the code, the WiFi.begin() function is used to initiate the connection,
requiring the specification of the Wi-Fi network's SSID and password. The WiFi.status()
function allows continuous monitoring of the connection status, enabling feedback on
successful or failed connections. Once connected, NodeMCU gains internet access, opening
avenues for fetching data, sending notifications, and participating in broader IoT
ecosystems. This integration is fundamental for creating smart and connected devices,
contributing to the broader landscape of the Internet of Things.
22
CIRCUIT DIAGRAM:
Procedure:
Program:
#include <ESP8266WiFi.h>
const char *ssid = "Your wifi Network name"; // replace with your wifi ssid and wpa2 key
const char *pass = "Network password";
WiFiClient client;
void setup()
23
{
Serial.begin(9600);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
}
Output
Output of this project is seen on serial monitor. Open serial monitor after uploading the
code. Output is seen there.
24
EXPERIMENT 9
NODEMCU ON ARDUINO IDE - GETTING MAC ADDRESS
Aim:
To retrieve the MAC address of NodeMCU using Arduino IDE for identification and network-
related configurations.
Apparatus:
S.no Component Quantity
1 Node MCU 1
2 USB Cable 1
3 Bread board 1
Theory:
The Media Access Control (MAC) address uniquely identifies a network device, such as NodeMCU,
on a network. Obtaining the MAC address is crucial for tasks like network configuration and device
identification. In the Arduino IDE, the WiFi.macAddress() function is used to fetch the MAC address
of NodeMCU. The MAC address is typically represented as a hexadecimal value and consists of six
pairs of digits, separated by colons or hyphens. This information is useful for network
administrators, enabling precise control over device access, troubleshooting network issues, and
ensuring secure communication.
The MAC (or Machine Access Control) address is a unique value associated with a network adapter.
So, MAC addresses are hardware addresses that uniquely identify a network adapter. MAC address
is best thought of as kind of serial number assigned to every network adapter.
No two devices shouldhave the same MAC address. MAC address works at the Data Link Control
Layer of the OSI Model. MAC addresses are most often assigned by the manufacturer of a network
interface controller (NIC).
Circuit diagram:
25
Procedure:
Program:
Output :
After uploading the code open serial monitor and you will find MAC of your NodeMCU.
26
EXPERIMENT 10
NODEMCU I2C SCANNER ON ARDUINO IDE
Aim:
Apparatus:
Theory:
I2C bus is a means of connecting several peripheral input and output devices that
support I2C on two wires. One of those wires is the DATA line called the SDA, whereas the
other is the CLOCK line called the SCL. The information is sent on these two lines using what
is called the I2C communication protocol. This tutorial explains how to find attached i2c
devices with NodeMCU. It also scans the address of each i2c device attached to the
NodeMCU.
an I2C scanner is a program designed to identify and communicate with devices
connected to the I2C (Inter-Integrated Circuit) bus. The I2C protocol enables multiple
devices to communicate with a microcontroller using a shared bus, utilizing unique
addresses for identification. The I2C scanner on the NodeMCU Arduino connection operates
by sequentially scanning through possible device addresses and checking for a response.
When a response is received, it indicates the presence of an active device at that particular
address. This tool is valuable for identifying and troubleshooting connected I2C devices,
ensuring proper communication and addressing in a connected network of sensors,
actuators, or other peripherals. It simplifies the process of configuring and integrating
various components into a cohesive and functional IoT (Internet of Things) system.
The I2C (Inter-Integrated Circuit) scanner for NodeMCU on Arduino facilitates the
discovery and recognition of I2C devices attached to the NodeMCU microcontroller. In the
I2C communication protocol, devices are assigned unique addresses, allowing the
NodeMCU to communicate with multiple peripherals on the same bus. The scanner
program iterates through possible I2C addresses, typically ranging from 0x08 to 0x77, and
sends a signal to each address. If a device is present at a specific address, it acknowledges
27
the signal, and the scanner logs the detected address. This information is crucial during the
development and debugging phases, as it helps identify the correct addresses of connected
devices, ensuring that the NodeMCU can interact with them effectively. The I2C scanner
enhances the efficiency and reliability of IoT projects by streamlining the process of
configuring and integrating various sensors, displays, or other I2C-enabled components into
a unified system.
Circuit diagram:
Programming:
#include<Wire.h>
void setup() {
Serial .begin (9600);
while ( !Serial ) // Waiting for serial connection
{
}
Serial .println ();
Serial .println ("I2C scanner. Scanning ...");
byte count = 0;
Wire .begin();
for (byte i = 8; i < 120; i++ ) {
Wire .beginTransmission (i);
28
if (Wire .endTransmission () == 0) {
Serial .print ("Found address: ");
Serial .print (i , DEC);
Serial .print (" (0x");
Serial .print (i , HEX);
Serial .println (")");
count++ ;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial .println ("Done.");
Serial .print ("Found ");
Serial .print (count , DEC);
Serial .println (" device(s).");
} // end of setup
void loop() {
}
Output:
Open serial monitor after uploading the code. The output of this sketch will be as shown
below. The I2C address is highlighted.
29
EXPERIMENT 11
NODEMCU 16X2 LCD ON ARDUINO IDE
Aim:
Apparatus:
Theory:
30
Circuit diagram:
Procedure:
1. Connect the I2C LCD to NodeMCU:SDA on the LCD to D2 on NodeMCU, SCL on the
LCD to D1 on NodeMCU, VCC on the LCD to 5V on NodeMCU, GND on the LCD to
GND on NodeMCU.
2. Now connect the LCD to the I2c LCD backpack using the jumper wires.
3. Set up NodeMCU on Arduino IDE:
• Open Arduino IDE.Install the "LiquidCrystal_I2C" library.
• Select "NodeMCU 1.0 (ESP-12E Module)" under "Tools" -> "Board."
4. Write Arduino Code:
• Create a new sketch and include "Wire" and "LiquidCrystal_I2C" libraries.
31
• Initialize the LCD with its I2C address (0x27 or 0x3F) and define the LCD size
(16x2 or 20x4).
5. Upload the Code:
• Click the "Upload" button in Arduino IDE to load the code onto NodeMCU.
6. Monitor LCD Display:
• Connect the I2C LCD to NodeMCU.
• Observe the LCD screen displaying the information specified in the code.
Program:
#include<Wire.h>
#include <LiquidCrystal_I2C.h>
Int counter=0;
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup()
{
lcd.begin(16,2); // The begin call takes the width and height. This
lcd.init(); // Should match the number provided to the constructor.
lcd.backlight(); // Turn on the backlight
lcd.clear();
void loop() {
lcd.setCursor(0, 0); // Move the cursor characters to the right and
// zero characters down (line 1).
lcd.print("HELLO"); // Print HELLO to the screen, starting at 0,0.
lcd.setCursor(0, 1); // Move the cursor to the next line and print
lcd.print("WORLD"); // WORLD.
}
Output:
Successful upload of the above code prints Hello in the first line and World in the second
line. As we have printed something else on the following image.
32
EXPERIMENT 12
NODEMCU TEMPERATURE, HUMIDITY DATA UPLOAD ON THINGSPEAK ON ARDUINO
IDE
Aim:
To upload temperature and humidity data from a DHT11 sensor connected to NodeMCU to
ThingSpeak for real-time monitoring.
Apparatus:
Theory:
NodeMCU, equipped with a DHT11 sensor, is programmed using the Arduino IDE to measure
temperature and humidity. The DHT library is utilized for sensor interfacing, while the ESP8266WiFi
library facilitates Wi-Fi communication. The ThingSpeak platform, a cloud-based IoT service, is
configured to receive and display the sensor data. The Arduino code initializes the sensor, connects
NodeMCU to a Wi-Fi network, and utilizes the ThingSpeak API to upload temperature and humidity
readings to a specified channel. This integration allows for remote monitoring of environmental
conditions via the ThingSpeak platform.
The DHT library in the Arduino IDE simplifies the communication with the DHT11 sensor, abstracting
the complexities of the one-wire protocol. It allows programmers to easily retrieve temperature and
humidity readings from the sensor, which can then be utilized for various applications. In this case,
the focus is on uploading the collected data to ThingSpeak for remote monitoring.
In practical terms, connecting the DHT11 sensor to NodeMCU involves powering the sensor (VCC to
3.3V), establishing a ground connection (GND to GND), and connecting the data pin to a digital input
on NodeMCU (e.g., D2). The NodeMCU, connected to the internet through Wi-Fi, is configured with
ThingSpeak channel information and the appropriate credentials.
The Arduino code written for NodeMCU initializes the DHT sensor, establishes a Wi-Fi connection,
and uses the ThingSpeak API to upload temperature and humidity data to a specific channel. The
code includes necessary libraries such as "DHT.h" and "ESP8266WiFi.h" and defines variables for the
Wi-Fi credentials and ThingSpeak channel information. The DHT sensor's data is read using
functions provided by the DHT library, and the data is then sent to ThingSpeak for visualization and
analysis.
33
Circuit diagram:
Procedure:
1. Connect the DHT11 sensor to NodeMCU: VCC to 3.3V, GND to GND, and Data pin to D2.
2. Set up NodeMCU on Arduino IDE: Open Arduino IDE, install necessary platform files for
NodeMCU, and select "NodeMCU 1.0 (ESP-12E Module)" under "Tools" -> "Board."
34
3. Go to https://thingspeak.com/ and create an account if you do not have one. Login to your
account.
4. Create a new channel by clicking on the button.Enter basic details of the channel.Than Scroll
down and save the channel.
5. Channel Id is the identity of your channel. Note down this. Than go to API keys copy and
paste this key to a separate notepad file will need it later
6. Write Arduino Code: Create a new sketch, include the required libraries, enter Wi-Fi and
ThingSpeak credentials, initialize the DHT sensor, and set up ThingSpeak channel
information.
7. Upload the Code: Click the "Upload" button in Arduino IDE to load the code onto NodeMCU.
8. Monitor Serial Output: Open the Serial Monitor in Arduino IDE (Tools -> Serial Monitor) to
observe NodeMCU connecting to Wi-Fi and sending data to ThingSpeak.
Program:
35
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
Output:
36
37
EXPERIMENT 13
NODEMCU WEATHER STATION ON ARDUINO IDE
Aim:
To make an online weather station using ESP8266 wifi module. It is a stand alone weather station
that collects weather updates from openweathermap.org and displays the retrieved information on
I2C 16X2 LCD.
Apparatus:
Theory:
Creating an online weather station using the ESP8266 WiFi module involves integrating
several components to collect and display weather updates from openweathermap.org. The
ESP8266 is a versatile and cost-effective WiFi module that can connect to the internet, making it
suitable for IoT projects like a weather station. The openweathermap.org API provides access to
real-time weather data, allowing your ESP8266 to fetch information such as temperature, humidity,
and forecasts. To display the retrieved information, an I2C 16x2 LCD is used, providing a compact
and easy-to-read interface. The I2C communication protocol reduces the number of wires required
for connecting the LCD to the ESP8266, simplifying the hardware setup.
To build an online weather station using the ESP8266 WiFi module, the project combines the
power of the ESP8266's internet connectivity with the openweathermap.org API and an I2C 16x2
LCD for user-friendly display. The ESP8266, equipped with built-in WiFi capabilities, enables
seamless communication with the internet, making it an ideal choice for IoT applications. In this
project, we leverage the openweathermap.org API to fetch real-time weather data, including
temperature, humidity, and forecasts. The I2C 16x2 LCD serves as the visual interface, providing a
compact display solution. The I2C protocol simplifies the hardware setup, minimizing the wiring
complexity between the ESP8266 and the LCD.
38
Circuit diagram:
39
Procedure:
1. Install Arduino IDE.
2. Add ESP8266 board support in Arduino IDE.
3. Connect ESP8266 to the computer via USB-to-Serial adapter.
4. Develop code for ESP8266 WiFi initialization.
5. Ensure successful internet connection.
6. Obtain an API key from openweathermap.org.
7. Implement code on ESP8266 for HTTP requests to the API.
8. Parse JSON response to extract weather data.
9. Connect I2C 16x2 LCD to ESP8266, managing wiring and power.
10. Install I2C LCD libraries in Arduino IDE.
11. Code initialization and weather data display on the LCD.
12. Combine WiFi setup, weather data retrieval, and LCD display code into a unified sketch.
13. Upload the integrated sketch to ESP8266.
14. Monitor Serial Monitor for debugging.
15. Confirm proper functionality by observing real-time weather updates on the LCD.
Program:
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#include <Wire.h>
const char* ssid = "Your Network Name"; // SSID of local network
const char* password = "Your Password"; // Password on network
String APIKEY = "API Key";
String CityID = "CityID"; //Your City ID
WiFiClient client;
char servername[]="api.openweathermap.org"; // remote server we will connect to
String result;
int counter = 60;
String weatherDescription ="";
String weatherLocation = "";
String Country;
float Temperature;
float Humidity;
float Pressure;
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Address of your i2c LCD back pack should be updated.
void setup() {
Serial.begin(115200);
int cursorPosition=0;
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.print(" Connecting");
Serial.println("Connecting");
40
WiFi.begin(ssid, password);
void loop() {
if(counter == 60) //Get new data every 10 minutes
{
counter = 0;
displayGettingData();
delay(1000);
getWeatherData();
}else
{
counter++;
displayWeather(weatherLocation,weatherDescription);
delay(5000);
displayConditions(Temperature,Humidity,Pressure);
delay(5000);
}
}
41
while(client.connected() && !client.available())
delay(1); //waits for data
while (client.connected() || client.available())
{ //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}
if (!root.success())
{
Serial.println("parseObject() failed");
}
String location = root["name"];
String country = root["sys"]["country"];
float temperature = root["main"]["temp"];
float humidity = root["main"]["humidity"];
String weather = root["weather"]["main"];
String description = root["weather"]["description"];
float pressure = root["main"]["pressure"];
weatherDescription = description;
weatherLocation = location;
Country = country;
Temperature = temperature;
Humidity = humidity;
Pressure = pressure;
}
void displayWeather(String location,String description)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(location);
lcd.print(", ");
lcd.print(Country);
lcd.setCursor(0,1);
lcd.print(description);
}
42
void displayConditions(float Temperature,float Humidity, float Pressure)
{
lcd.clear(); //Printing Temperature
lcd.print("T:");
lcd.print(Temperature,1);
lcd.print((char)223);
lcd.print("C ");
lcd.print(" H:"); //Printing Humidity
lcd.print(Humidity,0);
lcd.print(" %");
lcd.setCursor(0,1); //Printing Pressure
lcd.print("P: ");
lcd.print(Pressure,1);
lcd.print(" hPa");
}
void displayGettingData()
{
lcd.clear();
lcd.print("Getting data");
}
Output:
The output of the following code will display temperature, Humidity and Pressure (of the selected
city) on the I2C 16X2 LCD as well as on Serial Monitor.
43
EXPERIMENT 14
NODEMCU RGB LED ON ARDUINO IDE
Aim:
Develop a NodeMCU-based RGB LED project to showcase dynamic color control through
WiFi, highlighting the versatility of NodeMCU in IoT applications.
Apparatus:
Theory:
Creating a NodeMCU-based RGB LED project using Arduino IDE involves harnessing the
capabilities of NodeMCU, a popular ESP8266 development board, to control an RGB LED for colorful
illumination. The RGB LED allows dynamic color changes, making it suitable for various applications
like ambient lighting or visual indicators. By leveraging the NodeMCU's WiFi connectivity and
programmability through the Arduino IDE, users can develop custom code to control the RGB LED
remotely. This project aims to demonstrate the integration of NodeMCU with an RGB LED,
showcasing its versatility in IoT and home automation applications.
Emphasizing the ability to control the RGB LED dynamically through WiFi connectivity. By
harnessing the capabilities of NodeMCU and its compatibility with the Arduino IDE, this project
showcases the integration of Internet of Things (IoT) principles with a common RGB LED. The project
aims to demonstrate the practicality and versatility of NodeMCU for remote-controlled applications,
allowing users to explore creative lighting scenarios or incorporate dynamic visual indicators in IoT
and home automation projects.
An RGB LED is a combination of 3 LEDs RED, Green and Blue. These three colors Red, green and blue
can make any color. By varying supplied voltage to RGB LEDs different colors are formed. In the
Node MCU different voltage are supplied using analog output function.
An RGB LED has 4 pin interfaces. 3 Pins are for Red, Blue and Green. There is a common pins for all
three LEDs
An RGB LED can be two types-
1. Common Anode:-Anode (+) pin is common.
2. Common Cathode:- (Cathode-/GND) pin is common.
44
Common Anode Type
Building Circuit
Schematic of circuit
Layout of circuit
45
Procedure:
1. Install and set up the Arduino IDE with NodeMCU board support.
2. Connect NodeMCU to the computer via USB.
3. Write Arduino code for NodeMCU initialization and WiFi setup.
4. Wire the RGB LED to NodeMCU, ensuring proper connections and resistor usage.
5. Develop Arduino code for RGB LED control using PWM for color intensity modulation.
6. Implement functions for dynamic color changes and transitions.
7. Upload the compiled code to NodeMCU using the Arduino IDE.
8. Monitor the Serial Monitor for debugging information.
9. Test the RGB LED, observing color changes and patterns to ensure successful integration.
10. Validate remote control of RGB LED through WiFi, emphasizing the NodeMCU's applicability
in IoT scenarios.
Programming:
const int RED = 5;
const int GREEN = 4;
const int BLUE = 0;
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop() {
analogWrite(RED, 50);
analogWrite(GREEN, 50);
analogWrite(BLUE, 200);
delay (1000);
analogWrite(RED, 100);
analogWrite(GREEN, 100);
analogWrite(BLUE, 100);
delay (1000);
}
Output:
After successful uploading of the code, the RGB LED generates two different colors in a time interval
of 1 second. Try different set of RGB to get different colors.
46
EXPERIMENT 15
NODEMCU SWITCHING USING TRANSISTOR ON ARDUINO IDE
Aim:
Apparatus:
Theory:
The utilization of NodeMCU in conjunction with transistors for remote switching applications
signifies a significant advancement in the realm of Internet of Things (IoT) and home automation.
NodeMCU, based on the ESP8266 platform, offers integrated WiFi capabilities, making it an ideal
candidate for wirelessly controlling physical devices. The project revolves around employing the
Arduino IDE to program NodeMCU, which acts as the brain of the operation. By integrating
transistors into the circuit, the project aims to showcase how NodeMCU can serve as a versatile and
powerful controller for managing devices remotely. This paradigm underscores the potential of
NodeMCU to enhance user accessibility and control in various IoT scenarios.
The practical implementation involves coding NodeMCU to initialize WiFi connectivity and
establish communication channels. The transistor, whether NPN or PNP, serves as the switch that
NodeMCU controls, facilitating the remote manipulation of external devices. This configuration is
complemented by the inclusion of necessary components, such as resistors, to ensure the stability
and protection of the circuit. The uploaded code undergoes monitoring through the Arduino IDE's
Serial Monitor for debugging purposes. Rigorous testing is then conducted to validate the efficacy of
the NodeMCU-controlled transistor switch, highlighting its potential application in smart home
solutions and broader IoT projects. This project not only exemplifies the seamless integration of
NodeMCU in hardware applications but also paves the way for innovative and automated solutions
in the realm of connected devices.
47
Circuit diagram:
Program:
48
EXPERIMENT 16
NODEMCU IR PROXIMITY & COLOR DETECTION ON ARDUINO IDE
Aim:
Showcase the versatility of NodeMCU by integrating Infrared (IR) proximity sensing and color
detection for responsive IoT applications using the Arduino IDE.
Apparatus:
Theory:
NodeMCU's role in this project lies in its capability to seamlessly integrate with Infrared (IR)
proximity sensors and color sensors, showcasing its versatility in IoT scenarios. The IR proximity
sensor provides the ability to detect the presence of objects without physical contact, enabling
NodeMCU to respond to varying distances. Additionally, the incorporation of a color sensor further
enhances NodeMCU's functionality, allowing it to recognize and react to different colors. The
Arduino IDE serves as the programming environment, enabling users to code NodeMCU effectively
and demonstrate its potential for creating intelligent and responsive systems that go beyond
conventional microcontroller applications.
An infrared light emitting diode (IR LED) emits light of Infrared range 700 nanometers (nm)
to 1 mm. This light is not visible by naked eyes but can be seen by a camera (that is why these are
also used in night vision cameras). A photo diode gives response in term of change in resistance
when light falls on it. That change is measured in terms of voltage. An IR LED and a Photo diode are
used in a combination for proximity and color detection. An IR LED (transmitter) emits IR light, that
light gets reflected by the object, the reflected light is received by an IR receiver (Photo Diode).
Amount of reflection and reception varies with the distance. . This difference causes to change in
input voltage through IR input. This variation in input voltage is used for proximity detection. For
color detection application: The amount of reflected light depends upon the color of surface from
which it is reflected. The reflection is different for different colored surfaces. This makes it a color
detector.
49
Circuit diagram:
50
Procedure:
1. Install and configure the Arduino IDE with NodeMCU board support.
2. Establish a USB connection between NodeMCU and the computer for programming.
3. Connect the IR proximity sensor to NodeMCU, ensuring correct wiring.
4. Write Arduino code to initialize the IR proximity sensor and interpret proximity data.
5. Implement logic for NodeMCU to respond to detected objects, specifying actions based on
proximity.
6. Connect the color sensor to NodeMCU, ensuring proper wiring.
7. Develop Arduino code to read and interpret color data from the sensor.
8. Implement logic for NodeMCU to respond based on detected colors, allowing for applications
like mood lighting or color-specific automation.
9. Combine the IR proximity sensor and color sensor code into a unified Arduino sketch.
10. Ensure the code enables NodeMCU to seamlessly manage both sensor functionalities.
11. Upload the compiled code to NodeMCU using the Arduino IDE.
12. Monitor the Serial Monitor for debugging feedback.
13. Rigorously test the system, verifying NodeMCU's ability to detect objects through IR proximity
sensing and respond to different colors with the integrated color sensor.
14. Demonstrate the practical applications of NodeMCU's IR proximity and color detection
capabilities, emphasizing its potential for intelligent and responsive IoT solutions.
Program:
const int photo_diode = A0;
int inputVal = 0;
void setup() {
Serial.begin(9600);
}
void loop(){
inputVal = analogRead(photo_diode);
Serial.print("Input Value: ");
Serial.println(inputVal);
delay(1000);
}
Output:
Place an object in front of the IR LED & Photo diode. The analog input values from photo diode are
displayed on the serial monitor. Try using different colored surfaces and varying distance of object
from the IR LED & photodiode.
51
EXPERIMENT 17
NODEMCU SERVO CONTROL ON ARDUINO IDE
Aim:
Showcase NodeMCU's capability for precise servo motor control in IoT applications using the
Arduino IDE.
Apparatus:
Theory:
A servo is an actuator that rotates to a precise angle through command. The servo example included
in this chapter rotates between 0 to 180 degree. It can move to any angle between 0-180 degree
A servo receives command from the NodeMCU, moves to the commanded angle and stops there. A
servo has three interface in which two are for power supply and one is for signal input.
integrating NodeMCU with a servo motor, emphasizing its ability to act as a versatile and
programmable controller. The Arduino IDE facilitates the coding process, allowing users to initialize
the servo motor and define control parameters. NodeMCU's role is to execute the programmed
logic, enabling precise positioning of the servo motor. This project aims to highlight NodeMCU's
potential in automation and its contribution to the broader landscape of IoT applications involving
servo motor control.
NodeMCU, built on the ESP8266 platform, offers a compelling solution for servo motor
control in IoT applications. Servo motors are widely used for precise angular positioning, making
them essential for applications like robotics, automation, and remote-controlled devices. In this
project, NodeMCU serves as the central controller, interfacing with the servo motor through the
Arduino IDE. The Arduino code enables NodeMCU to initialize the servo motor, define specific
angles or rotations, and execute precise control based on user-defined parameters.
The integration of NodeMCU with servo motors opens avenues for automation in various IoT
scenarios, ranging from smart home devices to interactive installations. NodeMCU's WiFi
capabilities enhance its potential for remote and distributed applications, allowing users to control
servo motors wirelessly. This project not only underscores the adaptability of NodeMCU as a
microcontroller but also highlights its role in shaping intelligent and responsive systems within the
evolving landscape of IoT.
52
Circuit diagram:
53
Procedure:
1. Install and configure the Arduino IDE with NodeMCU board support.
2. Establish a USB connection between NodeMCU and the computer for programming.
3. Connect the servo motor to NodeMCU, ensuring correct wiring.
4. Write Arduino code to initialize the servo motor and set up control parameters.
5. Implement logic for NodeMCU to control the servo motor's position, specifying angles or
rotations.
6. Combine the servo motor control code into a unified Arduino sketch.
7. Ensure the code enables NodeMCU to seamlessly manage servo motor functionalities.
8. Upload the compiled code to NodeMCU using the Arduino IDE.
9. Monitor the Serial Monitor for debugging feedback.
10. Rigorously test the system, verifying NodeMCU's ability to accurately control the servo
motor.
11. Demonstrate the practical applications of NodeMCU's servo motor control capabilities,
showcasing its potential for automation and precise positioning in various IoT scenarios.
Program:
void loop(){
servo_1.write (45); // Servo will move to 45 degree angle.
delay (1000);
servo_1.write (90); // servo will move to 90 degree angle.
delay (1000);
}
Output:
As the code executes, servo comes to 45 degree from whatever angles it was. Then there is a delay
of one second. After that it goes to 90 degree stays there for one second and come back to 45
degree. This operation is performed in a continuous loop.
54
EXPERIMENT 18
NODEMCU PEIZO BUZZER ON ARDUINO IDE
Aim:
Illustrate NodeMCU's capacity for integrating and controlling a Piezo Buzzer in IoT
applications using the Arduino IDE.
Apparatus:
Theory:
A Peizo buzzer is a device that is used to generate beep sound (generally a warning or alert
in embedded system). It is a two leg device the longer leg is positive. If voltage is supplied it
generates beep sound. Through analog write volume of beep can be controlled. If a buzzer is
switched with different time intervals it generates a melody.
This project focuses on the synergy between NodeMCU and a Piezo Buzzer, showcasing
NodeMCU's ability to serve as a programmable controller in sound-related IoT applications. The
Arduino IDE provides a user-friendly platform to code NodeMCU, initializing the Piezo Buzzer and
determining specific audio patterns or frequencies. NodeMCU's WiFi capabilities can further extend
the project's applications, allowing for remote sound control. The aim is to emphasize NodeMCU's
versatility in handling acoustic components and its potential contribution to the broader spectrum
of IoT projects involving sound and alerts.
The integration of NodeMCU with a Piezo Buzzer adds a sonic dimension to its capabilities,
expanding its utility in various IoT applications. A Piezo Buzzer is a compact and versatile acoustic
component capable of producing different tones and frequencies. In this project, NodeMCU serves
as the orchestrator, utilizing the Arduino IDE to program and control the Piezo Buzzer. The code
allows NodeMCU to generate specific audio patterns, frequencies, or alerts based on user-defined
parameters, showcasing NodeMCU's role as a flexible sound controller.
The use of NodeMCU in conjunction with a Piezo Buzzer enhances its applicability in scenarios
requiring audio feedback, alarms, or interactive responses. With NodeMCU's inherent WiFi
capabilities, the project extends its reach to remote sound control applications, allowing users to
trigger or modify audio patterns wirelessly. This project exemplifies NodeMCU's potential to
contribute to the growing landscape of IoT projects involving sound and alerts, showcasing its
adaptability in creating intelligent and responsive systems that go beyond conventional
microcontroller applications.
55
Circuit diagram:
56
Procedure:
1. Install and configure the Arduino IDE with NodeMCU board support.
2. Establish a USB connection between NodeMCU and the computer for programming.
3. Connect the Piezo Buzzer to NodeMCU, ensuring proper wiring.
4. Write Arduino code to initialize the Piezo Buzzer and set up sound control parameters.
5. Implement logic for NodeMCU to produce specific audio patterns, frequencies, or alerts.
6. Combine the Piezo Buzzer control code into a unified Arduino sketch.
7. Ensure the code enables NodeMCU to seamlessly manage Piezo Buzzer functionalities.
8. Upload the compiled code to NodeMCU using the Arduino IDE.
9. Monitor the Serial Monitor for debugging feedback.
10. Rigorously test the system, verifying NodeMCU's ability to control the Piezo Buzzer and
produce desired audio patterns.
11. Demonstrate the practical applications of NodeMCU's Piezo Buzzer control capabilities,
highlighting its potential for sound alerts, notifications, or interactive audio responses in
various IoT scenarios.
Program:
void setup() {
pinMode(buzzer, OUTPUT);
void loop(){
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
}
Output:
The buzzer generates beep sound in a loop. Try delay in microseconds to generate melody. Volume
is controlled through analogWrite.
57
EXPERIMENT 19
NODEMCU SEVEN SEGMENT DISPLAY ON ARDUINO IDE
Aim:
To interface a Seven Segment Display with NodeMCU using the Arduino IDE, displaying
numeric characters through the GPIO pins, enabling the creation of digital counters, timers, or other
numerical outputs in IoT projects.
Apparatus:
Theory:
A Seven Segment Display is a widely used output device to represent numeric characters by
illuminating specific segments. When interfaced with a NodeMCU using the Arduino IDE, each
segment of the display is connected to a GPIO pin on the NodeMCU. By controlling the state (on or
off) of these individual pins, numeric characters ranging from 0 to 9 can be displayed. The Arduino
IDE provides functions for managing GPIO pins, making it possible to create a program that dictates
which segments should be illuminated to display a particular digit. This enables developers to
incorporate numerical outputs in their IoT applications, such as digital counters or timers, by
leveraging the simplicity and efficiency of Seven Segment Displays.
A seven segment display is a simple displaying device that uses 8 LEDs to display decimal
numerals. It is generally used in digital clocks, calculators, electronic meters, and other devices that
displays numerical information. A seven segment display has 8 LEDs in it. Each LED is controlled
through specific pin. These can be two type common anode and common cathode. In common
cathode type the cathode is common for all 8 LEDs and in common anode type the anode is
common for all 8 LEDs. A seven segment display has 10 pin interface, 2 pins are for Common and 8
pins are for each LED. Both common pins are internally shorted.
Seven Segment Display consists of seven LEDs arranged in a pattern to form the numeric
digits 0 through 9. An additional eighth segment may be used as a dot for displaying decimal points.
Each segment is labeled from 'a' to 'g,' and the corresponding GPIO pins on the NodeMCU are
connected to these segments through appropriate resistors to limit current flow.
58
Circuit diagram:
59
Procedure:
1. Set up the hardware by connecting the Seven Segment Display to the NodeMCU using
appropriate resistors to limit current flow.
2. Open the Arduino IDE and create a new sketch.
3. Define the GPIO pins to which each segment of the Seven Segment Display is connected,
specifying the connections in the sketch.
4. In the setup() function, set the defined GPIO pins as OUTPUT using the pinMode() function.
5. Create a function to display a specific digit on the Seven Segment Display, mapping the state
of each segment to the corresponding GPIO pin.
6. In the loop() function, call the digit display function for the desired numeric characters,
introducing delays between digits for visibility.
7. Upload the sketch to the NodeMCU board and observe the Seven Segment Display as it
cycles through the specified numeric characters.
8. Customize the program to display different sequences of numbers or integrate it into larger
IoT projects requiring numerical outputs.
Program:
void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(DP, OUTPUT);
}
void loop() {
two();
delay(1000);
five();
delay(1000);
}
void two(){
60
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(DP, HIGH);
}
void eight(){
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(DP, HIGH);
}
Output:
Number 2 is displayed on seven segment display for one second then number 5 is displayed for
another one second, this runs in a loop.
61
EXPERIMENT 20
NODEMCU CONTROLLING MOTOR THROUGH ON ARDUINO IDE
Aim:
To control a motor using a NodeMCU board through the Arduino IDE, allowing for the
development of IoT projects involving motorized devices, such as robotic systems, automated
actuators, or motor-driven appliances.
Apparatus:
Theory:
Controlling a motor with NodeMCU and Arduino involves using GPIO pins to manage the direction
and speed of the motor. Typically, a motor driver module is employed to interface between the NodeMCU
and the motor. The motor driver interprets signals from the NodeMCU and adjusts the voltage supplied to
the motor, thereby controlling its rotation. It is essential to connect the motor driver's input pins to specific
GPIO pins on the NodeMCU and provide a separate power supply for the motor.
The Arduino IDE simplifies the programming process by offering libraries and functions to manage
GPIO pins effectively. By using the analogWrite() function, the NodeMCU can control the motor
speed through pulse-width modulation (PWM), while digital pins determine the motor's rotation
direction. This setup enables developers to create diverse applications, from simple motorized
systems to more complex robotic projects, by adjusting the motor behavior based on programming
logic.In summary, the NodeMCU and Arduino integration allows for precise motor control,
expanding the possibilities for IoT projects that involve motion and automation. The modular and
versatile nature of this setup makes it suitable for various applications requiring motorized
components.
A relay is an electro mechanical switch. It is used for switching high current devices that cannot be
switched by transistor. Relay consists one electromagnetic coil and three terminals for switching.
One among these three terminals is a common terminal that floats between other two terminals.
These two terminals are Normally Close (NC) and Normally Open (NO) terminal. In normal condition
(power is not supplied to relay’s coil) the Common terminal remains connected to the NC terminal.
62
When a power is supplied to the relay’s magnetic coil, it generates magnetic force. This force
detaches common terminal from the NC terminal and connects the common terminal to the NO
terminal. The relay returns to its original condition as soon voltage supply stops. The common
terminal switches between the NO & the NC terminals.
Circuit diagram:
63
Procedure:
1. Connect the motor driver module to the NodeMCU, ensuring proper wiring for input pins
controlling motor direction and speed.
2. Supply power to the motor driver module and connect the motor to the corresponding
output terminals.
3. Open the Arduino IDE and create a new sketch.
4. Include any necessary libraries for the motor driver module.
5. Define the GPIO pins connected to the motor driver input pins in the sketch.
6. In the setup() function, set the defined GPIO pins as OUTPUT using the pinMode() function.
7. In the loop() function, use the analogWrite() function to control the motor speed through
PWM.
8. Use digital GPIO pins to set the motor direction by adjusting their state (HIGH/LOW).
9. Upload the sketch to the NodeMCU board and observe the motor's behavior as it rotates in
the specified direction and speed.
10. Customize the program to implement various motor control scenarios according to the
requirements of the IoT project.
Program:
const int motor = 4; //Motor connected with D2(GPI04) of NodeMCU
void setup() {
pinMode (motor, OUTPUT); }
void loop() {
digitalWrite (motor, HIGH);
delay(1000);
digitalWrite (motor, LOW);
delay(1000); }
Output:
The Motor rotates for 1 second and then stops, remains stopped for 1 second. This loop continues .
64