PIC - DHT22 with PIC16F887 not working - Page 1
Forum section: electronics / general

author: Rafa_Forum
author_rank: Newbie
timestamp: July 19, 2022, 02:25:40 pm
content: 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.Code:// LCD module connectionssbit 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 connectionchar 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 outputDHT22_PIN = 0; // Connection pin output lowDelay_ms(25); // Wait 25 msDHT22_PIN = 1; // Connection pin output highDelay_us(25); // Wait 25 usDHT22_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 initializedLcd_Cmd(_LCD_CURSOR_OFF);Lcd_Cmd(_LCD_CLEAR); //Clean displaywhile(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 = '-';Temp = Temp & 0x7FFF;}else{temperature = ' ';temperature = (Temp / 100)%10 + 48;temperature = (Temp / 10)%10 + 48;temperature = Temp / 10 + 48;}if(RH == 1000){humidity = 1 + 48;}else{humidity = ' ';humidity = (RH / 100) % 10 + 48;humidity = (RH / 10) % 10 + 48;humidity = RH % 10 + 48;}temperature = 223; //° Symbollcd_out(1, 1, temperature); //Display temperaturelcd_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);}}

author: macboy
author_rank: Super Contributor
timestamp: July 19, 2022, 03:52:22 pm
content: Look at the schematic more closely, RB0 has been directly connected to 5V.

author: rooppoorali
author_rank: Regular Contributor
timestamp: July 20, 2022, 11:57:39 am
content: Kindly make the correction suggested by MACBOY. Data pin is not supposed to be connected to VCC. Please correct it and let us know the update.