DHT11 (RHT01) and DHT22 (AM2302 – RHT03) are relative humidity and temperature sensors. These sensors are small, cheap and easy to use. They use only one wire to communicate with mcu.
The following two topic shows how to interface PIC16F877A with DHT11 and DHT22 sensors respectively:
Interfacing DHT11 relative humidity and temperature sensor with PIC16F877A
Interfacing PIC16F877A with DHT22 (AM2302-RHT03) sensor
The following table shows the DHT11 sensor characteristics (from DHT11 datasheet):
And the following table shows DHT22 (AM2302) technical specification (from AM2302 datasheet):
The differences between the two sensors are summarized in the following table:
DHT11 | DHT22 | |
Relative Humidity Operating Range | 20 ~ 90% | 0 ~ 100% |
Relative Humidity Accuracy | ±5%RH | ±2%RH (Max ±5%RH) |
Humidity Resolution | 1%RH | 0.1%RH |
Humidity Repeatability | ±1%RH | ±1%RH |
Humidity Hysteresis | ±1%RH | ±0.3%RH |
Long-term Stability | ±1%RH/year | ±0.5%RH/year |
Temperature Operating Range | 0 ~ 50°C | -40 ~ 80°C |
Temperature Accuracy | ±2°C | ±0.5°C |
Temperature Resolution | 1°C | 0.1°C |
DHT11 and DHT22 interfacing with PIC16F877A microcontroller:
The following circuit schematic shows the comparison between DHT11 and DHT22 sensors connection circuit.
The microcontroller used to make the comparison between DHT11 and DHT22 is PIC16F877A which runs with crystal oscillator at 8MHz.
The sensor DHT11 as well as DHT22 has 4 pins which are:
Vcc : Positive power supply (+5V)
Data : Sensor data input and output
NC : Not connected terminal
Gnd : Ground (0V).
A pull-up resistor must be added between the data pin of each sensor and VCC (+5V) pin as shown in the circuit schematic (4.7K ~ 10K).
2004 (20×4) LCD display is used to display temperature and humidity results for both sensors.
CCS C code for DHT11 vs DHT22 project:
The comparison between DHT11 and DHT22 (AM2302) sensors full CCS C code is below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | // DHT11 VS DHT22 CCS C code // https://simple-circuit.com/ //LCD module connections #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_ENABLE_PIN PIN_D2 #define LCD_DATA4 PIN_D3 #define LCD_DATA5 PIN_D4 #define LCD_DATA6 PIN_D5 #define LCD_DATA7 PIN_D6 //End LCD module connections #include <16F877A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock = 8000000) #include <lcd.c> #use fast_io(B) #define DHT11 PIN_B4 // Connection pin between DHT11 and mcu #define DHT22 PIN_B5 // Connection pin between DHT22 and mcu char message1[] = "DHT11 Temp = 00.0 C "; char message2[] = "DHT11 RH = 00.0 % "; char message3[] = "DHT22 Temp = 00.0 C "; char message4[] = "DHT22 RH = 00.0 % "; short Time_out; unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ; unsigned int16 Temp, RH; void dht11_start_signal(){ output_drive(DHT11); // Configure connection pin as output output_low(DHT11); // Connection pin output low delay_ms(25); output_high(DHT11); // Connection pin output high delay_us(30); output_float(DHT11); // Configure connection pin as input } short dht11_check_response(){ delay_us(40); if(!input(DHT11)){ // Read and test if connection pin is low delay_us(80); if(input(DHT11)){ // Read and test if connection pin is high delay_us(50); return 1; } } } unsigned int8 dht11_Read_Data(){ unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration if(Time_out) break; for(i = 0; i < 8; i++){ k = 0; while(!input(dht11)){ // Wait for DHT22 pin to go high k++; if(k > 100){ Time_out = 1; break; } delay_us(1); } delay_us(30); if(!input(dht11)) bit_clear(_data, (7 - i)); // Clear bit (7 - i) else{ bit_set(_data, (7 - i)); // Set bit (7 - i) while(input(dht11)){ // Wait for DHT22 pin to go low k++; if(k > 100){ Time_out = 1; break; } delay_us(1);} } } return _data; } void dht22_start_signal(){ output_drive(DHT22); // Configure connection pin as output output_low(DHT22); // Connection pin output low delay_ms(25); output_high(DHT22); // Connection pin output high delay_us(30); output_float(DHT22); // Configure connection pin as input } short dht22_check_response(){ delay_us(40); if(!input(DHT22)){ // Read and test if connection pin is low delay_us(80); if(input(DHT22)){ // Read and test if connection pin is high delay_us(50); return 1; } } } unsigned int8 dht22_Read_Data(){ unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration if(Time_out) break; for(i = 0; i < 8; i++){ k = 0; while(!input_state(dht22)){ // Wait for DHT22 pin to go high k++; if(k > 100){ Time_out = 1; break; } delay_us(1); } delay_us(30); if(!input_state(dht22)) bit_clear(_data, (7 - i)); // Clear bit (7 - i) else{ bit_set(_data, (7 - i)); // Set bit (7 - i) while(input_state(dht22)){ // Wait for DHT22 pin to go low k++; if(k > 100){ Time_out = 1; break; } delay_us(1);} } } return _data; } void main(){ lcd_init(); // Initialize LCD module lcd_putc('\f'); // LCD clear delay_ms(1000); while(TRUE){ Time_out = 0; dht11_Start_signal(); if(dht11_check_response()){ // If there is a response from dht11 sensor RH_byte1 = dht11_Read_Data(); // read RH byte1 RH_byte2 = dht11_Read_Data(); // read RH byte2 T_byte1 = dht11_Read_Data(); // read T byte1 T_byte2 = dht11_Read_Data(); // read T byte2 Checksum = dht11_Read_Data(); // read checksum if(Time_out){ // If reading takes long time lcd_gotoxy(1, 1); // Go to column 1 row 1 lcd_putc(" DHT11 Time out! "); lcd_gotoxy(1, 2); // Go to column 1 row 1 lcd_putc(" "); // Clear second row } else{ if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){ message1[13] = T_Byte1/10 + 48; message1[14] = T_Byte1%10 + 48; message1[16] = T_Byte2/10 + 48; message2[13] = RH_Byte1/10 + 48; message2[14] = RH_Byte1%10 + 48; message2[16] = RH_Byte2/10 + 48; message1[17] = 223; // Degree symbol lcd_gotoxy(1, 1); // Go to column 1 row 1 printf(lcd_putc, message1); // Display message1 lcd_gotoxy(1, 2); // Go to column 1 row 2 printf(lcd_putc, message2); // Display message2 } else{ lcd_gotoxy(1, 1); // Go to column 1 row 1 lcd_putc("DHT11 Checksum Error"); lcd_gotoxy(1, 2); // Go to column 1 row 2 lcd_putc(" "); // Clear second row } } } else { // If dht11 sensor don't respond lcd_gotoxy(1, 1); // Go to column 1 row 1 lcd_putc(" No response "); lcd_gotoxy(1, 2); // Go to column 1 row 2 lcd_putc(" from DHT11 sensor "); } Time_out = 0; dht22_Start_signal(); if(dht22_check_response()){ // If there is a response from sensor RH_byte1 = dht22_Read_Data(); // read RH byte1 RH_byte2 = dht22_Read_Data(); // read RH byte2 T_byte1 = dht22_Read_Data(); // read T byte1 T_byte2 = dht22_Read_Data(); // read T byte2 Checksum = dht22_Read_Data(); // read checksum if(Time_out){ // If reading takes long time lcd_gotoxy(21, 1); // Go to column 1 row 1 lcd_putc(" DHT22 Time out! "); lcd_gotoxy(21, 2); // Go to column 1 row 4 lcd_putc(" "); // Clear fourth row } else{ if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){ RH = RH_byte1; RH = (RH << 8) | RH_byte2; Temp = T_byte1; Temp = (Temp << 8) | T_byte2; if(Temp > 0X8000){ message3[12] = '-'; Temp = Temp & 0X7FFF; } else message3[12] = ' '; message3[13] = (Temp / 100) % 10 + 48; message3[14] = (Temp / 10) % 10 + 48; message3[16] = Temp % 10 + 48; message4[13] = (RH / 100) % 10 + 48; message4[14] = (RH / 10) % 10 + 48; message4[16] = RH % 10 + 48; message3[17] = 223; // Degree symbol lcd_gotoxy(21, 1); // Go to column 1 row 3 printf(lcd_putc, message3); // Display message3 lcd_gotoxy(21, 2); // Go to column 1 row 4 printf(lcd_putc, message4); // Display message4 } else{ lcd_gotoxy(21, 1); // Go to column 1 row 3 lcd_putc("DHT22 Checksum Error"); lcd_gotoxy(21, 2); // Go to column 1 row 4 lcd_putc(" "); // Clear fourth row } } } else { // If dht22 sensor don't respond lcd_gotoxy(21, 1); // Go to column 1 row 3 lcd_putc(" No response "); lcd_gotoxy(21, 2); // Go to column 1 row 4 lcd_putc(" from DHT22 sensor "); } delay_ms(1000); } } // End of program |
DHT11 VS DHT22 Video:
The following video shows the comparison between the two sensors results.
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.