IoT Lab Program 4
IoT Lab Program 4
4 Date:
Aim:
To write a program to read humidity and temperature values using Arduino and DHT11
sensor.
Components needed:
1. 1 × Arduino Uno board
2. 1 × USB cable
3. 1 × Computer
4. Connecting wires
5. 1 x Breadboard
6. DHT11 sensor
DHT11 sensor:
Hardware Procedure:
1. Connect USB cable with Type A end to the computer USB port and Type B end of the
cable to Arduino Uno.
2. This USB cable is used for power supply to development board i.e. Arduino Uno board
as well as to transfer the compiled program to run in the Arduino Uno controller chip.
3. Insert the DHT11 sensor into the breadboard as shown in the connection diagram and
connect the signal pin of the DHT11 to the digital pin 7 (D7) of the Arduino which act
as INPUT for temperature and humidity.
4. Connect 5V from Arduino to Vcc of DHT11 sensor.
5. Connect GND of Arduino to Ground of DHT sensor.
Connection Diagram:
Algorithm:
STEP 1: Start the process.
STEP 2: Start ->Arduino IDE -2.3.2
STEP 3: Select the board that you use and the corresponding port number
in the Arduino IDE.
STEP 4: Install Arduino Library by going to your Arduino IDE then go to Sketch >
Include Library > Manage Libraries. The Library Manager will be shown.
STEP 5: Then Search for “DHT” in the Search box and scroll all the way down to find
the DHT library by Adafruit and install it in Arduino IDE.
STEP 6: Then enter the coding in Arduino IDE and compile the sketch.
STEP 7: After the successful compilation of code, upload the compiled code to run in
Arduino microcontroller.
STEP 8: After uploading the code, open the serial monitor present in Arduino IDE and
get the temperature, humidity & heat index of your environment.
STEP 9: Stop the process.
Program:
1 #include "DHT.h"
2 #define DHTPIN 7 // what pin we're connected to
3 #define DHTTYPE DHT11 // DHT 11
// Initialize DHT sensor for normal 16mhz Arduino
4 DHT dht(DHTPIN, DHTTYPE);
5 void setup()
6 {
7 Serial.begin(9600);
8 Serial.println("DHTxx test!");
9 dht.begin();
10 }
11 void loop()
12 { // Wait a few seconds between measurements.
13 delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
14 float h = dht.readHumidity();
// Read temperature as Celsius
15 float t = dht.readTemperature();
// Read temperature as Fahrenheit
16 float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
17 if (isnan(h) || isnan(t) || isnan(f))
18 {
19 Serial.println("Failed to read from DHT sensor!");
20 return;
21 }
// Compute heat index
// Must send in temp in Fahrenheit!
22 float hi = dht.computeHeatIndex(f, h);
23 Serial.print("Humidity: ");
24 Serial.print(h);
25 Serial.print(" %\t");
26 Serial.print("Temperature: ");
27 Serial.print(t);
28 Serial.print(" *C ");
29 Serial.print(f);
30 Serial.print(" *F\t");
31 Serial.print("Heat index: ");
32 Serial.print(hi);
33 Serial.println(" *F");
34 }
Sketch explanation:
1. This sketch uses some library functions from the header file dht.h
2. This sketch works by setting pin D7 as INPUT from the DHT 11 sensor to read
temperature and humidity of the environment.
3. Then it defines what type of sensor we are using (whether DHT11 or DHT22 or
DHT21).
4. Serial.begin(9600) tells the Arduino to get ready to exchange messages with the
Serial Monitor at a data rate of 9600 bits per second. That's 9600 binary ones or zeros
per second, and is commonly called a baud rate.
5. dht.begin() - Start and initialize the dht object.
6. dht.readHumidity() - Read and return the relative humidity as a percentage
7. dht.readTemperature() - Read and return temperature in degrees Celcius
8. dht.readTemperature(true) - Read and return temperature in degrees Fahrenheit.
The argument 'true' changes the output from Celcius to Fahrenheit.
9. Isnan() – is used to check that the sensor is failed to read the data. (NAN is Not A
Number)
10. dht.computeHeatIndex(f, h) - Compute and return the heat index in degrees
Fahrenheit. The heat index is the measure of how hot it feels based on the temperature
and humidity.
Output:
Result:
Thus, the sketch use DHT11 temperature and humidity sensor with Arduino and print
the humidity, temperature & heat index on the serial monitor of Arduino IDE.