Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Internet of Things 3160716 190280107010

Practical -1

Aim: Study the pin design of


a) Arduino UNO micro controller b) Raspberry Pi

Arduino is a prototype platform (open-source) based on an easy-to-use hardware and


software. It consists of a circuit board, which can be programed (referred to as a
microcontroller) and a ready-made software called Arduino IDE (Integrated Development
Environment), which is used to write and upload the computer code to the physical board.
The key features are −
 Arduino boards are able to read analog or digital input signals from different sensors
and turn it into an output such as activating a motor, turning LED on/off, connect to
the cloud and many other actions.
 You can control your board functions by sending a set of instructions to the
microcontroller on the board via Arduino IDE (referred to as uploading software).
 Unlike most previous programmable circuit boards, Arduino does not need an extra
piece of hardware (called a programmer) in order to load a new code onto the
board. You can simply use a USB cable.
 Additionally, the Arduino IDE uses a simplified version of C++, making it easier to
learn to program.
 Finally, Arduino provides a standard form factor that breaks the functions of the
micro-controller into a more accessible package.

LDCE 1|Page
Internet of Things 3160716 190280107010

Board Types
Various kinds of Arduino boards are available depending on different microcontrollers used.
However, all Arduino boards have one thing in common: they are programed through the
Arduino IDE.
The differences are based on the number of inputs and outputs (the number of sensors,
LEDs, and buttons you can use on a single board), speed, operating voltage, form factor etc.
Some boards are designed to be embedded and have no programming interface
(hardware), which you would need to buy separately. Some can run directly from a 3.7V
battery, others need at least 5V.
Here is a list of different Arduino boards available.

Arduino boards based on ATMEGA328 microcontroller

Board Name Operating Clock Digital Analog PWM UART Programming


Volt Speed i/o Inputs Interface

Arduino Uno R3 USB via


5V 16MHz 14 6 6 1
ATMega16U2

Arduino Uno R3 USB via


5V 16MHz 14 6 6 1
SMD ATMega16U2

Red Board 5V 16MHz 14 6 6 1 USB via FTDI

Arduino Pro FTDI-Compatible


3.3V 8MHz 14 6 6 1
3.3v/8 MHz Header

Arduino Pro FTDI-Compatible


5V 16MHz 14 6 6 1
5V/16MHz Header

Arduino mini 05 FTDI-Compatible


5V 16MHz 14 8 6 1
Header

Arduino Pro FTDI-Compatible


3.3V 8MHz 14 8 6 1
mini Header
3.3v/8mhz

LDCE 2|Page
Internet of Things 3160716 190280107010

Arduino Pro mini FTDI-Compatible


5V 16MHz 14 8 6 1
5v/16mhz Header

Arduino Ethernet FTDI-Compatible


5V 16MHz 14 6 6 1
Header

FTDI-Compatible
Arduino Fio 3.3V 8MHz 14 8 6 1
Header

LilyPad Arduino FTDI-Compatible


3.3V 8MHz 14 6 6 1
328 main board Header

LilyPad Arduino FTDI-Compatible


3.3V 8MHz 9 4 5 0
simple board Header

Arduino boards based on ATMEGA32u4 microcontroller

Board Name Operating Clock Digital Analog PWM UART Programming


Volt Speed i/o Inputs Interface

Arduino Native USB


5V 16MHz 20 12 7 1
Leonardo

Pro micro Native USB


5V 16MHz 14 6 6 1
5V/16MHz

Pro micro Native USB


5V 16MHz 14 6 6 1
3.3V/8MHz

LilyPad Native USB


3.3V 8MHz 14 6 6 1
Arduino USB

LDCE 3|Page
Internet of Things 3160716 190280107010

Arduino boards based on ATMEGA2560 microcontroller

Board Name Operating Clock Digital Analog PWM UART Programming


Volt Speed i/o Inputs Interface

Arduino USB via


Mega 2560 5V 16MHz 54 16 14 4 ATMega16U2B
R3

Mega Pro FTDI-Compatible


3.3V 8MHz 54 16 14 4
3.3V Header

Mega Pro 5V FTDI-Compatible


5V 16MHz 54 16 14 4
Header

Mega Pro FTDI-Compatible


3.3V 8MHz 54 16 14 4
Mini 3.3V Header

Arduino boards based on AT91SAM3X8E microcontroller

Board Name Operating Clock Digital Analog PWM UART Programming


