Interfacing PIC16F877A with DHT11 (RHT01) sensor Proteus simulation

PIC16F877A + DHT11 (RHT01) Proteus simulation
This topic shows how to interface DHT11 (RHT01) digital relative humidity and temperature sensor with PIC16F877A microcontroller, and how to simulate this interfacing using Proteus.
Note that for the simulation Proteus version should beΒ 8.1 or higher. With these versions there is no need to install Proteus DHT11 library, it is included with the software, so don’t waste your time searching forΒ dht11 Proteus libraryΒ orΒ dhtxx.mdfΒ orΒ dht11 module for Proteus, just useΒ Proteus version 8.1Β or higher.
About DHT11 (RHT01) relative humidity and temperature sensor:
The DHT11 sensor comes in a single row 4-pin package and operates from 3.3Β to 5.5V power supply. It can measure temperature from 0-50 Β°C with an accuracy of Β±2Β°C and relative humidity ranging from 20-90% with an accuracy ofΒ  Β±5%. The sensor provides fully calibrated digital outputs for the two measurements. It has got its own proprietary 1-wire protocol, and therefore, the communication between the sensor and a microcontroller is not possible through a direct interface with any of its peripherals. The protocol must be implemented in the firmware of the MCU with precise timing required by the sensor.Interfacing PIC16F877A with DHT11 (RHT01) sensor Proteus simulation
The following timing diagrams describe the data transfer protocol between a MCU and the DHT11 sensor. The MCU initiates data transmission by issuing a β€œStart” signal. The MCU pin must be configured as output for this purpose. The MCU first pulls the data line low for at least 18 ms and then pulls it high for next 20-40 us before it releases it. Next, the sensor responds to the MCU β€œStartβ€œΒ  signal by pulling the line low for 80 us followed by a logic high signal that also lasts for 80 us. Remember that the MCU pin must be configured to input after finishing the β€œStartβ€œ signal. Once detecting the response signal from the sensor, the MCU should be ready to receive data from the sensor. The sensor then sends 40 bits (5 bytes) of data continuously in the data line. Note that while transmitting bytes, the sensor sends the most significant bit first.Curcuit Interfacing PIC16F877A with DHT11 (RHT01) sensor Proteus simulation
Data format:Β 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum. If the data transmission is right, the check-sum should be the last 8bit of β€œ8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data”.
The DHT11 is a digital sensor so it sends 1’s and 0’s, but it is very important to know how it sends the digital data. The figure below shows how the sensor sends its information:
Interfacing PIC16F877A with DHT11 (RHT01) sensor circuit:
The following circuit schematic shows complete project circuit.
The circuit is simple, there is the microcontroller PIC16F877A, DHT11 sensor and 1602 LCD to display humidity and temperature results.
Interfacing PIC16F877A with DHT11 (RHT01) sensor CCS C code:
The interfacing code is written with CCS PIC C compiler PCWHD version 5.051.
If you want to understand the code please read the DHT11 datasheet.

VariablesΒ Time_outΒ andΒ kΒ are used to test reading time to avoid wrong data reception or microcontroller hanging.

//Β InterfacingΒ PIC16F877AΒ withΒ DHT11Β sensor
//Β http://ccspicc.blogspot.com/
//Β [email protected]

//LCDΒ moduleΒ connections
#defineΒ LCD_RS_PINΒ PIN_B0
#defineΒ LCD_RW_PINΒ PIN_B1
#defineΒ LCD_ENABLE_PINΒ PIN_B2
#defineΒ LCD_DATA4Β PIN_B3
#defineΒ LCD_DATA5Β PIN_B4
#defineΒ LCD_DATA6Β PIN_B5
#defineΒ LCD_DATA7Β PIN_B6
//EndΒ LCDΒ moduleΒ connections

#includeΒ <16F877A.h>
#fusesΒ HS,NOWDT,NOPROTECT,NOLVP
#useΒ delay(clockΒ =Β 8000000)
#includeΒ <lcd.c>
#useΒ fast_io(D)
//Β ConnectionΒ pinΒ betweenΒ PIC16F877AΒ andΒ DHT11Β (RHT01)Β sensor
#BITΒ Data_PinΒ =Β 0x08.0Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β PinΒ mappedΒ toΒ PORTD.0
#BITΒ Data_Pin_DirectionΒ =Β 0x88.0Β Β Β Β Β Β Β Β Β Β Β Β Β //Β PinΒ directionΒ mappedΒ toΒ TRISD.0

