0% found this document useful (0 votes)
158 views54 pages

IoT Sensors and Actuators Lab Manual

Manual

Uploaded by

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

IoT Sensors and Actuators Lab Manual

Manual

Uploaded by

sri kanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

1.

LED INTERFACING TO NODEMCU


OBJECTIVE: a) To Blink an On-Board LED on NodeMCU.

b) To Interface an External LED to NodeMCU.


COMPONENTS REQUIRED: 1) NodeMCU – 1
2) Micro USB Cable – 1
3) PC – 1
4) Jumper Wires.
5) Bread Board.
6) Resistor- 1 (200 ohm or 1k ohm)

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:
NodeMCU is a development board featuring the popular ESP8266 WiFi chip.
As it turns out, you can program the ESP8266 just like any other microcontroller. Its obvious
advantage over the Arduino or PIC is that it can readily connect to the Internet via WiFi

Features:

 It is a Tensilica 32-bit RISC CPU Xtensa LX106 Microcontroller.


 Its Operating Voltage is 3.3V & Input Voltage is 7-12V.
 It is having 16 Digital I/O Pins & 1 Analog Input Pin (ADC).
 Its having Flash Memory of 4MB & SRAM 64KB. NodeMCU having clock
speed of 80 Mhz.
 Operating temperature range -40C ~ 125C.
 Modes of operation: Client, Access point, Client + Access point.
 10-bit ADC, SDIO 2.0, (H) SPI, UART, I2C, I2S, IR Remote Control,
PWM, GPIO.
The interface of the module is mainly divided into two parts including both
Firmware and Hardware where former runs on the ESP8266 Wi-Fi SoC and later is based on the
ESP-12 module.

Pin Diagram:

Fig: Pin diagram of NodeMCU

Pin Description:

1) Power Pin: There are four power pins viz. one VIN pin & three 3.3V pins. The VIN pin can
be used to directly supply the ESP8266 and its peripherals if you have a regulated 5V voltage
source. The 3.3V pins are the output of an on-board voltage regulator. These pins can be used to
supply power to external components.
2) GND: GND is a ground pin of ESP8266 NodeMCU development board.

3) I2C Pins: I2C Pins are used to hook up all sorts of I2C sensors and peripherals in your
project. Both I2C Master and I2C Slave are supported. I2C interface functionality can be
realized programmatically, and the clock frequency is 100 kHz at a maximum. It should be noted
that I2C clock frequency should be higher than the slowest clock frequency of the slave device.

4) GPIO Pins: ESP8266 NodeMCU has 17 GPIO pins which can be assigned to various
functions such as I2C, I2S, UART, PWM, IR Remote Control, LED Light, and Button
programmatically. Each digital enabled GPIO can be configured to internal pull-up or pull-down
or set to high impedance. When configured as an input, it can also be set to edge-trigger or level-
trigger to generate CPU interrupts.

5) ADC Channel: The NodeMCU is embedded with a 10-bit precision SAR ADC. The two
functions can be implemented using ADC viz. Testing power supply voltage of VDD3P3 pin and
testing input voltage of TOUT pin. However, they cannot be implemented at the same time.

6) UART Pins: ESP8266 NodeMCU has 2 UART interfaces, i.e. UART0 and UART1, which
provide asynchronous communication (RS232 and RS485), and can communicate at up to 4.5
Mbps. UART0 (TXD0, RXD0, RST0 & CTS0 pins) can be used for communication. It supports
fluid control. However, UART1 (TXD1 pin) features only data transmit signal so, it is usually
used for printing log.

7) SPI Pins: ESP8266 features two SPIs (SPI and HSPI) in slave and master modes. These SPIs
also support the following general-purpose SPI features:

 4 timing modes of the SPI format transfer.


 Up to 80 MHz and the divided clocks of 80 MHz
 Up to 64-Byte FIFO.

8) SDIO Pins: ESP8266 features Secure Digital Input/output Interface (SDIO) which is used to
directly interface SD cards. 4-bit 25 MHz SDIO v1.1 and 4-bit 50 MHz SDIO v2.0 are
supported.
9) PWM Pins: The board has 4 channels of Pulse Width Modulation (PWM). The PWM output
can be implemented programmatically and used for driving digital motors and LEDs. PWM
frequency range is adjustable from 1000 μs to 10000 μs, i.e., between 100 Hz and 1 kHz.

10) Control Pins: Control Pins are used to control ESP8266. These pins include Chip Enable pin
(EN), Reset pin (RST) and WAKE pin. EN pin – The ESP8266 chip is enabled when EN pin is
pulled HIGH. When pulled LOW the chip works at minimum power. RST pin – RST pin is used
to reset the ESP8266 chip. WAKE pin – Wake pin is used to wake the chip from deep-sleep.

Before coding, we need to set the Arduino IDE as following:

1. Step 1 – Open Arduino IDE


2. Step 2 – To connect the node microcontroller, go to ToolsBoard and select NodeMCU
1.0 (ESP-12E Module)
3. Step 3 – Now go to programmer and choose USBasp as we are using USB cable to
program it