Volt Speed i/o Inputs Interface

Arduino USB native


Mega 2560 3.3V 84MHz 54 12 12 4
R3

LDCE 4|Page
Internet of Things 3160716 190280107010

Arduino PIN SET

Power USB
Arduino board can be powered by using the USB cable from your computer. All
you need to do is connect the USB cable to the USB connection (1).

LDCE 5|Page
Internet of Things 3160716 190280107010

Power (Barrel Jack)


Arduino boards can be powered directly from the AC mains power supply by
connecting it to the Barrel Jack (2).

Voltage Regulator
The function of the voltage regulator is to control the voltage given to the
Arduino board and stabilize the DC voltages used by the processor and other
elements.

Crystal Oscillator
The crystal oscillator helps Arduino in dealing with time issues. How does
Arduino calculate time? The answer is, by using the crystal oscillator. The number
printed on top of the Arduino crystal is 16.000H9H. It tells us that the frequency
is 16,000,000 Hertz or 16 MHz.

Arduino Reset
You can reset your Arduino board, i.e., start your program from the beginning.
You can reset the UNO board in two ways. First, by using the reset button (17) on
the board. Second, you can connect an external reset button to the Arduino pin
labelled RESET (5).

Pins (3.3, 5, GND, Vin)


 3.3V (6) − Supply 3.3 output volt
 5V (7) − Supply 5 outputvolt
 Most of the components used with Arduino board works finewith
3.3 volt and 5 volt.
 GND (8)(Ground) − There are several GND pins on the Arduino, any
of which can be used to ground your circuit.
 Vin (9) − This pin also can be used to power the Arduino board from
an external power source, like AC mains power supply.

Analog pins
The Arduino UNO board has six analog input pins A0 through A5. These pins can
read the signal from an analog sensor like the humidity sensor or temperature
sensor and convert it into a digital value that can be read by the microprocessor.

LDCE 6|Page
Internet of Things 3160716 190280107010

Main microcontroller
The Raspberry Each Arduino board has its own microcontroller (11). You can assume it as the
header is the key brain of your board. The main IC (integrated circuit) on the Arduino is slightly
to its ability to different from board to board. The microcontrollers are usually of the ATMEL
interface with the Company. You must know what IC your board has before loading up a new
real world. The Pi program from the Arduino IDE. This information is available on the top of the IC.
either uses a 40- For more details about the IC construction and functions, you can refer to the
pin or 26-pin data sheet.
depending on the
model and it is
ICSP pin
important to
understand how Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting
those pins are of MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial
arranged and Peripheral Interface), which could be considered as an "expansion" of the output.
labelled. Actually, you are slaving the output device to the master of the SPI bus.

The GPIO header


provides the Power LED indicator
following power
and interface This LED should light up when you plug your Arduino into a power source to
options : indicate that your board is powered up correctly. If this light does not turn on,
then there is something wrong with the connection.
 3.3V
(on TX and RX LEDs
2
On your board, you will find two labels: TX (transmit) and RX (receive). They
appear in two places on the Arduino UNO board. First, at the digital pins 0 and 1,
to indicate the pins responsible for serial communication. Second, the TX and RX
led (13). The TX led flashes with different speed while sending the serial data. The
speed of flashing depends on the baud rate used by the board. RX flashes during
the receivingprocess.

Digital I/O
The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM
(Pulse Width Modulation) output. These pins can be configured to work as input
digital pins to read logic values (0 or 1) or as digital output pins to drive different
modules like LEDs, relays, etc. The pins labeled “~” can be used to generate PWM.

AREF
AREF stands for Analog Reference. It is sometimes, used to set an external
reference voltage (between 0 and 5 Volts) as the upper limit for the analog input
pins.

pins)
 5V (on 2 pins)
LDCE 7|Page
Internet of Things 3160716 190280107010
 Ground (on 8 pins)
 General purpose input and output
 PWM (pulse width modulation)
 I2C
 I2S
 SPI
 Serial
These allow a massive range of sensors, motors, LEDs and accessories to be connected to the Pi.

Practical -2

Aim: To interface LED/Buzzer with Arduino/ Raspberry Pi and write programs to turn
ON LED for 1 sec after every 5 secs.

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second (1 sec = 1000ms)
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(5000); // wait for a second (5 sec = 5000ms)
}

LDCE 8|Page
Internet of Things 3160716 190280107010

LDCE 9|Page
Internet of Things 3160716 190280107010

Practical -3
Aim: To interface DHT11 sensor with Arduino. Write program to print temperature and humidity
reading

#include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

void setup(){

Serial.begin(9600);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor

}//end "setup()"

void loop(){
//Start of Program

DHT.read11(dht_apin);

Serial.print("Current humidity = ");


Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");

delay(5000);//Wait 5 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}// end loop(

LDCE 10 | P a g e
Internet of Things 3160716 190280107010

LDCE 11 | P a g e
Internet of Things 3160716 190280107010

Practical -4

Aim: Write program on Arduino/ Raspberry Pi for MQTT publishing process.

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

const char* ssid = "Tirth";


const char* password = "21092001";

const char* mqtt_server = "192.168.1.0";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/

Adafruit_BME280 bme; // I2C


float temperature = 0;
float humidity = 0;

// LED Pin
const int ledPin = 4;

void setup() {
Serial.begin(115200);

status = bme.begin();
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
LDCE 12 | P a g e
Internet of Things 3160716 190280107010
pinMode(ledPin, OUTPUT);
}

void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

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


delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {


Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;

for (int i = 0; i < length; i++) {


Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();

if (String(topic) == "esp32/output") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}
LDCE 13 | P a g e
Internet of Things 3160716 190280107010
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");

} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

long now = millis();


if (now - lastMsg > 5000) {
lastMsg = now;

// Temperature in Celsius
temperature = bme.readTemperature();

// Convert the value to a char array


char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
client.publish("esp32/temperature", tempString);

humidity = bme.readHumidity();

// Convert the value to a char array


char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
client.publish("esp32/humidity", humString);
}
}

LDCE 14 | P a g e
Internet of Things 3160716 190280107010

Practical -5

Aim: Write program on Arduino/ Raspberry Pi for MQTT subscription process.

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
LDCE 15 | P a g e
Internet of Things 3160716 190280107010
#include <Adafruit_Sensor.h>

const char* ssid = "Tirth";


const char* password = "21092001";

const char* mqtt_server = "192.168.0.1";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/

Adafruit_BME280 bme; // I2C


float temperature = 0;
float humidity = 0;

// LED Pin
const int ledPin = 4;

void setup() {
Serial.begin(115200);
status = bme.begin();
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);

pinMode(ledPin, OUTPUT);
}

void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

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


LDCE 16 | P a g e
Internet of Things 3160716 190280107010
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {


Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;

for (int i = 0; i < length; i++) {


Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();

if (String(topic) == "esp32/output") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe
client.subscribe("esp32/output");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
LDCE 17 | P a g e
Internet of Things 3160716 190280107010
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

long now = millis();


if (now - lastMsg > 5000) {
lastMsg = now;

// Temperature in Celsius
temperature = bme.readTemperature();

// Convert the value to a char array


char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
client.publish("esp32/temperature", tempString);

humidity = bme.readHumidity();

// Convert the value to a char array


char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
client.publish("esp32/humidity", humString);
}
}

LDCE 18 | P a g e
Internet of Things 3160716 190280107010

Practical 6

Aim: Study any one IoT technology like fastag, or tracking and monitoring, write its detailed
communication protocols used.

ZigBee protocol provides low-cost, low power solution for remotely controlling and monitoring smart equipments, fleet
applications, home automation systems, medical devices and other compliant systems. It is used by leading
manufacturers of semiconductor devices, technology firms, wireless communication devices as the de-facto
communication protocol. 

In this modern era, there are several standards that are available for high data rate transfer, but none of them seems to
be compatible for sensors and control devices communication standards. This communication not only required high
data rates but also low tenancy and power consumption at lower bandwidths. Due to the fact that all of these
specifications are present in this ZigBee technology, it is making this “a best fit” for several embedded applications,
industrial control, HA, and so on.
LDCE 19 | P a g e
Internet of Things 3160716 190280107010

System structure of ZigBee technology consists of three main components: ZigBee coordinator, Router, and end device.
Every ZigBee network has to consist one coordinator which acts as a bridge of network. The coordinator acts as a hub of
receiving and storing important information during a process of transmitting data operations. ZigBee router acts as an
intermediate between the hub of information and end devices which permits the traffic or commands to move through
them to the end device. End devices have limited access of communication with their parents nodes such that to save
useful power, energy or battery itself. The pattern in which these three components are connected with each other’s
depends on star, tree, and mesh networks.

ZigBee protocol architecture consists of different layers as per IEEE 802.15.4 standards.

1.
Physical layer: this layer performs the task of modulation as well as demodulation of various transmitted and
received signals, respectively. Various frequency, data rates, and channel are used with this layers depending
upon the locality.

2.
MAC Layer: the function of this layer is to enable reliable data transfer communication by accessing different
networks with the carrier sense multiple access collision avoidance (CSMA).

3.
Network Layer: this layer accompanies for all network related operations such as connection between router
and different end devices, disconnection to network, routing and various device configuration.

4.
Application support sublayer: this layer is responsible interfere ZigBee devices with different object application
device in order to communicate through network layer. It is responsible of matching to peripherals on the basis
of their services, application, and needs.

5.
Application framework: it gives two sorts of information administrations as key esteem combine and nonspecific
message administrations. Nonspecific message is a developer characterized structure, while the key esteem
match is utilized for getting properties inside the application objects. ZDO gives an interface between application
items and APS layer in ZigBee gadgets. It is in charge of distinguishing, starting and restricting different gadgets
to the system.

ZigBee data transmission technology mainly works under two modes: non-Beacon mode and Beacon mode. In first
mode, i.e., beacon mode, coordinators and routers continuously monitor any changes of flowing data; therefore, more
power is consumed. The routers and coordinators cannot sleep in this mode as at anytime node could receive any signal
to communicate and respond. Nonetheless, it requires more power supply and its general power utilization is low on
the grounds that the greater part of the gadgets are in a latent state for over long stretches in the system.
Contrary, in beacon mode the router and coordinators enters the sleeping mode when there is no data transmission.
There is a cyclic process using relays which counters, which periodically switch on and off routers to transmit data to
LDCE 20 | P a g e
Internet of Things 3160716 190280107010
multiple nodes within a network. These networks are work for available time slots which implies, they work when the
correspondence required outcomes in lower duty cycles and longer battery use.

LDCE 21 | P a g e
Internet of Things 3160716 190280107010

Practical 7

Aim: Write program on Arduino/ Raspberry Pi to upload temperature and humidity data to the
thingspeak cloud.

#include <DHT.h> // Including library for dht

#include <ESP8266WiFi.h>

String apiKey = "Your API of thingsspeak";

const char *ssid = "Tirth";


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

#define DHTPIN 0

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(500);
Serial.print(".");
}
LDCE 22 | P a g e
Internet of Things 3160716 190280107010

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.");
}
LDCE 23 | P a g e
Internet of Things 3160716 190280107010

client.stop();

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

delay(10000);
}

Output:

LDCE 24 | P a g e
Internet of Things 3160716 190280107010

Practical 8

Aim: Case Study :


1. Study and pick a realife challenge in a application area
2. Write the Problem
3. Analyse how the problem can be solved using IoT
4. Assume that you have to develop the IoT solution for the problem. List the choice of selection of
items like sensor, microcontroller, communication protocol based on parameters like cost,
efficiency, performance and size.

Problem:

In education institutes taking attendance of students can be a cumbersome and redundant process.

Solution:

For solving this problem, we can use a RFID sensor attached to ID card. The sensor would be scanned
at each classroom and attendance would be uploaded to cloud on regular basis.

Sensors required:

RFID

Communication Protocols:

Gossip Protocol
State Routing Protocol
IGMP
EPCglobal Class 1 Gen 2 protocol (EPC C1G2)

LDCE 25 | P a g e
Internet of Things 3160716 190280107010

Practical 9

Aim: Develop a project on IoT application using Arduino or Raspberry Pi. with Tinkercad
simulation software tool.

Temperature Based Fan Controller


Temperature Based Fan Speed Control & Monitoring With Arduino and LM35 Temperature Sensor. The microcontroller
controls the speed of an electric fan according to the requirement & allows dynamic and faster control and the LCD makes
the system user-friendly. Sensed temperature in Celsius Scale and fan speed in percentage are simultaneously displayed
on the LCD panel.
The applications areas of this project are air-conditioners, water-heaters, snow-melters, ovens, heat- exchangers,
mixers, furnaces, incubators, thermal baths, and veterinary operating tables.
Applications
1. Air-conditioners, water-heaters, snow-melters, ovens, heat-exchangers, mixers, furnaces, incubators,
thermal baths.
2. Veterinary operating tables.
3. This project is useful in process industries for maintenance and controlling of Boilers temperature.
4. Normally we use the regulator to change the speed of the fan. Here the room temperature changes the
speed varies so that according to the temperature the fan rotates.
5. The fan designed in this project, has the more scope to use in the Middle East countries.
6. This product is more suitable for the hot regions.

