+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Full Member level 5
    Points: 2,933, Level: 12
    Achievements:
    7 years registered

    Join Date
    May 2010
    Location
    usa
    Posts
    313
    Helped
    0 / 0
    Points
    2,933
    Level
    12

    dht22 read wrong values

    I used this code to read dht22 but it displays wrong value. in simulation, it displays 1.245 % , when i change the humidity it change the tenths numbers and the full scale reading doesn't pass 2%
    Code:
    /*****************************************************
    Chip type               : ATmega8
    Program type            : Application
    AVR Core Clock frequency: 8,000000 MHz
    Memory model            : Small
    External RAM size       : 0
    Data Stack size         : 256
    *****************************************************/
    #include <mega8.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <delay.h>
    #include <stdlib.h>
    
    #define DHT_PORT        PORTD
    #define DHT_DDR         DDRD
    #define DHT_PIN         PIND
    #define DHT11_PIN 1
    
    
    
    // Alphanumeric LCD Module functions
    #include <alcd.h>
    uint8_t c=0,I_RH,D_RH,I_Temp,D_Temp,CheckSum;
     int q;
    
     	char data[5];
    
    void Request()                /* Microcontroller send start pulse/request */
    {
        DDRD |= (1<<DHT11_PIN);
        PORTD &= ~(1<<DHT11_PIN);    /* set to low pin */
        delay_ms(20);            /* wait for 20ms */
    	PORTD |= (1<<DHT11_PIN);	/* set to high pin */
    }
    
    void Response()				/* receive response from DHT11 */
    {
    	DDRD &= ~(1<<DHT11_PIN);
    	while(PIND & (1<<DHT11_PIN));
    	while((PIND & (1<<DHT11_PIN))==0);
    	while(PIND & (1<<DHT11_PIN));
    }
    
    uint8_t Receive_data()			/* receive data */
    {	
    	for (q=0; q<8; q++)
    	{
    		while((PIND & (1<<DHT11_PIN)) == 0);  /* check received bit 0 or 1 */
    		delay_us(30);
    		if(PIND & (1<<DHT11_PIN))/* if high pulse is greater than 30ms */
    		c = (c<<1)|(0x01);	/* then its logic HIGH */
    		else			/* otherwise its logic LOW */
    		c = (c<<1);
    		while(PIND & (1<<DHT11_PIN));
    	}
    	return c;
    }
    
    void main(void)
    {
    PORTB=0x00;
    DDRB=0xFF;
    
    PORTC=0x00;
    DDRC=0x00;
    
    PORTD=0x00;
    DDRD=0xFF;
    
    lcd_init(16);
    
    while (1)
          {
           Request();		/* send start pulse */
    		Response();		/* receive response */
    		I_RH=Receive_data();	/* store first eight bit in I_RH */
    		D_RH=Receive_data();	/* store next eight bit in D_RH */
    		I_Temp=Receive_data();	/* store next eight bit in I_Temp */
    		D_Temp=Receive_data();	/* store next eight bit in D_Temp */
    		CheckSum=Receive_data();/* store next eight bit in CheckSum */
    		
    		if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
    		{
    			lcd_gotoxy(0,1);
    			lcd_putsf("Error");
    		}
    		
    		else
            {  
              
            
          		lcd_gotoxy(0,0);
    	        lcd_putsf("Humidity ="); 
                lcd_gotoxy(0,10);
    			itoa(I_RH,data);
    			lcd_puts(data);
    			lcd_putsf(".");
    			itoa(D_RH,data);
    			lcd_puts(data);
    			lcd_putsf("%");    
                  } 
                	delay_ms(2000);
    
                }
    }
    Last edited by FvM; 14th February 2019 at 00:42. Reason: Disabled html to show the code correctly

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 79,953, Level: 68
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,093
    Helped
    4377 / 4377
    Points
    79,953
    Level
    68

    Re: dht22 read wrong values

    I think we need to see a little more code to make a diagnosis!

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisement

        
       

  3. #3
    Full Member level 5
    Points: 2,933, Level: 12
    Achievements:
    7 years registered

    Join Date
    May 2010
    Location
    usa
    Posts
    313
    Helped
    0 / 0
    Points
    2,933
    Level
    12

    Re: dht22 read wrong values

    I pasted the full code but i don't know where is is the problem, I attached it please modify and post it.
    Last edited by mshh; 11th February 2019 at 11:43.



    •   AltAdvertisement

        
       

  4. #4
    Full Member level 5
    Points: 2,933, Level: 12
    Achievements:
    7 years registered

    Join Date
    May 2010
    Location
    usa
    Posts
    313
    Helped
    0 / 0
    Points
    2,933
    Level
    12

    Re: dht22 read wrong values

    this code works well for dht11 but for dht22 it shows checksum error in the range from 49-51%

    Code:
    /*****************************************************
    Chip type               : ATmega8
    Program type            : Application
    AVR Core Clock frequency: 8,000000 MHz
    Memory model            : Small
    External RAM size       : 0
    Data Stack size         : 256
    *****************************************************/
    
    #include <mega8.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <delay.h>
    #include <stdlib.h>
    
    #define DHT22       //define DHT22 or DHT11
    #define DHT_PORT        PORTD
    #define DHT_DDR         DDRD
    #define DHT_PIN         PIND
    #define DHT11_PIN 1
    
    
    
    // Alphanumeric LCD Module functions
    #include <alcd.h>
    uint8_t c=0,I_RH,D_RH,I_Temp,D_Temp,CheckSum;
     int q;
         float RH;
       unsigned char data [5];  
    
    void Request()                /* Microcontroller send start pulse/request */
    {
        DDRD |= (1<<DHT11_PIN);
        PORTD &= ~(1<<DHT11_PIN);    /* set to low pin */
        delay_ms(20);            /* wait for 20ms */
    	PORTD |= (1<<DHT11_PIN);	/* set to high pin */
    }
    
    void Response()				/* receive response from DHT11 */
    {
    	DDRD &= ~(1<<DHT11_PIN);
    	while(PIND & (1<<DHT11_PIN));
    	while((PIND & (1<<DHT11_PIN))==0);
    	while(PIND & (1<<DHT11_PIN));
    }
    
    uint8_t Receive_data()			/* receive data */
    {	
    	for (q=0; q<8; q++)
    	{
    		while((PIND & (1<<DHT11_PIN)) == 0);  /* check received bit 0 or 1 */
    		delay_us(30);
    		if(PIND & (1<<DHT11_PIN))/* if high pulse is greater than 30ms */
    		c = (c<<1)|(0x01);	/* then its logic HIGH */
    		else			/* otherwise its logic LOW */
    		c = (c<<1);
    		while(PIND & (1<<DHT11_PIN));
    	}
    	return c;
    }
    
    
    void main(void)
    {
    PORTB=0x00;
    DDRB=0xFF;
    
    PORTC=0x00;
    DDRC=0x00;
    
    PORTD=0x00;
    DDRD=0xFF;
    
    lcd_init(16);
    
    while (1)
          {
           Request();		/* send start pulse */
    		Response();		/* receive response */
    		I_RH=Receive_data();	/* store first eight bit in I_RH */
    		D_RH=Receive_data();	/* store next eight bit in D_RH */
    		I_Temp=Receive_data();	/* store next eight bit in I_Temp */
    		D_Temp=Receive_data();	/* store next eight bit in D_Temp */
    		CheckSum=Receive_data();/* store next eight bit in CheckSum */
    		
    		if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
    		{
    			lcd_gotoxy(0,1);
    			lcd_putsf("Error");
    		}
    		
    		else
            {  
              
            
          		lcd_gotoxy(0,0);
    	        lcd_putsf("Humidity ="); 
                lcd_gotoxy(0,10); 
        
               #ifdef DHT22
                RH= (float) I_RH * 256.0 + D_RH ;
                RH=RH/10.0;
                
                #else
                RH =  I_RH;
                 #endif 
                 sprintf(data,"%.1f%%  ",RH);
               lcd_puts(data);
       
       
                } 
                	delay_ms(2000);
    
                }
    }



--[[ ]]--