4. Step 4 – Now choose the COM port on which the NodeMCU is connected
5. Step 5 – Your Arduino IDE is ready to program the NodeMCU

A) BUILT-IN LED BLINKING OF NODEMCU

To blink the built-in LEDs, we must only connect NodeMCU to the computer using a
micro USB cable. No external component is required.
Fig: Built-in LEDs of NodeMCU

 On Board LED for ESP8266 relates to GPIO2.


 For NodeMCU, it relates to GPIO16
NodeMCU sketch for Built-in LED Blinking

This code is to blink built-in LEDs of NodeMCU. The on-board LED of ESP8266 is connected
to GPIO2 and the LED on NodeMCU board is connected to GPIO16.The NodeMCU ESP8266
board has two of those LEDs. One on the NodeMCU PCB and another on the ESP-12 module’s
PCB. Both LEDs operate in “inverted” mode, with regard to the pin levels - when the pin is
HIGH, the LED is off; when the pin is LOW, the LED is on. The LED on GPIO2 flashes during
ESP programming, as it is connected to the U1TXD pin.

Code:

#define LED1 D0 // Led in NodeMCU at pin GPIO16 (D0).


#define LED2 D4 // Led in ESP8266 at pin GPIO2 (D4).
void setup() { // setups the functionality of the pins
pinMode(LED1, OUTPUT); // initialize GPIO2 and GPIO16 as an output
pinMode(LED2, OUTPUT);
}
// the loop function runs forever
void loop() {
digitalWrit(LED1, LOW); // turn the LED off
digitalWrite(LED2, HIGH); //Turn the LED on (Note that LOW is the voltage level but
actually the LED is on; this is because it is active low on the ESP-
01)
delay(1000); // wait for a second
digitalWrite(LED1, HIGH); // turn the LED on
digitalWrite(LED2, LOW);
delay(1000); // wait for a second
}
B) BLINK AN EXTERNAL LED
LED has a two-pin interface. Both pins are to be given input supply to the LED. Long leg is for
positive supply the smaller one is for negative supply.

Fig: LED
Digital means either 0/1, in other words HIGH/LOW or ON/OFF. So, in the form of digital
output we shall get either +3.3V or 0V on digital pin of Arduino.

INTERFACING DIAGRAM OF NODEMCU WITH EXTERNAL LED


Make the circuit diagram on bread board according to connection diagram shown below. Anode
of the LED (the positive leg or long leg) is connected to the D1 pin of the NodeMCU, the
cathode of the LED (the negative leg or short leg) is connected with the one terminal of the
resistor and another terminal of the resistor is connected to the ground pin. The value of the
resistor in series with the LED may be of a different value than 200 ohm; the LED will lit up also
with values up to 1K ohm.

Fig: External LED interfacing with NodeMCU

Node MCU Sketch:

#define LED D1 // Assign LED pin i.e: D1 on NodeMCU


void setup() {
pinMode(LED, OUTPUT); // initialize GPIO 5 as an output
}
void loop() { // the loop function runs over and over again forever
digitalWrite(LED, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(LED, LOW); // turn the LED off
delay(1000); // wait for a second
}
PROCEDURE:
1. Make the connections as per the circuit diagram.
2. Check the ground connection precisely before switching on the power supply.
3. Connect Micro USB Cable to PC.
4. Write the Code in C/C++ accordingly.
5. Run the code through Arduino IDE and Observe the output.

PRECAUTIONS:
1. Check the sensor is working properly.
2. Check the connections are properly connected.
3. See that there are no errors in the code.
4. Check whether Ground and VCC connections are connected properly.

RESULT: Successfully implemented & Blink an On-Board LED and external LED on
NodeMCU.

2. ANALOG INPUT READ ON NODEMCU USING


POTENTIOMETER
OBJECTIVE: To read the analog input from the physical world using potentiometer and
display the voltage values in serial monitor.
COMPONENTS REQUIRED: 1) NodeMCU – 1
2) Micro USB Cable – 1
3) PC – 1
4) Jumper Wires.
5) Bread Board.
6) Potentiometer- 1 (10k ohm)

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:
A potentiometer is a simple mechanical device that provides a varying amount of resistance
when its shaft is turned. By passing voltage through a potentiometer and into an analog input on
your board, it is possible to measure the amount of resistance produced by a potentiometer
(or pot for short) as an analog value. To read an analog signal through the NodeMCU, Analog to
Digital conversion is required. Analog to Digital Converter (ADC) is used to convert analog
signal into digital form. A NodeMCU has inbuilt 10-bit ADC with only one ADC channel i.e. it
has only one ADC input pin to read analog voltage from external device. NodeMCU has on
board register divider network which provide 3.3V to the ADC pin of ESP8266. Hence, we can
use 0–3.3V range for ADC input voltage. Since ADC is of 10-bit resolution, it will give 0-1023
value range for ADC input voltage 0-3.3V on development Kit.

Fig: Potentiometer
ESP8266 ADC pin on NodeMCU Kit

Fig: NodeMCU ADC Pin

