You are on page 1of 5

Interfacing PIC18F4550 with DHT11(RHT01) humidity

and temperature sensor
PIC18F4550 + DHT11 (RHT01) Proteus simulation
This topic shows how to interface DHT11 (RHT01) digital relative humidity and
temperature sensor with PIC18F4550 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.

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.

but it is very important to know how it sends the digital data. The figure below shows how the sensor sends its information: Interfacing PIC18F4550 with DHT11(RHT01) sensor circuit: The following circuit schematic shows complete project circuit. . 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". Data format: 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum. The DHT11 is a digital sensor so it sends 1's and 0's. A complete data transmission is 40bit. and the sensor sends higher data bit first.Data consists of decimal and integral parts. If the data transmission is right.

0 // Pin direction mapped to TRISB. Interfacing PIC18F4550 with DHT11(RHT01) sensor CCS C code: The interfacing code is written with CCS PIC C compiler PCWHD version 5. void start_signal(){ Data_Pin_Direction = 0.0 // Pin mapped to PORTB.h> #fuses NOMCLR INTRC_IO #use delay(clock = 8000000) #include <lcd. char message2[] = "RH = 00. Variables Time_out and k are used to test reading time to avoid wrong data reception or microcontroller hanging. DHT11 sensor and 1602 LCD to display humidity and temperature // electronnote@gmail.051. unsigned int8 T_byte1.0 C".0 %". short Time_out. CheckSum . T_byte2.0 #BIT Data_Pin_Direction = 0xF93. // Interfacing PIC18F4550 with DHT11 sensor // //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 <18F4550.The circuit is simple.blogspot. If you want to understand the code please read the DHT11 datasheet.0 char message1[] = "Temp = 00. RH_byte1. // Configure connection pin as output Data_Pin = 0. there is the microcontroller PIC18F4550. // Connection pin output low delay_ms(25). RH_byte2. PIC18F4550 uses its internal oscillator @ 8MHz and mclr pin function is disabled.c> #use fast_io(B) // Connection pin between PIC18F4550 and DHT11(RHT01) sensor #BIT Data_Pin = 0xF81. .

// Set internal oscillato r to 8MHz setup_adc_ports(NO_ANALOGS). Time_out = 0. Data_Pin = 1.} delay_us(1). // read RH byte1 RH_byte2 = Read_Data(). break. if (k > 100) {Time_out = 1. // read T byte1 T_byte2 = Read_Data(). (7 . // k is used to count 1 bit readi ng duration if(Time_out) break. k.} } } return _data. Data_Pin_Direction = 1. i++){ k = 0. _data = 0.i)). i < 8.} delay_us(30). // Clear bit (7 .} delay_us(1). if(!Data_Pin) bit_clear(_data.} } } unsigned int8 Read_Data(){ unsigned int8 i. if(check_response()){ // If there is response f rom sensor RH_byte1 = Read_Data().i) while(Data_Pin){ // Wait until pin goes low k++.i) else{ bit_set(_data. Start_signal(). } void main(){ setup_oscillator(OSC_8MHZ). // read RH byte2 T_byte1 = Read_Data(). // read T byte2 Checksum = Read_Data(). if(!Data_Pin){ // Read and test if connection pi n is low delay_us(80). // Initialize LCD module lcd_putc('\f'). // LCD clear while(TRUE){ delay_ms(1000). for(i = 0. while(!Data_Pin){ // Wait until pin goes high k++. // Configure connection pin as input } short check_response(){ delay_us(40). // Connection pin output high delay_us(30). (7 . break. // read checksum .i)). return 1. if (k > 100) {Time_out = 1. // Set bit (7 . // Configure AN pins as digital lcd_init(). if(Data_Pin){ // Read and test if connection pi n is high delay_us(50).

1). // Go to column 1 row 2 lcd_putc("from the sensor"). // Display message2 } else { lcd_putc('\f'). // Display message1 lcd_gotoxy(1. 2). 1). message1[10] = T_Byte2/10 + 48. if(Time_out){ // If reading takes long time lcd_putc('\f'). message1[8] = T_Byte1%10 + 48. message2). // LCD clear lcd_gotoxy(5. 2). // Go to column 3 row 1 lcd_putc("No response"). // LCD clear lcd_gotoxy(1. // LCD clear lcd_gotoxy(1. message2[7] = RH_Byte1/10 + 48. // Go to column 1 row 1 lcd_putc("Checksum Error!"). // Go to column 5 row 1 lcd_putc("Time out!"). 1). 1). } } } . } else{ if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xF F)){ message1[7] = T_Byte1/10 + 48. lcd_gotoxy(1. // LCD clear lcd_gotoxy(3. // Go to column 1 row 2 printf(lcd_putc. } } } else { lcd_putc('\f'). message2[10] = RH_Byte2/10 + 48. // Degree symbol lcd_putc('\f'). message1[11] = 223. message2[8] = RH_Byte1%10 + 48. // Go to column 1 row 1 printf(lcd_putc. message1).