Interfacing PIC16F877A with DHT22(AM2302-RHT03) sensor using CCS PIC C

Interfacing PIC16F877A with DHT22(AM2302-RHT03) digital humidity and temperature sensor
Interfacing PIC16F877A with DHT22(AM2302-RHT03) sensor using CCS PIC C
This topic shows how to interface PIC16F877A microcontroller with DHT22 sensor with hardware circuit.
Related topic:
The following topic shows PIC16F877A microcontroller and DHT22 Proteus simulation and some details about this sensor.
PIC16F877A and DHT22(AM2302, RHT03) sensor Proteus simulation
Interfacing PIC16F877A with DHT22(AM2302, RHT03) sensor circuit:
The following circuit schematic shows complete project circuit.Circuit Interfacing PIC16F877A with DHT22(AM2302-RHT03) sensor using CCS PIC C
The circuit is simple, there is the microcontroller PIC16F877A, DHT22 sensor and 1602 LCD to display humidity and temperature results.
Interfacing PIC16F877A with DHT22(AM2302, RHT03) 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 DHT22 datasheet.

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

//Β InterfacingΒ PIC16F877AΒ withΒ DHT22(AM2302-RHT03)Β sensorΒ CCSΒ CΒ code
//Β 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Β DHT22Β 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 ;
unsigned int16 Temp, RH;
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)){
Β Β Β Β Β Β Β Β RHΒ =Β RH_byte1;
Β Β Β Β Β Β Β Β RHΒ =Β (RHΒ <<Β 8)Β |Β RH_byte2;
Β Β Β Β Β Β Β Β TempΒ =Β T_byte1;
Β Β Β Β Β Β Β Β TempΒ =Β (TempΒ <<Β 8)Β |Β T_byte2;
Β Β Β Β Β Β Β Β if (Temp > 0X8000){
Β Β Β Β Β Β Β Β Β message1[6]Β =Β '-';
Β Β Β Β Β Β Β Β Β TempΒ =Β TempΒ &Β 0X7FFF;Β }
Β Β Β Β Β Β Β Β else
Β Β Β Β Β Β Β Β Β message1[6]Β =Β ' ';
Β Β Β Β Β Β Β Β message1[7]Β Β =Β (TempΒ /Β 100)Β %Β 10Β Β +Β 48;
Β Β Β Β Β Β Β Β message1[8]Β Β =Β (TempΒ /Β 10)Β %Β 10Β Β +Β 48;
Β Β Β Β Β Β Β Β message1[10]Β =Β TempΒ %Β 10Β Β +Β 48;
Β Β Β Β Β Β Β Β message2[7]Β Β =Β (RHΒ /Β 100)Β %Β 10Β +Β 48;
Β Β Β Β Β Β Β Β message2[8]Β Β =Β (RHΒ /Β 10)Β %Β 10Β +Β 48;
Β Β Β Β Β Β Β Β message2[10]Β =Β RHΒ %Β 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 DHT22 sensor video:
The following video shows hardware circuit of this project.


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.