INTERFACING DIAGRAM
Fig: NodeMCU interface with Potentiometer
Connect the three wires from the potentiometer to your board. The first goes from one of the
outer pins of the potentiometer to ground. The second goes from the other outer pin of the
potentiometer to 3.3 volts. The third goes from the middle pin of the potentiometer to the analog
pin A0. The ADC inside the microcontroller converts the input voltage range, 0 to 3.3 volts, to a
digital value between 0 and 1023 using the command analogRead().

By turning the shaft of the potentiometer, you change the amount of resistance on either side of
the center pin (or wiper) of the potentiometer. This changes the relative resistances between the
center pin and the two outside pins, giving you a different voltage at the analog input. When the
shaft is turned all the way in one direction, there is no resistance between the center pin and the
pin connected to ground. The voltage at the center pin then is 0 volts, and analogRead() returns
0. When the shaft is turned all the way in the other direction, there is no resistance between the
center pin and the pin connected to +5 volts. The voltage at the center pin then is 5 volts, and
analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that
is proportional to the amount of voltage being applied to the pin.

Node MCU Sketch:

In the sketch below, the only thing that you do in the setup function is to begin serial
communications, at 9600 (or 115200) bits of data per second (bps), between your board and your
computer with the command:
Serial.begin(115200);

Next, in the main loop of your code, you need to establish a variable to store the resistance value
(which will be between 0 and 1023, perfect for an ‘int ‘ datatype) coming in from your
potentiometer:

int sensorValue = analogRead(A0);

Finally, you need to print this information to your serial monitor window. You can do this with
the command Serial.println () in your last line of code:

Serial.println(sensorValue);

Now, when you open your Serial Monitor in the Arduino Software (IDE) (by clicking the icon
that looks like a lens, on the right, in the green top bar or using the keyboard shortcut
Ctrl+Shift+M), you should see a steady stream of numbers ranging from 0-1023, correlating to
the position of the pot. As you turn your potentiometer, these numbers will respond almost
instantly.

Code:

void setup() {

Serial.begin(115200); // initialize serial communication at 115200 bits per second

void loop() { // the loop routine runs repeatedly forever

int sensorValue = analogRead(A0); // read the input on analog pin 0

Serial.print(“sensor =”); // print the variable name to on serial monitor to which


value is assigned

Serial.println(sensorValue); // print out the value you read


float Voltage = sensorValue* (3.3/1023.0) // convert the analog reading (0-1023) to a voltage
(0-3.3V)

Serial.println(Voltage); // print out the value you read

delay(1000); // delay in between reads for stability

Uploading the Code


Upload the code to the ESP8266. Make sure you have the right board and COM port
select. Go to Tools> Board and select the NodeMCU 1.0 (ESP8266 12-E Module).
Go to Programmer and choose USBasp as we are using USB cable to program it. Finally
choose the COM port on which the NodeMCU is connected. Press the Arduino IDE upload
button.
OUTPUT
After uploading the code, open the Serial Monitor at a baud rate of 115200. The
analog readings should be displayed.

Fig: Serial Monitor Window (Output)


Rotate the potentiometer and see the values increasing or decreasing.
PROCEDURE:
1. Make the connections as per the circuit diagram.
2. Check the ground connection precisely before switching on the power supply.
3. Connect Micro USB Cable to PC.
4. Rotate the shaft of potentiometer gently.
5. Write the Code in C/C++ accordingly.
6. Run the code through Arduino IDE and Observe the output.

PRECAUTIONS:
1. Check the potentiometer is working properly.

2. Check the connections are properly connected.

3. See that there are no errors in the code.

4. Check whether Ground and VCC connections are connected properly.

RESULT: Successfully implemented and verify the digital values by varying the resistance of
the potentiometer in serial monitor through NodeMCU.
3. LDR INTERFACING WITH NODEMCU
OBJECTIVE: To read the output voltage of LDR whose resistance changes depending upon
the intensity of incident light and display the voltage values in serial monitor.
COMPONENTS REQUIRED:
1) NodeMCU – 1
2) Micro USB Cable – 1
3) PC – 1
4) Jumper Wires.
5) Bread Board.
6) Resistor- 1 (10k ohm)
7) LDR / photoresistor

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:

An LDR or light dependent resistor is also known as photo resistor, photocell, photoconductor. It
is a one type of resistor whose resistance varies depending on the amount of light falling on its
surface. When the light falls on the resistor, then the resistance changes. These resistors are often
used in many circuits where it is required to sense the presence of light. These resistors have a
variety of functions and resistance. For instance, when the LDR is in darkness, then it can be
used to turn ON a light or to turn OFF a light when it is in the light. A typical light dependent
resistor has a resistance in the darkness of 1MOhm, and in the brightness a resistance of a couple
of KOhm.
Fig: LDR and its symbol
Working Principle of LDR
This resistor works on the principle of photo conductivity. It is nothing but, when the light falls
on its surface, then the material conductivity reduces and the electrons in the valence band of the
device are excited to the conduction band. These photons in the incident light must have energy
greater than the band gap of the semiconductor material. This makes the electrons to jump from
the valence band to conduction.

Fig: Working Principle of LDR

