1. This is (Fig-1) your DHT11-type Humidity and Temperature Sensor.
Figure-1:
2. The Sensor converts the humidity into 16-bit data (8-bit for integer part and 8-bit for fractional part). It converts the temperature into 16-bit data (8-bit for integer part and 8-bit for fractional part). There is another 8-bit data called CHKSUM (Checksum) which is for error detection in case there is any data corruption during transmission.
3. To read the 40-bit (5-byte) data from the Sensor, the UNO gives the following command:
DHT.read11(DHT11_PIN); //Sensor's Signal/DATA pin is connected with DPin-2
The above command has two parts:
DHT (Left part to the . dot) is called object in view of C++ Programming
read11(DHT11_PIN) (Right part to . dot) is called method in view of C++ Programming.
4. The 5-byte (40-bit) data of Step-3 (coming from Sensor) are stored in a an array of the MCU named bits[ ] (not visible to user) which is conceptually shown in Fig-2.
Figure-2:
5. To compute float-type (number with both integer part and
fractional part) Humidity from <bits[0], bits[1]>, the following command is executed.
float myHumi = DHT.humidity; //this line is missing in sketch of Post-1
As a result, the Humidity value is stored in the variable/identifier named myHumi.
6. Similarly the following command is executed to compute the float-type Temperature.
float myTemp = DHT.Temperature; //this line is missing in sketch of Post-1
7. Now, telling few words on the need of chk variable in the following command.
int chk = DHT.read11(DHT11_PIN);
The above command does the following jobs:
(1) It stores humidity, temperature, and CHKSUM data in the array of Fig-2.
(2) It assigns -1 to the variable chk if there is any data corruption during transmission.
(3) It assigns -2 to the variable chk if there is a timeout event between host MCU and sensor. It means the MCU has not received the 40-bit data from Sensor within 4 ms time after issuing the read11() command.
(4) It assigns 0 to the variable chk if neither of the above two errors occur which means that there is good data.
The value of chk variable can be used for diagnostic purposes of a non-functional DHT11 Sensor.
8. Finally, your sketch for UNO would appear as follows (only for Temperature):
#include <dht.h> //header file containing codes for various commands
dht DHT; //dht = className; DHT = Object
int tempe = 33; //reference temperature
#define DHT11_PIN 2
void setup()
{
Serial.begin(9600);
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
if (chk != 0)
{
Serial.print("There is error!");
while (1); //wait for ever
}
Serial.println(chk, DEC); //see 0 on the screen
Serial.print("Temperature = ");
float myTemp = DHT.temperature;
Serial.println(myTemp, 2); //2-digit after decimal point
delay(1000); //test/sampling interval
}
0
Temperature = 32.00 //why the fractional part is always 0 for my DHT11 sensor?
9. To get fractional part of temperature, I declared myTemp variable as double (as per declaration of my DHT Library) and then uploaded the sketch in DUE Board; surprisingly, the sketch did not work; rather, it announced - There is error!.