0% found this document useful (0 votes)
1K views58 pages

R20 JNTUK EEE IOT LAB Manual Final Print

This document provides instructions for installing and familiarizing with the Arduino IDE software. It describes downloading and installing the Arduino IDE, the basic structure of Arduino sketches with Setup and Loop functions, and an overview of the digital and analog pins on the Arduino Nano board. It also includes instructions for installing the CH340G USB driver required to interface the Arduino Nano with a computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views58 pages

R20 JNTUK EEE IOT LAB Manual Final Print

This document provides instructions for installing and familiarizing with the Arduino IDE software. It describes downloading and installing the Arduino IDE, the basic structure of Arduino sketches with Setup and Loop functions, and an overview of the digital and analog pins on the Arduino Nano board. It also includes instructions for installing the CH340G USB driver required to interface the Arduino Nano with a computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Gayatri Vidya Parishad college of Engg.

For women

Internet of Things
Laboratory

LABORATORY MANUAL
INTERNET OF THINGS
(SKILL COURSE)
(By V.V.V. SATYANARAYANA)
ELECTRICAL AND ELECTRONICS
ENGINEERING
(R20 – JNTUK – KAKINADA)

1
INSTITUTE VISION

To emerge as an acclaimed centre of learning that


provides value-based technical education for the
holistic development of students

MISSION
Undertake the activities that provide value-based
knowledge in Science, Engineering, and Technology
Provide opportunities for learning through industry-
institute interaction on the state-of the-art
technologies
Create a collaborative environment for research,
innovation, and entrepreneurship Promote activities
that bring in a sense of social responsibility

2
DEPARTMENT VISION

To develop into a centre of learning that empowers


students with contemporary knowledge in Electrical
& Electronics Engineering for their overall growth

MISSION
Impart skills both in traditional and modern areas of
Electrical & Electronics Engineering
Provide exposure to latest developments in the field
through Seminars, Industrial visits, Workshops and
Paper presentations
Prepare the young minds to apply professional
engineering practices by considering environmental
and societal needs

3
INDEX
1. LIST OF EXPERIMENTS (SYLLABUS) [5]
2. EXPERIMENTS LIST CONDUCTED IN THIS LABORATORY [6]
3. EXPT-1 INSTALLATION OF ARDUINO IDE [7-14]
4. EXPT-2 INTERFACING OF LED/BUZZER [16-17]
5. EXPT-3 INTERACING OF PUSH BUTTON/IR/LDR SENSOR [18-20]
6. EXPT-4 INTERFACING OF DHT11 HUMIDITY AND TEMPERATURE
SENSOR [21-23]

7. EXPT-5 INTERFACING OF OLED [24-26]


8. EXPT-6 INTERFACING OF BLUETOOTH TO SEND SENSOR DATA
TO ANDROID PHONE [27-30]
9. EXPT-7 INTERFACING OF BLUETOOTH WITH ARDUINO TO
CONTROL ITS LED UPON RECEIVING 0/1 [27-30]
10.EXPT-8 POSTING DATA OF HUMIDITY AND TEMPERATURE TO
THINGSPEAK CLOUD USING ARDUINO IDE AND NODEMCU
[31-46]
11.EXPT-9 INTERFACING OF 7-SEGMENT DISPLAY [47-49]
12.EXPT10- ANALOG INPUT AND DIGITAL OUTPUT [50-51]
13.EXPT11 - NIGHT LIGHT CONTROL AND MONITORING SYSTEM
[52-53]
14.EXPT-12 FIRE ALARM [54-57]

15. TRAINER BOARD USED IN THIS LAB [58]

4
List of Experiments: (As per R20 regulation of JNTUK- Kakinada)
Any TEN of the following Experiments are to be conducted
1. Familiarization with Arduino/Raspberry Pi and perform necessary software
installation.
2. To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to turn ON
LED for 1 sec after every 2 seconds.
3. To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and
write a program to turn ON LED when push button is pressed or at sensor detection.
4. To interface temperature sensor with Arduino/Raspberry Pi and write a program to
print temperature and humidity readings.
5. To interface Organic Light Emitting Diode (OLED) with Arduino/Raspberry Pi
6. To interface Bluetooth with Arduino/Raspberry Pi and write a program to send sensor
data to smartphone using Bluetooth.
7. To interface Bluetooth with Arduino/Raspberry Pi and write a program to turn LED
ON/OFF when „1‟/‟0‟ is received from smartphone using Bluetooth.
8. Write a program on Arduino/Raspberry Pi to upload and retrieve temperature and
humidity data to thingspeak cloud.
9. 7 Segment Display
10. Analog Input & Digital Output
11. Night Light Controlled & Monitoring System
12. Fire Alarm Using Arduino
13. IR Remote Control for Home Appliances
14. A Heart Rate Monitoring System
15. Alexa based Home Automation System