These devices depend on the light, when light falls on the LDR then the resistance decreases and
increases in the dark. When LDR is kept in the dark place, its resistance is high and, when the
LDR is kept in the light its resistance will decrease.

INTERFACING DIAGRAM
The connections are made as shown in the below image. One leg of LDR is connected to analog
pin (A0) and other leg is connected to 3.3v of NodeMCU. One end of the 10k resistor is
connected across the same analog leg of LDR and the other end is grounded.

Fig: LDR interfacing with NodeMCU

Node MCU Sketch:

In the sketch below, the only thing that you do in the setup function is to begin serial
communications, at 9600 bits of data per second (bps), between your board and your computer
with the command:

Serial.begin(9600);

Next, in the main loop of your code, you need to establish a variable to store the resistance value
(which will be between 0 and 1023, perfect for an ‘int ‘ datatype) coming in from your
potentiometer:
int sensorValue = analogRead(A0);

Finally, you need to print this information to your serial monitor window. You can do this with
the command Serial.println () in your last line of code:

Serial.println(sensorValue);

Now, when you open your Serial Monitor in the Arduino Software (IDE) (by clicking the icon
that looks like a lens, on the right, in the green top bar or using the keyboard shortcut
Ctrl+Shift+M), you should see a steady stream of numbers ranging from 0-1023, correlating to
the position of the pot. As you turn your potentiometer, these numbers will respond almost
instantly.

Code:

void setup() {

Serial.begin(9600); // initialize serial communication at 9600 bits per second

void loop() { // the loop routine runs repeatedly forever

int sensorValue = analogRead(A0); // read the input on analog pin 0

Serial.print(“sensor =”); // print the variable name to on serial monitor to which


value is assigned

Serial.println(sensorValue); // print out the value you read

float Voltage = sensorValue* (3.3/1023.0) // convert the analog reading (0-1023) to a voltage
(0-3.3V)

Serial.println(Voltage); // print out the value you read

delay(1000); // delay in between reads for stability

Uploading the Code


Upload the code to the ESP8266. Make sure you have the right board and COM port select. Go
to Tools> Board and select the NodeMCU 1.0 (ESP8266 12-E Module). Go to
Programmer and choose USBasp as we are using USB cable to program it. Finally choose the
COM port on which the NodeMCU is connected. Press the Arduino IDE upload button.
OUTPUT
After uploading the code, open the Serial Monitor at a baud rate of 9600. The analog voltage
value according to the varying resistance should be displayed.

PROCEDURE:
1. Make the connections as per the circuit diagram.
2. Check the ground connection precisely before switching on the power supply.
3. Connect Micro USB Cable to PC.
4. By varying the incident light on LDR, observe the voltage and note down the readings.
5. Write the Code in C/C++ accordingly.
6. Run the code through Arduino IDE and Observe the output.

PRECAUTIONS:
1. Check the potentiometer is working properly.

2. Check the connections are properly connected.

3. See that there are no errors in the code.

4. Check whether Ground and VCC connections are connected properly.

RESULT: Successfully implemented and verify the digital and analog voltage values in serial
monitor by varying the incident light on LDR through NodeMCU.
4. TEMPERATURE AND HUMIDITY MONITORING IN
CLOUD PLATFORM
OBJECTIVE: To read the ambient temperature and humidity by interfacing the DHT11
sensor with NodeMCU and monitor in cloud platform using esp8266.

COMPONENTS REQUIRED:
1) NodeMCU
2) DHT11 – Temperature and Humidity Sensor
3) Micro USB Cable
4) PC
5) Bread Board
6) Jumper Wires

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:
DHT11 Sensor

The DHT11 detects water vapor by measuring the electrical resistance between two electrodes.
The humidity sensing component is a moisture holding substrate with electrodes applied to the
surface. When water vapor is absorbed by the substrate, ions are released by the substrate which
increases the conductivity between the electrodes. The change in resistance between the two
electrodes is proportional to the relative humidity. Higher relative humidity decreases the
resistance between the electrodes, while lower relative humidity increases the resistance between
the electrodes. It can measure relative humidity in percentage (20 to 90% RH) and temperature in
degree Celsius in the range of 0 to 50°C.

There are two different versions of the DHT11 you might come across. One type has four
pins, and the other type has three pins and is mounted to a small PCB. The PCB mounted version
is nice because it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here
are the pin outs for both versions.

Fig: DHT11 Temperature and Humidity sensor (Two Different DHT11)

DHT11 Specifications:

 Operating Voltage: 3.5V to 5.5V .


 Operating current: 0.3mA (measuring) 60uA (standby).
 Output: Serial data.
 Temperature Range: 0°C to 50°C.
 Humidity Range: 20% to 90%.
 Resolution: Temperature and Humidity both are 16-bit.
 Accuracy: ±1°C and ±1%.

