Thursday, November 5, 2015

Interfacing PIC18F4550 With DHT11 Sensor and 2-Wire LCD (Hardware)

How to interface PIC18F4550 with DHT11 (digital humidity and temperature sensor) and a 2-wire serial 16x2 LCD

This simple project talks about interfacing PIC18F4550 with the very known digital humidity and temperature sensor DHT11 and a 2-wire serial LCD display. The DHT11 uses only one wire to communicate with the microcontroller and the serial LCD uses only two wires one is data line and the other is clock line.
The 2-wire serial LCD needs its mikroC library to work, it is very simple, more details about this library on the following topic: 
2-Wire LCD MikroC Library 

Interfacing PIC18F4550 with DHT11 sensor and 2-wire LCD circuit:
The DHT11 datasheet must be read in-order to understand how it works and the code will be more easier.
The complete circuit schematic of this project is shown below:
pic18f4550 dht11mikroc

The internal oscillator of the PIC18F4550 is used at 8MHz and the MCLR pin is disabled which means no VCC needed at MCLR pin.
The type of the LCD is 16x2 but other types such as 20x4 can be used.
The shift register is 74HC164 and any serial-in parallel-out shift register will work like 74HC595 or CD4094.
Interfacing PIC18F4550 with DHT11 sensor and 2-wire LCD mikroC code:

 // PIC18F4550 + 2-Wire LCD + DHT11 digital humidity
 //        and temperature sensor MikroC code
// Written by: BENCHEROUDA Okba
// electronnote@gmail.com
// http://elecnote.blogspot.com/
// Internal crystal used at 8MHz
// Use at your own risk


 // LCD module connections
sbit Serial_LCD_DATA at RB0_bit;
sbit Serial_LCD_CLK at RB1_bit;
sbit Serial_LCD_DATA_Direction at TRISB0_bit;
sbit Serial_LCD_CLK_Direction at TRISB1_bit;
// End LCD module connections

 unsigned char  Check, T_byte1, T_byte2,
                RH_byte1, RH_byte2, Ch ;
 unsigned Temp, RH, Sum ;
void StartSignal(){
 TRISD.F0 = 0;    //Configure RD0 as output
 PORTD.F0 = 0;    //RD0 sends 0 to the sensor
 delay_ms(18);
 PORTD.F0 = 1;    //RD0 sends 1 to the sensor
 delay_us(30);
 TRISD.F0 = 1;    //Configure RD0 as input
  }
 //////////////////////////////
 void CheckResponse(){
 Check = 0;
 delay_us(40);
 if (PORTD.F0 == 0){
 delay_us(80);
 if (PORTD.F0 == 1)   Check = 1;   delay_us(40);}
 }
 //////////////////////////////
 char ReadData(){
 char i, j;
 for(j = 0; j < 8; j++){
 while(!PORTD.F0); //Wait until PORTD.F0 goes HIGH
 delay_us(30);
 if(PORTD.F0 == 0)
       i&= ~(1<<(7 - j));  //Clear bit (7-b)
 else {i|= (1 << (7 - j));  //Set bit (7-b)
 while(PORTD.F0);}  //Wait until PORTD.F0 goes LOW
 }
 return i;
 }
 //////////////////////////////
void main() {
 OSCCON =  0X70;       // Set internal oscillator at 8MHz
 ADCON1 = 0x0F;        // Configure AN pins as digital
 TRISD.F1 = 0;
 Serial_Lcd_Init();
 Serial_Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
 Serial_Lcd_Cmd(_LCD_CLEAR);            // clear LCD
 while(1){
 StartSignal();
  CheckResponse();
  if(Check == 1){
  RH_byte1 = ReadData();
  RH_byte2 = ReadData();
  T_byte1 = ReadData();
  T_byte2 = ReadData();
  Sum = ReadData();
  if(Sum == ((RH_byte1+RH_byte2+T_byte1+T_byte2) & 0XFF)){
  Temp = T_byte1;
  RH = RH_byte1;
  Serial_Lcd_Out(1, 1, "     Temp:   C ");
  Serial_Lcd_Out(2, 1, " Humidity:   % ");
  Serial_LCD_Chr(1, 12, 48 + ((Temp / 10) % 10));
  Serial_LCD_Chr(1, 13, 48 + (Temp % 10));
  Serial_LCD_Chr(2, 12, 48 + ((RH / 10) % 10));
  Serial_LCD_Chr(2, 13, 48 + (RH % 10));
  }
  else{
  Serial_Lcd_Cmd(_LCD_CLEAR);             // clear LCD
  Serial_Lcd_Out(1, 1, "Check sum error");}
  }
  else {
  Serial_Lcd_Out(1, 1, "  No response  ");
  Serial_Lcd_Out(2, 1, "from the sensor");
  }
  delay_ms(1000);
  }
}

Interfacing PIC18F4550 With DHT11 Sensor and 2-Wire LCD video:


Simulation files download