dht22 read wrong values

Status
Not open for further replies.

mshh

Full Member level 6
Joined
May 30, 2010
Messages
349
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
usa
Activity points
3,871
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 a moderator:

I pasted the full code but i don't know where is is the problem, I attached it please modify and post it.
 

Attachments

  • code.txt
    2.5 KB · Views: 41
Last edited:

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);

            }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…