DHT22 with PIC16F887 not working

Thread Starter

Rafa_Forum

Joined Jul 19, 2022
1
Hi guys!

I am trying to make a DHT22 sensor interface using a PIC16F887. I compiled the code below and it works, but when I simulated it in Proteus, there is a problem with the "Check_Response" function. The LCD displays the message "No response from the sensor".

I am using the mikroC PRO for PIC compiler and Proteus 8 Professional for simulations.
I attach the schema used in Proteus.

DHT22 Interface:
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// end LCD module connections

// DHT22 sensor connection (here sensor data pin is connected to pin RB0)
sbit DHT22_PIN at RB0_bit;
sbit DHT22_PIN_Direction at TRISB0_bit;
// End DHT11 sensor connection

char temperature[] = "Temp = 00.0 C  ";
char humidity[] = "RH   = 00.0 %  ";
unsigned short T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum, checkBit ;
unsigned int Temp, RH;

void Start_Signal(void){
   DHT22_PIN_Direction = 0;              // Configure connection pin as output
   DHT22_PIN = 0;                        // Connection pin output low
   Delay_ms(25);                         // Wait 25 ms
   DHT22_PIN = 1;                        // Connection pin output high
   Delay_us(25);                         // Wait 25 us
   DHT22_PIN_Direction = 1;              // Configure connection pin as input
}

void Check_Response(void){
   checkBit = 0;
   Delay_us(40);
   if(DHT22_Pin == 0){
      Delay_us(80);
      if(DHT22_Pin == 1){
         checkBit = 1;
      }
   Delay_us(50);
   }
}

char Read_Dht22(){
   char i;
   char dataVar;
   for(i = 0; i < 8; i++){
      while(!DHT22_Pin);
      Delay_us(30);
      if(DHT22_Pin == 0){
         dataVar&= ~(1<<(7 - i));        //Clear bit (7-b)
      }else{
         dataVar|= (1 << (7 - i));       //Set bit (7-b)
         while(DHT22_Pin);
      }                                  //Wait until PORTD.F0 goes LOW
   }
   return dataVar;                       //Read value
}

void main() {
   //ANSELH = 0;
   Lcd_Init();                           //Lcd initialized
   Lcd_Cmd(_LCD_CURSOR_OFF);
   Lcd_Cmd(_LCD_CLEAR);                  //Clean display
   while(1){
      Delay_ms(800);
      Start_Signal();
      if(checkBit){
         RH_Byte1 = Read_Dht22();
         RH_Byte2 = Read_Dht22();
         T_Byte1 = Read_Dht22();
         T_Byte2 = Read_Dht22();
         CheckSum = Read_Dht22();
         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){
               temperature[6] = '-';
               Temp = Temp & 0x7FFF;
            }else{

               temperature[6] = ' ';
               temperature[7] = (Temp / 100)%10 + 48;
               temperature[8] = (Temp / 10)%10 + 48;
               temperature[7] = Temp / 10 + 48;
            }
            if(RH == 1000){
               humidity[6]  = 1 + 48;
            }else{

               humidity[6]  = ' ';
               humidity[7]  = (RH / 100) % 10 + 48;
               humidity[8]  = (RH / 10) % 10 + 48;
               humidity[10] = RH % 10 + 48;
            }
            temperature[11] = 223;       //° Symbol
            lcd_out(1, 1, temperature);  //Display temperature
            lcd_out(2, 1, humidity);     //Display humidity
         }else{

            Lcd_Cmd(_LCD_CLEAR);
            lcd_out(1, 1, "Checksum Error!");
         }
      }else{

         Lcd_Cmd(_LCD_CLEAR);
         Lcd_out(1, 3, "No response");
         Lcd_out(2, 1, "from the sensor");
      }
      Delay_ms(1000);
   }
}
DHT22 - Copy.jpg
 
Top