5
EXPERIMENTS CONDUCTED IN THE IoT LAB – GVPCEW

1. Familiarization with Arduino/Raspberry Pi and perform necessary software installation.


2. To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to turn ON
LED for 1 sec after every 2 seconds.
3. To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and
write a program to turn ON LED when push button is pressed or at sensor detection.
4. To interface temperature sensor with Arduino/Raspberry Pi and write a program to
print temperature and humidity readings.
5. To interface Organic Light Emitting Diode (OLED) with Arduino/Raspberry Pi
6. To interface Bluetooth with Arduino/Raspberry Pi and write a program to send sensor
data to smartphone using Bluetooth.
7. To interface Bluetooth with Arduino/Raspberry Pi and write a program to turn LED
ON/OFF when „1‟/‟0‟ is received from smartphone using Bluetooth.
8. Write a program on Arduino/Raspberry Pi to upload and retrieve temperature and
humidity data to thingspeak cloud.
9. 7-segment display
10. Analog Input & Digital Output
11 Night Light Controlled & Monitoring System
12. Fire Alarm Using Arduino

6
EXPT 1:

INSTALLATION AND FAMILIARIZATION OF ARDUINO IDE

Step 1: To install the Arduino IDE use the following link

https://www.arduino.cc/en/software

Click on windows win7 and newer option to download the file

7
Double click on the executable file highlighted in the oval and proceed to installation.

During installation, it may ask you for permissions and throw warnings also. Surely you can
give access to all permissions.

The installation may take several minutes depending on the configuration of the system. After

successful installation, one may find the green coloured icon of Arduino on desktop.
Double click that to open a new sketch.

The basic Arduino sketch contains two parts

8
1. Setup function
2. Loop function

The code written inside setup function gets executed, line by line, only once. i.e if the users
presses the RESET button on the Arduino UNO or Nano, the code written under Setup will
get executed only once. Then directly the controller jumps to Loop function and executes
the code for ever until a RESET takes place

Under the setup function, Pin declarations like INPUT or OUTPUT, initialization of Serial
modules like Serial.begin(baud rate), initialization of OLED display, initialization of seven
segment module TM1637, initialization of DHT11 sensor etc. have to be written.

Under “loop” function, the process that is to be done for ever is written. The “loop” gets
executed line by line until it reaches the end and repeats from the first line of code written
under the “loop” and so on.

About the pins on ARDUINO Nano:

ARDUINO NANO BOARD

There are a total of 14 digital input and output pins marked as 0,1,2,.... 12,13 known as
Digital pins. Out of the 14, pin 0 and 1 are used for Hardware UART by the controller to
communicate with the Personal Computer used for programming. Hence pins 0 and 1 should
not be used for connecting digital input output devices.

CONTROL OF DIGITAL PINS IN SOFTWARE

******** ALL CODES WRITTEN HERE ARE CASE-SENSITIVE *************

DONOT CHANGE THE CASE OF ANY OF THE LETTER IN THE NAME OF THE
FUNCTION *****************

pinMode(pin, INPUT); // Declares the “pin” as input

pinMode(pin, OUTPUT); // Declares the “pin” as output

9
pinMode(pin, INPUT_PULLUP); // Declares the “pin” as input with pin being held at HIGH
(5volts for Arduino Nano)

Driving the pin to HIGH or LOW:

digitalWrite(pin, HIGH); // makes the “pin” at 5 Volt

digitalWrite(pin, LOW); // makes the “pin” at 0 Volt

There are 8 analog (INPUT) pins on Arduino named as A0,A1,A2,A3,A4,A5,A6 and A7. They
can be configured as digital input and output pins also. In addition to this they are special pins
as they can read the analog signal voltages on the pins (< 5 volts). There is a 10 bit ADC on the
controller which multiplexes with all the 8 pins on the Arduino Board. There is an internal
voltage reference of 1.1Volt which can be invoked in the setup code with the following syntax

analogReference(INTERNAL);

