You are on page 1of 3

IOT Lab 7 Enrollment No: IU2141050017

Lab 7: Interfacing of DHT22 Humidity and Temperature Sensor with Arduino


Uno Board.
Introduction:

The DHT-22 (also named as AM2302) is a digital-output relative humidity and temperature sensor. It uses a
capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the
data pin. In this tutorial you will learn how to use this sensor with Arduino uno.

Description about DHT22 Sensor & it’s pin functions:

The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity
sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin (no
analog input pins needed).
DHT sensors have 3 pins: GND, VCC & Signal. You have to connect the following 3 pins to the Board: GND to
Ground Pin of the Board. VCC to 3.3V or VCC Pin of the Board.

Connection Diagram:

Fig. 1 Interfacing DHT22 Sensor with an Arduino Uno Board

Page No 1
IOT Lab 7 Enrollment No: IU2141050017

Program:

#include <Wire.h>
#include “DHT.h”
#define DHTTYPE DHT22
uint8_t DHTPin = 5;
//D1
DHT dht(DHTPin, DHTTYPE);
float Temperature;
float Humidity;
void setup() (
Serial.begin(9600);
pinMode(DHTPin, INPUT);
dht.begin();
}

void loop() {
Humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
Temperature = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float Temp_Fahrenheit = dht.readTemperature(true)
// Check if any reads failed and exit early (to try again)
if(isnan(Humidity) || isnan(Temperature)) {
Serial.println(F(“Failed to read from DHT Sensor!”));
return;
}
Serial.print(F(“Humidity: “));
Serial.print(Humidity);
Serial.println(“%”);
Serial.print(F(“Temperature: “));
Serial.print(Temperature);
Serial.println(F(“°C”));
Serial.print(F(“Temperature in Fahrenheit: “));
Serial.print(Temp_Fahrenheit);
Serial.println(F(“°F”));
delay(5000)
}

Function Description:

• isnan() - The isNaN() function determines whether a value is not a number, first converting the
value to a number if necessary. Because coercion inside the isNaN() function can be surprising,
you may prefer to use Number.isNaN().
• Wire.h - this library allows you to communicate with I2C devices, a feature that is present on all
Arduino boards. I2C is a very common protocol, primarly used for reading/sending data to/from
external I2C components.

Page No 2
IOT Lab 7 Enrollment No: IU2141050017

• dht.pin - the pin the DHT sensor is connected to. DHTTYPE: DHT type (the kit uses the DHT11)
• dht.begin() – starts and initializes the DHT object
Faculty Signature

Page No 3

You might also like