As you are a beginner in programming a DHT11 (Temperature and Humidity) sensor using Arduino UNO, you could ask the Forum to guide you how to write codes yourself rather than exploring Google/ChatGPT which are usually consulted by the experineced people.
You could begin with asking questions like --
1. What is the pictorial view of the DHT11 sensor to identify its pin signals?
Let us search in the net and we find (Fig-1):
Figure-1:
2. How to make connection diagram with Arduino UNO using DPin-7?
Connection diagram amog DHT11, UNO, and PC (Fig-2).
Figure-2:
3. Guide Lines to program DHT11 Sesnor (taken from a Lecture Note).
13.5 DHT11 Type Humidity and Temperature Sensor
(1) Figure-13.14 is the pictorial view of DHT11-type Humidity and Temperature Sensor. It has three pins: GND-pin, DATA-pin, and Vcc-pin. The supply voltage for the sensor is 5V. The DATA-pin is terminated to 5V by an onboard 4.7kW resistor. There is also a power LED with a series resistor. It is a digital sensor which delivers 40-bit data to UNO when asked. Therefore, the DATA-pin of the sensor is to be connected with a DPin (digital pin) of UNO.
(2) The Sensor converts humidity information into 16-bit data of which the upper 8-bit is the integer part and the lower 8-bit is the fractional part. Likewise, the sensor converts the temperature information into 16-bit data of which upper the 8-bit is the integer part and the lower 8-bit is the fractional part. There is another 8-bit data called CHKSUM (checksum) which is computed from humidity and temperature data. It is used by UNO to check if any of the bits of the data frame has undergone corruption during transmission. Thus, there are 40-bit (16 + 16 + 8) data available in the memory area of the sensor which is conceptually shown a byte-type array in Fig-13.15.
(3) The necessary software routines to acquire data from the sensor and then to process them are available as ready-made functions in the library file named dht.h. Thus user must collect the file from the internet and then include it in the Arduino IDE and sketch.
(4) To read the 40-bit (5-byte) data from the Sensor, the UNO gives the following command:
int chk = DHT.read11(DPin); //Sensor's SigDATA pin is at DPin-7
(5) In response to the above command, the 40-bit data of the sensor arrives to the UNO and is saved in a 5-byte wide array (similar to Fig-13.15) and is unseen to user) named byte bits [5].
(6) To compute float-type (number with both integer part and fractional part) Humidity from <bits[0], bits[1]> of Fig-13.15
float myHumi = DHT.humidity;
(7) Similarly the following command is executed to compute the float-type Temperature.
float myTemp = DHT.Temperature;
(8) 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:
(a) It stores humidity, temperature, and CHKSUM data in the array of Fig-13.15.
(b) It assigns -1 to the variable chk if there is any data corruption during transmission.
(c) 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 within 4 ms time after issuing the read11() command.
(d) 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.
(9) Finally, the 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
#define DHT11_PIN 7 //DATA pin of sensor is connected with DPin-2
void setup()
{
Serial.begin(9600);
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
if (chk != 0)
{
Serial.print("There is error!");
while (true); //wait for ever
}
Serial.println(chk, DEC); //see 0 on the screen
Serial.print("Temperature = ");
float myTemp = DHT.temperature;
Serial.println(myTemp, 1); //1-digit after decimal point
delay(1000); //test/sampling interval
}