// SETS REFERENCE VOLTAGE FOR 10 BIT ADC AS1.1V INTERNALLY

// RESOLUTION WILL BE 1.1/1024 = 1.07 milli Volt

If no such reference is used, the controller assumes Vcc (=5 Volt) of the supply as reference
voltage and hence gives a resolution of 5/1024 = 4.88 milli Volt.

For using the ADC,

int val = analogRead(pin);

// “int” is type of variable, which is integer variable named as “val”

// pin refers to the pin number used as analog input, say A0,A5 etc.

Pins A4 and A5 are multiplexed with I2C communication pins (I2C = Inter Integrated
Communication protocol designed by Philips semiconductors during 1982, which is used for
communication between devices on a circuit board, A4 is serial data pin, A5 is serial
clock pin)

All analog input pins, A0 though A7 can be configured as digital pins also.

Ex:

digitalWrite(A5,OUTPUT);

digitalWrite(A2,INPUT);

10
Installation of drivers for Nano:

Arduino Nano comes with an on board TTL – USB chip name CH340G. It needs driver
software to connect to the personal computer.

Use the following link to download the driver https://sparks.gogo.co.nz/ch340.html

11
Extract the contents of zip file named CH341SER.zip and click on install to install the
driver.

Now go to device manager using the following.

Connect the Nano board with USB cable to the Computer. Press the Windows key +R to
open the run command. Then type

devmgmt.msc

in the dialogue box. This will open the device manager of your computer

Here one may find the item “ports (Com &LPT)” and further expansion of the tab shows the
communication ports connected to the computer, in the above case it is USB-SERIAL

12
CH340 (COM4), the COM number may vary from system to system. Remember this COM
port number.

Go to Arduino IDE and select tools/Board/ AVR microcontrollers/Arduino Nano

Now select the processor,

(In my case it is ATMega328p with old boot loader)

Then select the PORT,

13
The port in my case is COM4.

Now it is everything ready to go with the Arduino Nano Board.

To open the Serial monitor/Plotter one may use the Tools menu and select the option.

Or one may directly click on the lens symbol in the top-right corner of the window
highlighted below. The Serial plotter is graph plotter which graphs the serial incoming data.
One may use even the keyboard short cut keys like

ctrl+shift+M

or

ctrl+shift+L

14
A view of the serial monitor. The settings at bottom show the change of BAUD RATE,
Auto Scroll and Time stamp

Baud rate must be the same as the one written in the code while initializing the serial
module using

Serial.begin(9600); or

Serial.begin(115200);

15
Expt 2: To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to turn
ON LED for 1 sec after every 2 seconds.

Aim: To blink the Light Emitting Diode connected to Arduino Nano to blink it ON for 1
second and OFF for 2 seconds

Apparatus: Trainer board containing Arduino Nano, USB cable, LED with Series resistor
(470 Ohm), connecting jumper wires

Circuit diagram:

Use the following sketch and make the connections on the board as above.

Take a wire and connect it from D13 pin of Arduino Nano to The pin Marked as L1 on the
Board. You may use any L1, L2, L3 or L4 : L1= RED, L2=Green, L3= Yellow, L4=Blue

/*

Task 3 :

To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and

Write a program to turn ON LED when push button is pressed or at sensor detection

*/

#define pin 13 // set pin number at which this led toggle must take place: : connect the
buzzer, if required, at same pin

