Saturday, November 28, 2015

Interface PIC12F683 with DHT11 sensor

Interface PIC12F683 with DHT11 digital humidity and temperature sensor
There are many topics on this blog talking about how to interface the digital humidity and temperature sensor DHT11 with different types of PIC microcontrolles and Arduino.
Simply the DHT11 is a digital humidity and temperature sensor which uses one wire to communicate with the microcontroller.
In this topic we will see how to interface this sensor with the microcontroller PIC12F683 and display the results on 1602 LCD display.
Generally the Lcd display uses at least 6 data lines but we have 6 pins can be used as outputs in the PIC12F683, that why we have developed a new 2-wire serial Lcd display. This 2-wire LCD uses another library which is nearly the same as the original MikroC LCD library, this library is simple and easy to use. To see how to use 2-wire LCD display visit the following topic:
Interface PIC12F683 with LCD display

Interface PIC12F683 with DHT11 sensor circuit:
Circuit schematic diagram is shown below.
Internal oscillator is used at 8MHz and MCLR pin function is disabled.

pic12f683 dht11 mikroc

Interface PIC12F683 with DHT11 mikroC Code:
2-WIRE SERIAL LCD LIBRARY MUST BE INSTALLED!!

// Interface PIC12F683 with DHT11 sensor
// Serial LCD library must be installed
// Written by: BENCHEROUDA Okba
// electronnote@gmail.com
// http://elecnote.blogspot.com/
// Internal oscillator used at 8MHz
// Use at your own risk

  // LCD module connections
 sbit Serial_LCD_DATA at GP0_bit;
 sbit Serial_LCD_CLK at GP1_bit;
 sbit Serial_LCD_DATA_Direction at TRISIO0_bit;
 sbit Serial_LCD_CLK_Direction at TRISIO1_bit;
  // End LCD module connections

unsigned short  Check, Temp, RH, Sum, Ch ;
 //////////////////////////////
void StartSignal(){
 TRISIO.F2 = 0;    //Configure TRISIO.F2 as output
 GPIO.F2 = 0;    //GP2 sends 0 to the sensor
 delay_ms(18);
 GPIO.F2 = 1;    //GP2 sends 1 to the sensor
 delay_us(30);
 TRISIO.F2 = 1;  //Configure TRISIO.F2 as input
  }
 //////////////////////////////
 void CheckResponse(){
 Check = 0;
 delay_us(40);
 if (!GPIO.F2){
 delay_us(80);
 if (GPIO.F2)   Check = 1;   delay_us(40);}
 }
 //////////////////////////////
 char ReadData(){
 char i, j;
 for(j = 0; j < 8; j++){
 while(!GPIO.F2); //Wait until GP2 goes HIGH
 delay_us(30);
 if(!GPIO.F2)
       i&= ~(1 << (7 - j));  //Clear bit (7-b)
 else {i|= (1 << (7 - j));  //Set bit (7-b)
 while(GPIO.F2);  //Wait until GP2 goes LOW
 }
 }
 return i;
 }
 //////////////////////////////
 void main() {
  OSCCON = 0x71;       // Configure internal oscillator (8MHz)
  ANSEL = 0;           // All I/O pins are configured as digital
  ADCON0 = 0;         // Disable ADC module
  GPIO = 0;
  TRISIO = 0;
  Serial_Lcd_Init();                 // LCD display initialization
  Serial_Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
  Serial_Lcd_Cmd(_LCD_CLEAR);             // clear LCD
  while(1){
  StartSignal();
  CheckResponse();
  if(Check){
  RH = ReadData();
  ReadData();
  Temp = ReadData();
  ReadData();
  Sum = ReadData();
  if(Sum == (RH + Temp)){
  Serial_Lcd_Out(1, 1, "    Temp:   .0 C");
  Serial_Lcd_Out(2, 1, "Humidity:   .0 %");
  Serial_LCD_Chr(1, 11, 48 + ((Temp / 10) % 10));
  Serial_LCD_Chr(1, 12, 48 + (Temp % 10));
  Serial_LCD_Chr(2, 11, 48 + ((RH / 10) % 10));
  Serial_LCD_Chr(2, 12, 48 + (RH % 10));
  }
  else{
  Serial_Lcd_Cmd(_LCD_CLEAR);
  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);
  }
 }

Interface PIC12F683 with DHT11 video:
The following video shows Proteus simulation of this project: