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   

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8,9,4,5,6,7); 

void setup(void) {

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

void loop() {
  
 float humidity, pressure;
  
 humidity = dht.readHumidity()+10.0f;
 pressure = readPressureAndTemperature();
 
 delay(2000);
 
 lcd.clear(); 
 
 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("T:"); 
 lcd.print(tempF);
 lcd.print((char)223);
 lcd.print("C ");
 
 //Printing Humidity
 lcd.print("H: ");
 lcd.print(humF);
 lcd.print("%");
 
 //Printing Pressure
 lcd.setCursor(0,1);
 lcd.print("P: ");
 lcd.print(pressF);
 lcd.print(" hPa");
}

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

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    { 
      temperature = T;
      status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);
        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!

Pete

"DHT dht(DHTPIN, DHTTYPE);"

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 "https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.h" 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!