NodeMCU
NodeMCU is an open source development board and firmware based in the widely
used ESP8266 -12E WiFi module. It allows you to program the ESP8266 WiFi module with the
simple and powerful LUA programming language or Arduino IDE. ESP8266 Wi-Fi
transceiver is one of the most popular WiFi modules for IoT based Applications. With just a few
lines of code you can establish a WiFi connection and define input/output pins according to your
needs exactly like arduino, turning your ESP8266 into a web server and a lot more. It is the WiFi
equivalent of ethernet module. Now you have internet of things (IoT) real tool. With its USB-
TTL, the NodeMCU Dev board supports directly flashing from USB port. It combines features
of WIFI access point and station + microcontroller. These features make the NodeMCU
extremely powerful tool for Wifi networking. It can be used as access point and/or station, host a
webserver or connect to internet to fetch or upload data.
Features:
 Finally, programable WiFi module.
 Arduino-like (software defined) hardware IO.
 Can be programmed with the simple and powerful Lua programming language or Arduino
IDE.
 USB-TTL included, plug & play.
 10 GPIOs D0-D10, PWM functionality, IIC and SPI communication, 1-Wire and ADC A0
etc. all in one board.
 Wifi networking (can be used as access point and/or station, host a web server), connect to
internet to fetch or upload data.
 Event-driven API for network applications.
 PCB antenna.
ThingSpeak
Thingspeak is a data platform for monitoring your data online, targeted to be used for IoT
applications. In ThingSpeak channel you can set the data as private or public according to your
choice. ThingSpeak takes minimum of 15 seconds to update your readings.
Here, we are going to send Temperature and Humidity sensor data to Thingspeak
using DHT11. By this method we can monitor our DHT11 sensor’s temperature and humidity
data over internet using Thingspeak IOT server, and we can view the logged data and graph over
time on the ThingSpeak dashboard. NodeMCU reads the current temperature and humidity from
DHT11 and sends it to ThingSpeak server for live monitoring from anywhere in the world.

INTERFACING DIAGRAM
Fig: NodeMCU interface with DHT11
Procedure
Wiring the DHT11 to the NodeMCU is easy, but the connections are different depending on
which type you have either 3-pins or 4-pins. The wiring connections are made as follows:
 Signal pin of the DHT11 goes into Digital Pin D4 of the NodeMCU. (D4 is the GPIO 2
of NodeMCU)
 VCC pin of the DHT11 goes into +3v of the NodeMCU.
 GND pin of the DHT11 goes into Ground Pin (GND) of the NodeMCU.
Before you use the DHT11 with NodeMCU, you need to install the DHT11 library. It has all the
functions needed to get the humidity and temperature readings from the sensor.
Open up the Arduino IDE, then go to Sketch > Include Library > Manage
Libraries > Search DHT Sensor. Click on that entry, and then select Install.
Once the library has been added to the Arduino IDE, open the IDE and open the example sketch
named DHT_Test from the library added.

Node MCU Sketch:

#include <DHT.h>

#include <ESP8266WiFi.h>

String apiKey = "Your API KEY"; // Enter your Write API key here

const char *ssid = “WiFi Name"; // Enter your WiFi Name

const char *pass = "WiFi Password"; // Enter your WiFi Password

const char* server = "api.thingspeak.com";

#define DHTPIN 4 // GPIO Pin where the dht11 is connected

DHT dht(DHTPIN, DHT11);

WiFiClient client;

void setup()

{
Serial.begin(115200);

delay(10);

dht.begin();

Serial.println("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)

delay(550);

Serial.print(".");

Serial.println("");

Serial.println("WiFi connected");

void loop()

float h = dht.readHumidity();

float t = dht.readTemperature();

if (isnan(h) || isnan(t))

Serial.println("Failed to read from DHT sensor!");

return;

}
if (client.connect(server,80))

String postStr = apiKey;

postStr +="&field1=";

postStr += String(t);

postStr +="&field2=";

postStr += String(h);

postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");

client.print("Host: api.thingspeak.com\n");

client.print("Connection: close\n");

client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");

client.print("Content-Type: application/x-www-form-urlencoded\n")

client.print("Content-Length: ");

client.print(postStr.length());

client.print("\n\n");

client.print(postStr);

Serial.print("Temperature: ");

Serial.print(t);

Serial.print(" degrees Celcius, Humidity: ");

Serial.print(h);

Serial.println("%. Send to Thingspeak.");

}
client.stop();

Serial.println("Waiting...");

delay(10000);

Step 1: ThingSpeak Setup for Temperature and Humidity Monitoring

For creating your channel on ThingSpeak you first need to sign up on ThingSpeak. In case if you
already have account on ThingSpeak just sign in using your id and password.

For creating your account go to www.thingspeak.com

Click on signup if you don’t have account and if you already have account click on sign in.

After clicking on signup fill your details.


After this verify your E-mail id and click on continue.

Step 2: Create a Channel for Your Data

Once you Sign in after your account verification, Create a new channel by clicking “New
Channel” button

After clicking on “New Channel”, enter the Name and Description of the data you want to
upload on this channel. For example, I am sending DHT11 sensor data (temperature and
humidity), so I named it as “DHT11”.

Enter the name of your data ‘Temperature’ in Field1 and ‘Humidity’ in Field2. If you want to use
more than one Field, you can check the box next to Field option and enter the name and
description of your data.

After this click on save channel button to save your details.

Step 3: API Key