void setup() {

pinMode(pin,OUTPUT);// set the pin as output, define the pin number above

16
void loop() {

digitalWrite(pin,HIGH); // set the pin high

delay(1000);// set it high for 1000 millisec, i.e. 1 sec

digitalWrite(pin,LOW); // set the pin low

delay(2000);// set it low for 2000 millisec, i.e 2 sec

// this program is not suitable if one wants to execute other tasks with the controller
(arduino nano)

// rather use millis() function to avoid blocking the processor during delay function

Output:

LED is blinking for every two seconds as per the requirement

17
Expt 3: To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and
write a program to turn ON LED when push button is pressed or at sensor detection.

AIM: To connect the Push button to Arduino Nano and turn ON the LED when the push
button is closed

Apparatus: Trainer board containing Arduino Nano, USB cable, LED with Series
resistor(470 Ohm), Push button with 10k Ohm resistor , connecting jumper wires

Circuit diagram:

Take two wires. Connect one wire between Arduino Nano D13 and any LED, say L1.
Connect the other wire between SWTCH1 or SWTCH2 Pin and D2 pin of Arduino

Code:

/*

Task 3 :

To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and

write a program to turn ON LED when push button is pressed or at sensor detection

*/

#define pin 13 // set pin number at which this led toggle must take place: : connect the
buzzer, if required, at same pin

#define switch 2 // define the switch pin as digital 2

void setup() {

18
pinMode(pin,OUTPUT);// set the pin as output, define the pin number above

pinMode(switch,INPUT); // Switch connected to pin as input

Serial.begin(9600); // start the serial module

void loop() {

if(!digitalRead(switch))// IF SWITCH IS LOW, (i.e. not pressed)

digitalWrite(pin,LOW); // set the pin low

Serial.println("key not pressed");

else //if switch is pressed

digitalWrite(pin,HIGH);Serial.println(“key pressed”);

Output:

19
20
EXPT4: To interface temperature sensor with Arduino/Raspberry Pi and write a program to
print temperature and humidity readings.

Aim: To interface the DHT11 sensor with Arduino Nano and write a program to print the
temperature and humidity values on Serial monitor.

Apparatus: Arduino Trainer kit, DHT11 sensor, connecting wires

Circuit diagram:

On the Arduino kit, connect the signal pin near DHT11 with Arduino Nano‟s digital pin 2.

Before uploading the code to the board, download the following libraries to Arduino
IDE. Press shift+ctrl+I on the keyboard with Arduino IDE active. Type “DHT sensor
Adafruit” in the search box. Click install on the Adafruit DHT sensor library. If it asks
for additional libraries install please click “install all”.

Or go to the following URLs and download zip files.

Go to sketch/include library/ add zip library

When prompted choose the path to the above downloaded zip files

// Install the following Arduino libraries:

// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library

// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include <Adafruit_Sensor.h>

21
#include <DHT.h>

#include <DHT_U.h>

#define DHTPIN 2 // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11 // DHT 11

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {

Serial.begin(9600);

// Initialize device.

dht.begin();

Serial.println(F("DHTxx Unified Sensor Example"));

// Print temperature sensor details.

sensor_t sensor;

dht.temperature().getSensor(&sensor);

dht.humidity().getSensor(&sensor);

delayMS = sensor.min_delay / 1000;

void loop() {

// Delay between measurements.

delay(delayMS);

// Get temperature event and print its value.

sensors_event_t event;

dht.temperature().getEvent(&event);

if (isnan(event.temperature)) {

22
Serial.println(F("Error reading temperature!"));

else {

Serial.print(F("Temperature: "));

Serial.print(event.temperature);

Serial.println(F("°C"));

// Get humidity event and print its value.

dht.humidity().getEvent(&event);

if (isnan(event.relative_humidity)) {

Serial.println(F("Error reading humidity!"));

else {

Serial.print(F("Humidity: "));

Serial.print(event.relative_humidity);

Serial.println(F("%"));

Output:

On the serial monitor one may observe the outputs

23
Expt 5

To interface Organic Light Emitting Diode (OLED) with Arduino/Raspberry Pi

Aim: To interface the SSD1306 OLED with Arduino IDE and Print a name on it

Apparatus: SSD1306 OLED 0.96 inch display, Arduino Nano, Connecting wires

Circuit diagram:

On the Arduino kit , the Serial Clock and Data pins (A5, A4) are connected to the OLED
SCL and SDA pins. Hence no connections are needed to be done.

Code:

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

24
// The pins for I2C are defined by the Wire-library.

// On an arduino UNO: A4(SDA), A5(SCL)

#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

#define SCREEN_ADDRESS 0x3C // SET THE OLED I2C ADDRESS AS 0X3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire,


OLED_RESET);

void setup() {

Serial.begin(9600);

display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

display.clearDisplay();// CLEAR EVERYTHING IN BUFFER

delay(2000); // Pause for 2 seconds

display.display();// SHOW DISPLAY

void loop() {

display.setTextSize(1); // Normal 1:1 pixel scale

display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor(0,0); // Start at top-left corner

display.println(F("Hello, world!"));

display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text

display.println("HELLO WORLD");

display.setTextSize(2); // Draw 2X-scale text

display.setTextColor(SSD1306_WHITE);

25
display.print(F("IoT Lab"));

display.display();

delay(2000);

Output:

The Text mentioned in the above program got printed on the OLED screen

26
Experiments 6 and 7:

To interface Bluetooth with Arduino and write a program to send sensor data to smart phone
using Bluetooth.
To interface Bluetooth with Arduino and write a program to turn LED ON/OFF when
„1‟/‟0‟ is received from smart phone using Bluetooth.

Aim: To interface the HC-05 Bluetooth module with Arduino Nano and establish
bidirectional communication between smart phone and Arduino Nano.

Apparatus:

Hardware: HC-05 Bluetooth module, Arduino Nano trainer kit, connecting wires, Android
smart phone ,Thermistor sensor for sending data , on board LEDs

Software: Android app: HC-05 Bluetooth terminal, Arduino IDE

Circuit diagram:

1. Connect the digital pin 2 of Arduino Nano with BT_Tx pin (TX pin of HC-05)
2. Connect the digital Pin 3 of Arduino Nano with BT_RX pin (RX pin of HC-05)
3. Connect the analog output pin of Thermistor sensor to analog input pin A3 of Arduino
Nano.
4. Connect a wire between On-board LED say L1 (red LED) with digital pin 13 of Arduino

Code for Arduino:

#include <SoftwareSerial.h>

27
SoftwareSerial ss(2,3);// 2 is rx, 3 is tx for Arduino

#define pin A3 // connect the thermsitor sensing pin at A3 of arduino

// subroutines

float get_average(int pin){

float z=0;// intialize z to zero

for(int i=0;i<1000;i++)z=z+analogRead(pin); // take the sum of 1000 analogRead() values.

z=z/1000.00;// get average analogRead() value from 1000 data points

return z;// return the value of average

void setup() {

// put your setup code here, to run once:

ss.begin(9600);

Serial.begin(9600);

ss.setTimeout(100);

pinMode(13,OUTPUT);

pinMode(A3,INPUT);

unsigned long tim=0;float temp,x,y;

void loop() {

// put your main code here, to run repeatedly:

if(ss.available()>0){

String dat =ss.readString();

dat.trim();

if(dat=="0"){

digitalWrite(13,LOW);Serial.println("led is off");//ss.println("off");

28
if(dat=="1"){

digitalWrite(13,HIGH);Serial.println("led is on");}

//ss.println("on");

if(millis()-tim>1000){// check for every one second

y=get_average(pin); // get the average value of analogRead();

x=(1.00/298.15)+(1.00/3950.00)*log(1.00*y/(1024.00-y));

temp=1.000/x;

ss.println(String(temp-273.15)+String("Celsius"));

Serial.println(String(temp-273.15)+String("Celsius"));

tim=millis();

Upload the above code to Arduino Nano.

Open the Android phone with HC-05 bluetooth terminal App from play store

Drag the menu on the Android phone and switch on the Bluetooth connection

Search for the Bluetooth devices

You will find the Bluetooth with name HC-05 on the search

Try to pair it

It asks for a pairing code.

The default code is 1234

29
Once after successful paring, open the HC-05 bluetooth terminal App to see the
available devices. You will find the HC-05 along with its MAC ID

Just select the HC-05, After few seconds it shows the device connected.

Before connecting to terminal the HC-05 BT module will blink continuously. But after
pairing it with Bluetooth terminal App, the blinking time gets slowed down indicating
the paired communication.

You will notice the temperature value getting printed on the Terminal window of App.

There you will find the Tab “send ASCII”.

Type 0 and press Send ASCII button.

LED connected to pin 13 will glow

Type 1 and press send Ascii button.

LED connected to pin 13 will turn off.

30
EXPT8:

Write a program on Arduino/Raspberry Pi to upload and retrieve temperature and


humidity data to thingspeak cloud.

BEFORE YOU START USING THE NODEMCU BOARD DO THE


FOLLOWING:

INSTALLATION OF ARDUINO CORE FOR


NODEMCU

Type here for search with keyword ESP8266, you will find the link

31
https://arduino.esp8266.com/stable/package_esp8266com_index.json

copy this link and paste it in the additional boards manager URL window

NOW NAVIGATE TO

32
THEN YOU GET A NEW WINDOW..

THEN TYPE NODEMCU... AND CLICK INSTALL BUTTON.. YOU WILL NOT GET THE REMOVE BUTTON
INITIALLY. AFTER YOU INSTALL THE CORE, THEN YOU WILL FIND THE REMOVE BUTTON

It might take a while to complete the installation process..

After installation you will find..

33
THIS COMPLETES THE CORE
INSTALLATION. NOW YOU MAY
PROCEED WITH THE EXPERIMENT.
Aim: To Post the temperature and Humidity data to cloud using NodeMCU and Arduino
IDE using Thingspeak cloud

Apparatus:

Software: Thingspeak cloud account with valid write and read API keys, NodeMCU IoT
core for Arduino IDE

Hardware: NodeMCU board, DHT11 temperature and humidity sensor connecting wires

Circuit diagram:

34
1. CONNECT THE VCC PIN ON NODEMCU TO 3.3 VOLT ON NODEMCU
2. CONNECT THE GND PIN ON NODEMCU TO GND PIN
3. CONNECT THE JUMPER CLIP ON THE BOTTOM OF THE BOARD NAMED
3V3/VCC/5V. SHORT THE CLIP WITH VCC AND 3.3V
4. CONNECT THE SIGNAL PIN TO D2 PIN ON NODEMCU

CODE:

PRESS SHIFT+CNTRL+I on Arduino IDE to go to library manager. Make sure that


have selected NodeMCU board under Tools menu as described above. Then in the
library manager, type “thingspeak”. You will find “Thingspeak by Mathworks”
library. Just click install on it.

35
Then goto File/Examples/Thingspeak/ESP8266/program board directly/write multiple
fields example sketch. (like the following screenshot)

36
Open the tab named secrets.h given in the sketch as shown below.

Use your mobile phone hot spot for connecting to internet. Switch on the data and hotspot
on your mobile phone. In the secrets.h file, enter your SSID (of your hotspot) under the
double quotations

#define SECRET_SSID "wifihotspot"

37
#define SECRET_PASS "12345678"

Here in this example my hot spot is named as “wifihotspot” with set password 12345678.

DONOT USE “SPACES” IN THE HOT SPOT, SAY “anu galaxy”. SIMILARLY
PASSWORD MUST BE OF 8 CHARACTERS LONG. YOU MAY USE _ OR ANY
OTHER SYMBOLS FOR SSID AS WELL AS PASS WORD.

Now go to thingspeak cloud

Enter your email and password

You will get a screen like the following

38
Create a new channel with desired name

IN MY CASE THE NAME OF CHANNEL IS SATYANARAYANA

THE FIELD 1 IS NAMED AS HUMIDITY

THE FIELD 2 IS NAMED AS TEMPERATURE

SCROLL DOWN….. TO SEE THE FOLLOWING

39
CLICK ON SAVE CHANNEL.

YOU WILL GET A NEW CHANNEL DASH BOARD LIKE THE FOLLOWING

NOW CLICK ON API KEYS TAB

40
NOTE DOWN YOUR CHANNEL ID, THE NUMBER WILL DIFFERENT FOR
YOU (THAN WHAT IS SHOWN ABOVE)

CLICK ON API KEYS TAB TO GET THE FOLLOWING

GET YOUR WRITE API KEY FROM YOUR DASH BOARD (COVERED YELLOW
BOX IN MY CASE)

NOW COMEBACK TO ARDUINO SKETCH/ SECRETS.H FILE:

41
ENTER YOUR CHANNEL ID AND YOUR WRITE APIKEY HERE AND SAVE
THE SKETCH.

SWITCH TO WRITE MULTIPLE FIELDS TAB.

Use the following code:

#include <ESP8266WiFi.h>

#include "secrets.h"

#include "ThingSpeak.h" // always include thingspeak header file after other header
files and custom macros

#include <Adafruit_Sensor.h>

#include <DHT.h>

#include <DHT_U.h>

#define DHTPIN 2 // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11 // DHT 11

// See guide for details on sensor wiring and usage:

// https://learn.adafruit.com/dht/overview

42
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

char ssid[] = SECRET_SSID; // your network SSID (name)

char pass[] = SECRET_PASS; // your network password

int keyIndex = 0; // your network key Index number (needed only for WEP)

WiFiClient client;

unsigned long myChannelNumber = SECRET_CH_ID;

const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values

float number1 = 0;

float number2 = 0;

String myStatus = "";

void setup() {

Serial.begin(115200); // Initialize serial

WiFi.mode(WIFI_STA);

ThingSpeak.begin(client); // Initialize ThingSpeak

// start the dht sensor

dht.begin();

Serial.println(F("DHTxx Unified Sensor Example"));

// Print temperature sensor details.

sensor_t sensor;

43
dht.temperature().getSensor(&sensor);

dht.humidity().getSensor(&sensor);

delayMS = sensor.min_delay / 1000;

void loop() {

// Connect or reconnect to WiFi

if(WiFi.status() != WL_CONNECTED){

Serial.print("Attempting to connect to SSID: ");

Serial.println(SECRET_SSID);

while(WiFi.status() != WL_CONNECTED){

WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if


using open or WEP network

Serial.print(".");

delay(5000);

Serial.println("\nConnected.");

// now get the dht sensor data from sensor

sensors_event_t event;

dht.temperature().getEvent(&event);

if (isnan(event.temperature)) {

44
Serial.println(F("Error reading temperature!"));

else {

Serial.print(F("Temperature: "));

Serial.print(event.temperature);

Serial.println(F("°C"));

number2 = event.temperature;

// Get humidity event and print its value.

dht.humidity().getEvent(&event);

if (isnan(event.relative_humidity)) {

Serial.println(F("Error reading humidity!"));

else {

Serial.print(F("Humidity: "));

Serial.print(event.relative_humidity);

Serial.println(F("%"));

number1= event.relative_humidity;

// set the fields with the values

ThingSpeak.setField(1, number1);//send humidity field 1

ThingSpeak.setField(2, number2);// send temperature to field2

// write to the ThingSpeak channel

int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

45
if(x == 200){

Serial.println("Channel update successful. Temp and Humidity values posted to


cloud sucessfully");

else{

Serial.println("Problem updating channel. HTTP error code " + String(x));

delay(20000); // Wait 20 seconds to update the channel again

************************************************ End of code

Now upload the code by selecting the board as NODEMCU 1.0 (ESP12E) and with
correct COM port number. It takes a while to upload the code, say 2 minutes.

After successful upload the NodeMCU tries to connect first to wifi hot spot and then
posts humidity and temperature data on the thingspeak dash board for every 20
seconds delay.

46
Expt: 9

Interfacing of 7 segment display TM1637

Aim: To interface TM1637 display with Arduino Nano and print numbers

Apparatus: TM1637 seven segment display, Arduino Nano board, connecting wires

Software library: Grove – 4 digit display

Circuit diagram:

1. Take four female to male type connecting wires and make the following
connections
2. Vcc pin on TM1637 === Arduino 5V pin
3. Gnd pin on TM1637 == Arduino Gnd pin
4. CLK pin on TM1637 == Arduino Digital pin 2
5. DIO pin on TM1637 = Arduino Digital pin 3

Install the library for TM1637 using Arduino IDE by hitting

CTRL+SHIFT+I, type tm1637 and install grove 4- digit display library

After successful installation, use the following code to upload to Arduino Nano.

47
Code:

#include <TM1637.h>

int CLK = 2;

int DIO = 3;

TM1637 tm(CLK,DIO);

void setup() {

// put your setup code here, to run once:

tm.init();

//set brightness; 0-7

tm.set(2);

void loop() {

// put your main code here, to run repeatedly:

// example: "12:ab"

// tm.display(position, character);

tm.display(0,1);

tm.display(1,2);

tm.point(1);

tm.display(2,10);

tm.display(3,11);

48
delay(1000);

// example: "1234"

displayNumber(1234);

delay(1000);

void displayNumber(int num){

tm.display(3, num % 10);

tm.display(2, num / 10 % 10);

tm.display(1, num / 100 % 10);

tm.display(0, num / 1000 % 10);

******************** end of the code **************************

Here

tm.display(position, character);

function uses the following for characters.

Ex: tm.display(0,12); // prints the charcter “C” in the ones place of the 4 digit display

tm.display(2, 1);// prints the number 1 in the 100’s place and so on

49
Expt:10 Analog Input and digital Output

Aim: To interface analog sensor (LDR/Thermistor) to the Arduino and print the Analog
output on serial monitor

Apparatus: Arduino Nano kit, Thermistor , LDR sensors, connecting wires

Circuit diagram:

Connect the output sensing pin of either LDR or Thermistor to A3 pin of Arduino
Nano.

Upload the following code

Code:

void setup() {

pinMode(A3,INPUT);// CONNECT THE fire sensor TO A3

Serial.begin(9600);// start the serial communication with baud rate 9600

void loop(){

int analogvalue = analogRead(A3);

Serial.print("Sensor value is : ");

50
Serial.println(analogvalue);

delay(20);// give a delay of 20 milli second between each measurement

Then select the serial monitor by pressing

SHIFT+CTRL+M

Select the baud rate of 9600 on the serial monitor to see the printed values of analogRead on the
screen

51
Expt 11:

Night Light Controlled & Monitoring System

Aim: To control and LED based on the light intensity sensed by LDR and to print the
intensity of light on to the serial monitor.

Apparatus: Arduino Nano trainer kit, LDR sensor, LED, connecting wires

Circuit diagram:

1. Connect the analog Output of LDR sensor with A3 pin of Arduino Nano.
2. Connect a wire to one of the LEDs L1,L2,L3,L4 on the board
3. Upload the following code to board.

Code:

#define pin A3

void setup() {

// put your setup code here, to run once:

pinMode(13,OUTPUT);

pinMode(pin,INPUT);// CONNECT THE LIGHT SENSOR TO A0

Serial.begin(9600);

void loop() {

// put your main code here, to run repeatedly:

52
int val= analogRead(pin);Serial.println(val);

if(val>700){

// fire detected

digitalWrite(13,HIGH);//SET THE LIGHT ON // AS EVERYTHING IS DARK

Serial.println("IT IS DARK .. NOW LIGHT IS ON");

else if (val<500) // donot chatter

digitalWrite(13,LOW);

Serial.println("now it is bright outside... light is turned off");

Output:

Cover the LDR sensor with some object like, a cap of a pen (opaque, but not
transparent cap) and create darkness. This will switch on the LED on Pin13. When
light is shined/brightness is sensed by LDR, the LED will go off.

53
Experiment 12: 12. Fire Alarm Using Arduino

Aim: To create a fire sensing alarm with Arduino Nano

Apparatus: Thermistor, Arduino Nano and connecting wires, Soldering iron/hair drier/some
hot material.

Circuit diagram:

Connect a buzzer to pin 13 of Arduino Nano

Connect the analog output of thermsitor to pin A3 of Arduino Nano

Upload the following code.

Code:

// steinhart equation says

// 1/T =1/T0 + (1/B)*ln(R/R0)

// here T is the temperature in kelvin

// T0 = 273.15+25; i.e temperature of 25 degree celsius

// B = 3950 which is a constant for given thermistor material

// R is the resistance at T kelvin temperature which can be obtained from adc value

54
//R0 is the resistance of the thermsitor at 25 degree celsius, which is the resistance
printed on the thermistor

// in this case it is 10000 (103 printed on thermistor)

// let the adc gives a value called ADC

/*

* 5*R/(10000+R)=ADC*5/1024

* R= 10000*ADC/(1024-ADC)

sustituting all gives

1/T =1/T0 + (1/B)*ln(R/R0)

1/T =1/298.15 + (1/3950.00)*ln(10000*ADC/(1024-ADC)/10000)

1/T =1/298.15 + (1/3950.00)*ln(ADC/(1024-ADC))

*/

/*

* AUTHOR NAME : VVV SATYANARAYANA

* 20TH MARCH 2022

* VERSION 1.0

* TO CALCULATE THE TEMPERATURE USING THERMISTOR WITH THE


HELP OF STEINHART - HART EQUATION

*/

#define temp_threshold =40

void setup() {

// put your setup code here, to run once:

pinMode(13,OUTPUT);

pinMode(A1,INPUT);// CONNECT THE fire sensor TO A0

Serial.begin(9600);

55
}

float temp,x,y;

void loop() {

y=get_average(A1); // get the average value of analogRead();

x=(1.00/298.15)+(1.00/3950.00)*log(1*y/(1024.00-y));

temp=1.000/x-273.15;// temperature in degree celcius, not in kelvin

Serial.println(temp);// print present temperature

if(temp>temp_threshold){

// fire detected

digitalWrite(13,HIGH);//SET THE siren/buzzer on...

Serial.println("It is on fire...");

else

digitalWrite(13,LOW);

Serial.println("No fire detected");

56
float get_average(int pin){

float z=0;// intialize z to zero

for(int i=0;i<1000;i++)z=z+analogRead(pin); // take the sum of 1000 analogRead()


values.

z=z/1000.00;// get average analogRead() value from 1000 data points

return z;// return the value of average

******************* end of code ***************************

Output:

After uploading the code to Nano, bring a hot object, close to thermistor sensor, like
soldering iron/ hair drier etc and raise the temperature to see in the serial monitor that
fire happened.

57
TRAINER BOARD USED IN THIS LAB

58

You might also like