charΒ message1[]Β =Β "TempΒ =Β 00.0Β C";
charΒ message2[]Β =Β "RHΒ Β Β =Β 00.0Β %";
shortΒ Time_out;
unsignedΒ int8Β T_byte1,Β T_byte2,Β RH_byte1,Β RH_byte2,Β CheckSumΒ ;
voidΒ start_signal(){
Β Β Data_Pin_DirectionΒ =Β 0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ConfigureΒ connectionΒ pinΒ asΒ output
Β Β Data_PinΒ =Β 0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ConnectionΒ pinΒ outputΒ low
Β Β delay_ms(25);
Β Β Data_PinΒ =Β 1;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ConnectionΒ pinΒ outputΒ high
Β Β delay_us(30);
Β Β Data_Pin_DirectionΒ =Β 1;Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ConfigureΒ connectionΒ pinΒ asΒ input
}
shortΒ check_response(){
Β Β delay_us(40);
Β Β if(!Data_Pin){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ReadΒ andΒ testΒ ifΒ connectionΒ pinΒ isΒ low
Β Β Β Β delay_us(80);
Β Β Β Β if(Data_Pin){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ReadΒ andΒ testΒ ifΒ connectionΒ pinΒ isΒ high
Β Β Β Β Β Β delay_us(50);
Β Β Β Β Β Β returnΒ 1;}
Β Β Β Β }
}
unsignedΒ int8Β 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(!Data_Pin){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β WaitΒ untilΒ pinΒ goesΒ high
Β Β Β Β Β Β k++;
Β Β Β Β Β Β ifΒ (kΒ >Β 100)Β {Time_outΒ =Β 1;Β break;}
Β Β Β Β Β Β delay_us(1);}
Β Β Β Β delay_us(30);
Β Β Β Β if(!Data_Pin)
Β Β Β Β Β Β bit_clear(_data,Β (7Β -Β i));Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ClearΒ bitΒ (7Β -Β i)
Β Β Β Β else{
Β Β Β Β Β Β bit_set(_data,Β (7Β -Β i));Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β SetΒ bitΒ (7Β -Β i)
Β Β Β Β Β Β while(Data_Pin){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β WaitΒ untilΒ pinΒ goesΒ 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
Β Β while(TRUE){
Β Β Β Β delay_ms(1000);
Β Β Β Β Time_outΒ =Β 0;
Β Β Β Β Start_signal();
Β Β Β Β if(check_response()){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β IfΒ thereΒ isΒ responseΒ fromΒ sensor
Β Β Β Β Β Β RH_byte1Β =Β Read_Data();Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β readΒ RHΒ byte1
Β Β Β Β Β Β RH_byte2Β =Β Read_Data();Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β readΒ RHΒ byte2
Β Β Β Β Β Β T_byte1Β =Β Read_Data();Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β readΒ TΒ byte1
Β Β Β Β Β Β T_byte2Β =Β Read_Data();Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β readΒ TΒ byte2
Β Β Β Β Β Β ChecksumΒ =Β Read_Data();Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β readΒ checksum
Β Β Β Β Β Β if(Time_out){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β IfΒ readingΒ takesΒ longΒ time
Β Β Β Β Β Β Β Β lcd_putc('\f');Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β LCDΒ clear
Β Β Β Β Β Β Β Β lcd_gotoxy(5,Β 1);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β GoΒ toΒ columnΒ 5Β rowΒ 1
Β Β Β Β Β Β Β Β lcd_putc("TimeΒ out!");
Β Β Β Β Β Β }
Β Β Β Β Β Β else{
Β Β Β Β Β Β Β if(CheckSumΒ ==Β ((RH_Byte1Β +Β RH_Byte2Β +Β T_Byte1Β +Β T_Byte2)Β &Β 0xFF)){
Β Β Β Β Β Β Β Β message1[7]Β Β =Β T_Byte1/10Β Β +Β 48;
Β Β Β Β Β Β Β Β message1[8]Β Β =Β T_Byte1%10Β Β +Β 48;
Β Β Β Β Β Β Β Β message1[10]Β =Β T_Byte2/10Β Β +Β 48;
Β Β Β Β Β Β Β Β message2[7]Β Β =Β RH_Byte1/10Β +Β 48;
Β Β Β Β Β Β Β Β message2[8]Β Β =Β RH_Byte1%10Β +Β 48;
Β Β Β Β Β Β Β Β message2[10]Β =Β RH_Byte2/10Β +Β 48;
Β Β Β Β Β Β Β Β message1[11]Β =Β 223;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β DegreeΒ symbol
Β Β Β Β Β Β Β Β lcd_putc('\f');Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β LCDΒ clear
Β Β Β Β Β Β Β Β 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_putc('\f');Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β LCDΒ clear
Β Β Β Β Β Β Β Β Β Β lcd_gotoxy(1,Β 1);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β GoΒ toΒ columnΒ 1Β rowΒ 1
Β Β Β Β Β Β Β Β Β Β lcd_putc("ChecksumΒ Error!");
Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β }
Β Β Β Β }
Β Β Β Β elseΒ {
Β Β Β Β Β Β lcd_putc('\f');Β Β Β Β Β Β Β Β Β Β Β Β Β //Β LCDΒ clear
Β Β Β Β Β Β lcd_gotoxy(3,Β 1);Β Β Β Β Β Β Β Β Β Β Β //Β GoΒ toΒ columnΒ 3Β rowΒ 1
Β Β Β Β Β Β lcd_putc("NoΒ response");
Β Β Β Β Β Β lcd_gotoxy(1,Β 2);Β Β Β Β Β Β Β Β Β Β Β //Β GoΒ toΒ columnΒ 1Β rowΒ 2
Β Β Β Β Β Β lcd_putc("fromΒ theΒ sensor");
Β Β Β Β }
Β Β }
}

Interfacing PIC16F877A with DHT11 (RHT01) sensor Proteus simulation video:
The following video shows Proteus simulation of this interfacing.


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.