Basic DHT22 Programming Question

First time poster here.

I have really been enjoying my Arduino, and going through it with my 10 y/o son as well. It has been a nice way for us to spend time together and both learn at the same time :slight_smile: .

We have been building a series of weather station projects that are on Youtube, and though they work fine, I still cannot quite understand how the DHT22 programming works. Below is the code:

#include "DHT.h"
#include <LiquidCrystal.h>
#include <SFE_BMP180.h>
#include <Wire.h>

SFE_BMP180 pressure;

float temperature;

#define ALTITUDE 216.0 // Altitude in Sparta, Greece

#define DHTPIN 22     // what pin we're connected to

#define DHTTYPE DHT11   

LiquidCrystal lcd(8,9,4,5,6,7); 

void setup(void) {

  lcd.begin(16, 2);
  lcd.print("Reading sensors");

void loop() {
 float humidity, pressure;
 humidity = dht.readHumidity()+10.0f;
 pressure = readPressureAndTemperature();
 char tempF[6]; 
 char humF[6];
 char pressF[7];
 dtostrf(temperature, 5, 1, tempF);
 dtostrf(humidity, 2, 0, humF);
 dtostrf(pressure, 7, 2, pressF);

 //Printing Temperature
 lcd.print("C ");
 //Printing Humidity
 lcd.print("H: ");
 //Printing Pressure
 lcd.print("P: ");
 lcd.print(" hPa");

float readPressureAndTemperature()
  char status;
  double T,P,p0,a;

  status = pressure.startTemperature();
  if (status != 0)
    status = pressure.getTemperature(T);
    if (status != 0)
      temperature = T;
      status = pressure.startPressure(3);
      if (status != 0)
        status = pressure.getPressure(P,T);
        if (status != 0)
          p0 = pressure.sealevel(P,ALTITUDE);       
          return p0;

What does the code “DHT dht(DHTPIN, DHTTYPE);” mean?I realize that DHTPIN and DHTTYPE are variables defining the sensor type and pin that is being read, but what are “DHT” and “dht”? I thought “dht” would be considered a function, but in the case of functions is not the first item in the function code the expected output (like “INT” for integer)? Is “DHT” considered the function output in this line of code?

Lastly, where are the details of functions like this documented? I would much rather research it myself then waste everyone’s time on the forum.

Thanks in advance!



This line create an object named "dht" of class type "DHT" and the object now knows what the pin and type are because of those constructor arguments (DHTPIN and DHTTYPE). It is the same concept for "LiquidCrystal lcd(8,9,4,5,6,7); " this line. You are creating an object named "lcd" of class type "LiquidCrystal" and supplying it 6 constructor arguments. If you look here you will see what type of constructors are available...

As for your second question, it would be located "#include "DHT.h"" which may either be in the project folder or the library folder

After a little searching, which by the way you should have provided, just saying...I figure that you got your dht library from here "" and the constructor is

DHT(uint8_t pin, uint8_t type, uint8_t count=6);
//the third argument is optional, and according to the .cpp file's comments, it is ignored and useless

Also, please edit your code using code tags

so that is looks like this

The documentation you seek should be with the libraries that you downloaded, or on the websites where you obtained them.

Thanks for everyone's quick responses!