In this tutorial we will see how to measure temperature & humidity with Arduino so lets get started. For this you will need

  1. Arduino
  2. Breadboard
  3. Jumper Wire
  4. 10K ohms Resistor
  5. DHT22 sensor.

There are two version of sensor. One is DHT11 & second is DHT22. DHT11 is cheaper in cost and less accurate compare to DHT22. DHT22 is more accurate and have larger range compare to DHT11. In this tutorial we are using DHT22. Sensor have four pin. pin1 is for : 5V power, pin2 is for: Data , pin3: not used, pin4 is for: GND.

This is circuit diagram. 10k ohms resistor is used which is a pull-up resistor because Random noise can change the state to go HIGH or LOW level randomly. So, The pull-up resistor ensures that the wire is at a defined logic level even if no active devices are connected to it. Do connection as shown in diagram.

Download DHT library from Github OR you can download it from here because github library may change by the time or it may not work.

Download DHT-sensor-library-master.zip

And download the file. After downloading unzip it. Add to library of Arduino. Now open the example folder then DHT tester. Open DHT tester file you can directly upload the sketch.

Sketch:
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
Explanation:

The purpose of the instruction “#include” is to include an external library in our project so we can use its functions. Define DHT pin number that is pin 2. Define DHT type 22 or 11. Then initialize the library. Serial.begin (9600) it will create serial connection with Arduino. Serial.println will print hello massage when serial monitor is started. Dht.begin will start DHT.

void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}

Lets going to loop function. We take measurement for humidity, temperature in Celsius & Fahrenheit using Float point h, t & f. “isnan” for is not a number. This is || OR Boolean operator. If value of h,t or f is not a number it will print Failed to read from DHT sensor!. If you don’t want heat index you can delete it.

if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Go to serial monitor now you can see the values of humidity, temperature in Celsius & Fahrenheit.

Till then keep learning keep making 🙂

 

 


1 Comment

How to take Arduino sensor data to excel sheet and plot it for analysis. - Robotica DIY · March 14, 2020 at 7:59 pm

[…] air of hair dryer and sensor data will be plotted in excel sheet. If you want to know more about temperature sensor with Arduino you can read tutorial click on the link. After downloading software Tera term. Install it. Now you can see its icon on desktop but before […]

Comments are closed.