To send data to ThingSpeak, we need an unique API key, which we will use later in our code to
upload the Temperature and Humidity to ThingSpeak Website.

Click on “API Keys” button to get your unique API key for uploading DHT11 sensor data.
Now copy your “Write API Key”. We will use this API key in our code.

After this, navigate to your Thingspeak page and open your channel in ThingSpeak and output
will be displayed.

OUTPUT

Once code uploading is done open serial monitor to see whether wifi is connected or not. Make
sure the baud rate should be 115200. If wifi is connected then you can see the temperature and
humidity values displayed and data will be sent to thingspeak.

Open thingspeak channel and select pubic/private view. Here you can see the data uploaded after
the interval of 15 seconds.
Fig: Temperature & Humidity data monitoring in ThingSpeak (Output)

PROCEDURE:

1. Make the connections as per the circuit diagram.


2. Check the ground connection precisely before switching on the power supply.
3. Connect Micro USB Cable to PC.
4. Write the Code in Python or C/C++ accordingly.
5. Run the code through Arduino IDE and Observe the output.

PRECAUTIONS:

1. Check the sensor is working properly.


2. Check the connections are properly connected.
3. See that there are no errors in the code.
4. Check whether Ground and VCC connections are connected properly.

RESULT: Successfully implemented & monitored temperature and humidity in cloud platform
using ESP8266.
5. GAS SENSOR DETECTION USING NODEMCU AND
MONITORING IN CLOUD PLATFORM
OBJECTIVE: To interface the MQ-2 gas sensor with NodeMCU and monitor the values in
cloud platform using esp8266.

COMPONENTS REQUIRED:
1) NodeMCU
2) MQ-2 Gas Sensor
3) Micro USB Cable
4) PC
5) Bread Board
6) Jumper Wires

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:
MQ2 gas sensor is an electronic sensor used for sensing the concentration of gases in the air such
as LPG, propane, methane, hydrogen, alcohol, smoke and carbon monoxide. It is also known as
chemiresistor. It contains a sensing material whose resistance changes when it comes in contact
with the gas. This change in the value of resistance is used for the detection of gas. MQ2 is
a metal oxide semiconductor type gas sensor. Concentrations of gas in the gas is measured using
a voltage divider network present in the sensor. These voltage values are measured to know the
concentration of gas. The output voltage from the Gas sensor increases with the
concentration of gas. This sensor works on 5V DC voltage. It can detect gases in the
concentration of range 200 to 10000ppm.
Fig: MQ-2 Gas sensor

Pin Description:

Pin Pin Description


No: Name:

1 Vcc This pin powers the module, typically the operating voltage is +5V

2 Ground Used to connect the module to system ground

3 Digital You can also use this sensor to get digital output from this pin, by
Out setting a threshold value using the potentiometer

4 Analog This pin outputs 0-5V analog voltage based on the intensity of the gas
Out

Features:
 Operating Voltage is +5V
 Can be used to Measure or detect LPG, Alcohol, Propane, Hydrogen, CO and even
methane
 Analog output voltage: 0V to 5V
 Digital Output Voltage: 0V or 5V (TTL Logic)
 Preheat duration 20 seconds
 Can be used as a digital or analog sensor
 The Sensitivity of Digital pin can be varied using the potentiometer
INTERFACING DIAGRAM
1. Connect VCC pin of MQ-2 Gas Sensor module to Vin pin of NodeMCU ESP8266-12E
Board.
2. Connect GND pin of MQ-2 Gas Sensor module to GND pin of NodeMCU pin of
ESP8266-12E Board.
3. Connect D0 pin of MQ-2 Gas Sensor module to the A0 pin of NodeMCU ESP8266-12E
board.

Fig: MQ-2 Gas Sensor interface with NodeMCU

Node MCU Sketch:

String apiKey = "SKP9YQY2CFVNK919"; // Enter your Write API key from


ThingSpeak

const char *ssid = "…………"; // replace with your wifi ssid and wpa2 key

const char *pass = "…………";


const char* server = "api.thingspeak.com";

WiFiClient client;

void setup()

Serial.begin(115200);

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()

float h = analogRead(A0);

if (isnan(h))

Serial.println("Failed to read from MQ-5 sensor!");


return;

if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com

String postStr = apiKey;

postStr += "&amp;field1=";

postStr += String(h/1023*100);

postStr += "r\n";

client.print("POST /update HTTP/1.1\n");

client.print("Host: api.thingspeak.com\n");

client.print("Connection: close\n");

client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");

client.print("Content-Type: application/x-www-form-urlencoded\n");

client.print("Content-Length: ");

client.print(postStr.length());

client.print("\n\n");

client.print(postStr);

Serial.print("Gas Level: ");

Serial.println(h/1023*100);

Serial.println("Data Send to Thingspeak");

delay(500);

client.stop();
Serial.println("Waiting...");

delay(1500); // ThingSpeak needs minimum 15 sec delay between updates.

Setting Thingspeak & Getting API Key:


1. Go to https://thingspeak.com/ and create an account if you do not have one. Login to your
account.

2. Create a new channel by clicking on the button. Enter the basic details of the channel. Then
Scroll down and save the channel.