The components used are:


1. Arduino UNO
2. Temperature Sensor
3. Fan(DC Motor)
4. LCD Display
5. Resistor
6. Relay
7. Breadboard
8. Connecting wires
Circuit Diagram

LDCE 26 | P a g e
Internet of Things 3160716 190280107010

Working
1. Temperature sensor LM35 senses the temperature and converts it into an electrical (analog) signal, which
is applied to the ATmega328 microcontroller of the Arduino UNO Board.
2. The analog value is converted into a digital value. Thus the sensed values of the temperature and speed of the
fan are displayed on the LCD.
3. When the temperature exceeds 20°C the fan starts rotating.
4. A low-frequency pulse-width modulation (PWM) signal, whose duty cycle is varied to adjust the fan’s speed is
used.
5. Lm35 temperature sensor voltage to temperature conversion, now one of the most difficult thing is how to
convert the voltage generated/output by the lm35 sensor at output in Celsius or Fahrenheit scales. Well this
needs you to first go through the data sheet of temperature sensor and know about the characteristics of the
sensor.

Code

LDCE 27 | P a g e
Internet of Things 3160716 190280107010
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); float temp;
int tempPin = A1; int relayPin = 8; #define fan 9

void setup(){
pinMode(fan, OUTPUT); pinMode(relayPin, OUTPUT);
lcd.begin(16, 3); lcd.print("AUTO TEMPERATURE");
delay(2000); lcd.clear();
}

void loop()
{
lcd.setCursor(3,0); lcd.print("Recording");
lcd.setCursor(2, 1); lcd.print("Temperature.."); delay(3000);
lcd.clear(); lcd.setCursor(0,2);
temp = analogRead(tempPin); //temp = temp*0.48828125; float voltage = temp * 5.0;

LDCE 28 | P a g e
Internet of Things 3160716 190280107010
voltage /= 1024.0; // print out the voltage
lcd.print(voltage); lcd.println(" volts"); // now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
lcd.setCursor(0, 0);
lcd.print("Temperature = ");
lcd.setCursor(2,1); //lcd.print(temp);
lcd.print(temperatureC);
lcd.println(" degrees C");
delay(3000);
lcd.clear();

if(temperatureC >= 20)


{
poweronRelay();
if(temperatureC >= 20 && temperatureC <= 25)
{
analogWrite(fan,51);
lcd.print("Fan Speed: 20%
"); delay(2000);
lcd.clear();
}
else if(temperatureC <= 35)
{
analogWrite(fan,102);
lcd.print("Fan Speed: 40%
"); delay(2000);
lcd.clear();
}
else if(temperatureC <= 40)
{
analogWrite(fan,153);
lcd.print("Fan Speed: 60%
"); delay(2000);
lcd.clear();
}
else if(temperatureC <= 44)
{
analogWrite(fan,200);
lcd.print("Fan Speed: 80%
"); delay(2000);
lcd.clear();
}
else if(temperatureC >= 45)
{
analogWrite(fan,255);
lcd.print("Fan Speed: 100% ");
delay(2000);
lcd.clear();
}
}
else if(temperatureC < 20)
{
poweroffRelay();
}
}
void poweronRelay()
{
digitalWrite(relayPin, HIGH);
lcd.print("Fan ON");
delay(2000);
lcd.clear();
}
void poweroffRelay()

LDCE 29 | P a g e
Internet of Things 3160716 190280107010

{
digitalWrite(relayPin, LOW); analogWrite(fan,0);
lcd.print("Fan OFF"); delay(2000); lcd.clear();
}

Output:

LDCE 30 | P a g e
Internet of Things 3160716 190280107010

Advantages
1. This project can be used in both the home and Industry.
2. It helps in saving the time, energy and electricity.
3. Normally we use the regulator to change the speed of the fan. Here the
room temperature changes the speed varies so that according to the
temperature the fan rotates.

Disadvantages

1. Due to complexity in circuit, this system introduces a slight delay.


2. As everyone has different body temperature, this system can’t accommodate those
changes.

Future
Enhancements
1. To integrate IR sensor, so that it can detect presence of people in room and
accordingly switch the fan off when no one’s around.

LDCE 31 | P a g e

You might also like