DHT11 SENSOR with PIC16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by RRITESH KAKKAR, Jul 15, 2015.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    How to a Display humidity sensor DHT11 on leds?
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    First you need to draw a schematic with headers for a DHT11, socket for you PIC, and some kind of display. When you say you want to display humidity sensor on leds, what exactly do you mean?5mm LED or 7-segments?

    You'd better start with a schematic drawing.

    Remember: "First comes the hardware, then comes the software."
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    How are you doing with that round graphical LED display that makes patterns, Rritesh?
     
  4. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,508
    380
    hi Ritesh,
    Look at the DHT22 sensor, it has a superior specification compared to the DHT11.
     
  5. theonewho

    New Member

    Jul 9, 2015
    17
    2
    Your question is poorly specified. Perhaps this similar post can help you formulate a better question: DHT11 Sensor
     
  6. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    The sensor with 7 segment. there is 3 pin sensor of 40 bits data according to data sheet.

    not build right now!
     
  7. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    What datasheet?

    Please attach one. I got some DHT11, and they do not have any 7-segments.
     
  8. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    I mean to say that the DHT11 is to be connected with micro and micro will display the Humidity on it.
    how to do communication process?
     
  9. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    Like this:
    at168_lcd_dht11.JPG

    Here I have used a 2x16 LCD display. If you see page 3 in the attached datasheet, you'll see the schematic diagram. Further down the document, you'll see how to communicate.

    My picture are using an ATmega168... :(
     
    RRITESH KAKKAR likes this.
  10. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,508
    380
    theonewho likes this.
  11. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    What is difference between integers and decimal data
     
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    In this link..
    http://embedded-lab.com/blog/?p=4333


    Code (C):
    1. // LCD module connections sbit LCD_RS at RB2_bit; sbit LCD_EN at RB3_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB2_bit; sbit LCD_EN_Direction at TRISB3_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit;   sbit Data at RA0_bit; sbit DataDir at TRISA0_bit; char message1[] = "Temp = 00.0 C"; char message2[] = "RH = 00.0 %"; unsigned short TOUT = 0, CheckSum, i; unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;   void StartSignal(){ DataDir = 0; // Data port is output Data = 0; Delay_ms(25); // Low for at least 18us Data = 1; Delay_us(30); // High for 20-40 us DataDir = 1; // Data port is input   }   unsigned short CheckResponse(){ TOUT = 0; TMR2 = 0; T2CON.TMR2ON = 1; // Start TMR2 while waiting for sensor response while(!Data && !TOUT); // If there's no response within 256us, the Timer2 overflows if (TOUT) return 0; // and exit else { TMR2 = 0; while(Data && !TOUT); if (TOUT) return 0; else { T2CON.TMR2ON = 0; return 1; } } }   unsigned short ReadByte(){ unsigned short num = 0, t; DataDir = 1; for (i=0; i<8; i++){ while(!Data); TMR2 = 0; T2CON.TMR2ON = 1; // Start TMR2 from 0 when a low to high data pulse while(Data); // is detected, and wait until it falls low again. T2CON.TMR2ON = 0; // Stop the TMR2 when the data pulse falls low. if(TMR2 > 40) num |= 1<<(7-i); // If time > 40us, Data is 1 } return num; }   void interrupt(){ if(PIR1.TMR2IF){ TOUT = 1; T2CON.TMR2ON = 0; // stop timer PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag } }   void main() { unsigned short check; TRISB = 0b00000000; PORTB = 0; TRISA = 0b00100001; CMCON = 7; INTCON.GIE = 1; //Enable global interrupt INTCON.PEIE = 1; //Enable peripheral interrupt // Configure Timer2 module PIE1.TMR2IE = 1; // Enable Timer2 interrupt T2CON = 0; // Prescaler 1:1, and Timer2 is off initially PIR1.TMR2IF =0; // Clear TMR INT Flag bit TMR2 = 0; Lcd_Init(); Lcd_Cmd(_Lcd_Clear); Lcd_Cmd(_LCD_CURSOR_OFF);   do { Delay_ms(1000); StartSignal(); check = CheckResponse(); if (!check) { Lcd_Cmd(_Lcd_Clear); Lcd_Out(1, 1, "No response"); Lcd_Out(2, 1, "from the sensor"); } else{   RH_Byte1 = ReadByte(); RH_Byte2 = ReadByte(); T_Byte1 = ReadByte(); T_Byte2 = ReadByte(); CheckSum = ReadByte(); // Check for error in Data reception 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_Cmd(_Lcd_Clear); Lcd_Out(1, 1, message1); Lcd_Out(2, 1, message2); }   else{ Lcd_Cmd(_Lcd_Clear); Lcd_Out(1, 1, "Checksum Error!"); Lcd_Out(2, 1, "Trying Again ..."); } }   }while(1); - See more at: http://embedded-lab.com/blog/?p=4333#sthash.K0Vlw2cl.dpuf
     
  13. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    sbit Data at RB0_bit;
    sbit DataDir at TRISB0_bit;
    unsigned short k;

    unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
    char temp[] = "Temperature is 00.0 C";
    char hum[] = "Humidity is 00.0 %";

    void DHT11StartSignal(){
    DataDir = 0;
    Data = 0;
    Delay_ms(25);
    Data = 1;
    Delay_us(30);
    DataDir = 1;
    }

    unsigned short DHT11CheckResponse(){
    k = 150;
    while(!Data){
    Delay_us(2);
    k--;
    if(k<1) return 0; // time out
    }
    k = 150;
    while(Data){
    Delay_us(2);
    k--;
    if(k<1) return 0; // time out
    }
    return 1;
    }


    unsigned short DHT11ReadByte(){
    int i;
    unsigned short num = 0;
    DataDir = 1;
    for (i=0; i<8; i++){
    while(!Data);
    Delay_us(40);
    if(Data) num |= 1<<(7-i);
    while(Data);
    }
    return num;
    }

    void main() {
    UART1_Init(9600);
    TRISC.RC0 = 0;
    TRISC.RC1 = 0;
    TRISC.RC2 = 0;

    while(1){
    DHT11StartSignal();
    if(!DHT11CheckResponse()) continue;
    RH_Byte1 = DHT11ReadByte();
    RH_Byte2 = DHT11ReadByte();
    T_Byte1 = DHT11ReadByte();
    T_Byte2 = DHT11ReadByte();
    DHT11ReadByte(); /* Checksum */

    // Set temp
    temp[15] = T_Byte1/10 + 48;
    temp[16] = T_Byte1%10 + 48;
    temp[18] = T_Byte2/10 + 48;
    UART1_Write_Text(temp);
    UART1_Write(10);
    UART1_Write(13);

    // Set hum
    hum[12] = RH_Byte1/10 + 48;
    hum[13] = RH_Byte1%10 + 48;
    hum[15] = RH_Byte2/10 + 48;
    UART1_Write_Text(hum);
    UART1_Write(10);
    UART1_Write(13);
    UART1_Write(10);
    UART1_Write(13);

    PORTC.RC0 = (RH_Byte1) >= 20;
    PORTC.RC1 = (RH_Byte1) >= 30;
    PORTC.RC2 = (RH_Byte1) >= 40;

    // Wait
    Delay_ms(1000);
    }
    }
     
  14. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    I thought you were using MPLAB X?

    From the link above:
     
  15. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    I have taken from internet, that's re not my codes...
    i need for mplab x yes.
     
  16. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    What does your code look like?

    Do you have a schematic layout?
     
  17. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    I have no schematic first i am working on code because schematic is just power the sensor and connect the data to any port pin.
     
  18. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    It is better to do it the other way.

    First make the hardware, then make the software. Be sure to consult the datasheet when making the hardware.
     
  19. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    I belive that you are able to go through the code, line by line, and convert it to suit your setup. I belive it is MPLAB X, with XC8 compiler, and a PIC 16F877?

    With help from this link, I was able to make it happen on my 16F628.

    RH_Temp.JPG

    Upper LCD: ATMEL 168. I used the including libraries and one found online.
    Lower: PIC16F628. I used the link TS provided, and converted all the lines to match my setup. MPLAB X v3.05, XC8 v1.34, DHT11 and 2x16 5x7 LCD.

    Good luck.
     
    Last edited: Jul 18, 2015
    RRITESH KAKKAR likes this.
Loading...