3. To send data to ThingSpeak, we need a unique API key. Click on “API Keys” button to get your
unique API key. Copy your API key and Paste in your Code for uploading Gas sensor data to
Thingspeak website.
4. Make sure to change the Wifi SSID, Password API key in the code.

After this, navigate to your Thingspeak page and open your channel in ThingSpeak and output
will be displayed.

OUTPUT
Once code uploading is done open serial monitor to see whether wifi is connected or not. Make
sure the baud rate should be 115200. If wifi is connected, then you can see the gas level
displayed in percentage and data will be sent to ThingSpeak.

Fig: Serial Monitor Window (Output)


You can now introduce gas or smoke or perfume at the MQ-2 and see the changes in a rise in the
percentage value of gas level.

Open ThingSpeak channel and select public/private view. Here you can see the data uploaded
after the interval of 15 seconds.
Fig: Gas Sensor data monitoring in ThingSpeak (Output)
PROCEDURE:
1. Make the connections as per the circuit diagram.
2. Check the ground connection precisely before switching on the power supply.
3. Connect Micro USB Cable to PC.

4. Introduce gas or smoke or perfume at the MQ-2 and see the changes in a rise in the
percentage value of gas level.
5. Observe the voltage and note down the readings.
6. Write the Code in C/C++ accordingly.
7. Run the code through Arduino IDE and Observe the output.

PRECAUTIONS:

1. Check the sensor is working properly.


2. Check the connections are properly connected.
3. See that there are no errors in the code.
4. Check whether Ground and VCC connections are connected properly.

RESULT:-
Hence the gas sensor senses the harmful gases present around it, by using NODEMCU
microcontroller.

6. SOIL MOISTURE SENSOR INTERFACING WITH


NODEMCU AND MONITORING IN CLOUD PLATFORM
OBJECTIVE: To interface the soil moisture sensor with NodeMCU and monitor the amount
of soil in cloud platform using esp8266.

COMPONENTS REQUIRED:
1) NodeMCU

2) Soil Moisture Sensor

3) Micro USB Cable

4) PC
5) Bread Board

6) Jumper Wires

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:
Soil moisture is basically the content of water present in soil. This can be measured using a soil
moisture sensor which consists of two conducting probes that act as a probe. It can measure the
moisture content in the soil based on the change in resistance between the two conducting plates.
The resistance between the two conducting plates varies in an inverse manner with the amount of
moisture present in the soil. When the sensor is inserted into the water, the resistance will
decrease and get better conductivity between plates.

Fig: Soil Moisture Sensor


The Soil Moisture sensor FC-28 has four pins
VCC: For power
A0: Analog output
D0: Digital output
GND: Ground
The Module also contains a potentiometer that will set the threshold value and then this threshold
value will be compared by the LM393 comparator. The output LED will light up and down
according to this threshold value.
Working of Resistive Soil Moisture Sensor
The soil moisture sensor consists of two probes that are used to measure the volumetric content
of water. The two probes allow the current to pass through the soil and then it gets the resistance
value to measure the moisture value. When there is more water, the soil will conduct more
electricity which means that there will be less resistance. Therefore, the moisture level will be
higher. Dry soil conducts electricity poorly, so when there will be less water, then the soil will
conduct less electricity which means that there will be more resistance. Therefore, the moisture
level will be lower.

The analog output of soil moisture sensor is processed using ADC. The moisture content in terms
of percentage is displayed on the serial monitor.

The output of the soil moisture sensor changes in the range of ADC value from 0 to 1023. This
can be represented as moisture value in terms of percentage using formula given below.

Analog Output = ADC Value / 1023

Moisture in percentage = 100 – (Analog output * 100)

For zero moisture, we get maximum value of 10-bit ADC, i.e. 1023. This in turn gives ~0%
moisture.

NodeMCU ADC can be used to measure analog voltage from soil moisture sensor.

INTERFACING DIAGRAM
Fig: Soil Moisture Sensor FC-28 Interface with NodeMCU
The wiring connections are made as follows:
1. Connect the two pins of the moisture sensor to the two pins on the Amplifier circuit
using jumper wires.
2. Connect the Vcc from the Amplifier to the 3.3V pin on the NodeMCU.
3. Connect the GND pin to the ground (GND) pin on the NodeMCU.
4. Connect the Analog pin to the A0 pin on the NodeMCU.
5. Connect NodeMCU to PC via a USB cable.
After your completed with wiring connections, then insert the sensor into the soil or place it in
anywhere you want.

Node MCU Sketch:

String apiKey = "14K8UL2QEK8BTHN6"; // Enter your Write API key from


ThingSpeak
const char *ssid = "…………"; // replace with your wifi ssid and wpa2 key

const char *pass = "………….";

const char* server = "api.thingspeak.com";

const int sensor_pin = A0; // Connect Soil moisture analog sensor pin
to A0 of NodeMCU

WiFiClient client;

void setup() {

Serial.begin(115200);

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()

int moisture_percentage;

moisture_percentage = ( 100.00 - ( (analogRead(sensor_pin)/1023.00) * 100.00 ) );


Serial.print("Soil Moisture(in Percentage) = ");

Serial.print(moisture_percentage);

Serial.println("%");

if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com

String postStr = apiKey;

postStr += "&field1=";

postStr += String(moisture_percentage);

postStr += "r\n";

client.print("POST /update HTTP/1.1\n");

client.print("Host: api.thingspeak.com\n");

client.print("Connection: close\n");

client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");

client.print("Content-Type: application/x-www-form-urlencoded\n");

client.print("Content-Length: ");

client.print(postStr.length());

client.print("\n\n");

client.print(postStr);

Serial.println("Data Send to Thingspeak");

client.stop();

Serial.println("Waiting...");
delay(2000); // ThingSpeak needs minimum 15 sec delay
between updates.
}

Monitoring Soil Moisture Sensor Data with NodeMCU on Thingspeak


In order to monitor the sensor data on Thingspeak Server, you first need to Setup the
Thingspeak. To setup the Thingspeak Server,
1. Go to https://thingspeak.com/ and create an account if you do not have one. Login to
your account.
2. Create a new channel by clicking on the button. Enter the basic details of the channel.
Then Scroll down and save the channel.
3. To send data to ThingSpeak, we need a unique API key. Click on “API Keys” button to
get your unique API key. Copy your API key and Paste in your Code for uploading Gas
sensor data to Thingspeak website.
4. After this, navigate to your Thingspeak page and open your channel in ThingSpeak and
output will be displayed.
5. Make sure to change the Wifi SSID, Password API key in the code.

After this, navigate to your Thingspeak page and open your channel in ThingSpeak and output
will be displayed.

OUTPUT

Once code uploading is done open serial monitor to see whether wifi is connected or not. Make
sure the baud rate should be 115200. If wifi is connected, then you can see the moisture level
displayed in percentage and data will be sent to ThingSpeak.

Fig: Serial Monitor Window (Output)


You can place the soil moisture sensor in various soil contents and see the changes in a rise in
the percentage value of moisture level.

Open ThingSpeak channel and select public/private view. Here you can see the data uploaded
after the interval of 15 seconds.

Fig: Soil Moisture sensor data monitoring in ThingSpeak (Output)


PROCEDURE:
1. Make the connections as per the circuit diagram.
2. Check the ground connection precisely before switching on the power supply.
3. Connect Micro USB Cable to PC.
4. Place the soil moisture sensor in various soil contents.
5. Observe the moisture levels and note down the readings.
6. Write the Code in C/C++ accordingly.
7. Run the code through Arduino IDE and Observe the output.
PRECAUTIONS:

1. Check the sensor is working properly.


2. Check the connections are properly connected.
3. See that there are no errors in the code.
4. Check whether Ground and VCC connections are connected properly.

RESULT:-
Successfully implemented and verify the moisture values of sensor placed in various soil
contents and monitor the values in cloud platform.
SERVO MOTOR INTERFACING TO NODEMCU
OBJECTIVE: To read the analog input from the physical world using potentiometer and
display the voltage values in serial monitor.
COMPONENTS REQUIRED: 1) NodeMCU – 1
2) Micro USB Cable – 1
3) PC – 1
4) Jumper Wires.
5) Bread Board.
6) Potentiometer- 1 (10k ohm)

SOFTWARE REQUIREMENTS:
1) Arduino IDE (with ESP8266 Library installed)

INTRODUCTION:
A servo motor is an electric device used for precise control of angular rotation. It is used in
applications that demand precise control over motion, like in case of control of robotic arm. It
works on PWM principle. The rotation angle of the servo motor is controlled by applying a
PWM signal to it. By varying the width of the PWM signal, we can change the rotation angle and
direction of the motor. Mostly 50Hz PWM is used to control the shaft position of servo motor.
To control servo motor shaft position within 180 degree it is required to control its PWM duty
cycle within 5% to 10%.

Fig: Servo Motor

INTERFACING DIGRAM
 Input of Servo (Yellow Wire) ---------- Digital Input Pin NodeMCU (D5)
 Vcc of Servomotor (Red Wire) ------------ Vcc Of Esp32
 Gnd of Servo (Black Wire)----------------- Gnd Of Esp32

Node MCU Sketch:

#include <Servo.h> // including servo library.

Servo servo_1; // Giving name to servo.

void setup (){

servo_1.attach(0); // Attaching Servo to D

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);
Servo_1.write(135); //servo will move to 135 degree angle.

Delay(1000);

Servo_1.write(180); //servo will move to 180 degree angle.

PROCEDURE:

1.) Make the connections as per the circuit diagram.


2.) Check the ground connection precisely before switching on the power supply.
3.) Connect Micro USB Cable to PC.
4.) Observe the angle of rotaion and note down the readings.
5.) Write the Code in C/C++ accordingly.
6.) Run the code through Arduino IDE and Observe the output.

PRECAUTIONS:

1. Check the sensor is working properly.

2. Check the connections are properly connected.

3. See that there are no errors in the code.

4. Check whether Ground and VCC connections are connected properly.

RESULT: Successfully implemented & controlled Servo motor using NodeMCU.

You might also like