DHT 22 interfacing help

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

 

I've interfaced DHT22 humidity and temp. sensor with mega 16, and trying to get data at serial terminal using UART with 2400 baud rate. But, i'm continuously getting check sum error at serial terminal (as data's check sum is not happy then I've sending "checksum error message at uart" )

here is DHT.c and .h file from git hub.

before that  i'd also tried with goole's  DHT.c and DHT.h  but same thing is happening!

 

 

 

 


#include "DHT.h"

uint8_t DHT_Read_Data(uint8_t select)
{

    //data[5] is 8byte table where data come from DHT are stored
    //laststate holds laststate value
    //counter is used to count microSeconds
    uint8_t data[5], laststate = 0, counter = 0, j = 0, i = 0;
    
    //Clear array
    data[0] = data[1] = data[2] = data[3] = data[4] = 0;
    //4 byte of DHT data
  
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    //Prepare the bus(sending start signel from mcu)
    DDRC |= _BV(DHT_PIN);               //Set pin Output   PC.0(HIGH(O\P))
    DHT_PORT        |= _BV(DHT_PIN);    //Pin High
    _delay_ms(250);                     //Wait for 250mS
    
    //Send Request Signal(strt signal)
    DHT_PORT        &=~_BV(DHT_PIN);    //Pin Low
    _delay_ms(18);                      //18ms Low 
    
    DHT_PORT        |= _BV(DHT_PIN);    //Pin High
    _delay_us(40);                      //40us High
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////   
 
 
    //Set pin Input to read Bus(read ack from sensor//responce signal)
    DDRC   &=~_BV(DHT_PIN);    //Set pin Input    PC.0 (LOW(I\P))

    laststate=DHT_Read_Pin;             //Read Pin value(PINC &(1<<0))   PC.0
    
    //Repeat for each Transistions
	
    for (i=0; i<MAXTIMINGS; i++)          //maxtiming=84
	{
        //While state is the same count microseconds
        while (laststate==DHT_Read_Pin)
	    {
            _delay_us(1);
            counter++;
        }
        
        //laststate==_BV(DHT_PIN) checks if laststate was High
        //ignore the first 2 transitions which are the DHT Response
        if (laststate==_BV(DHT_PIN) && (i > 2))
		 {
            //Save bits in segments of bytes
            //Shift data[] value 1 position left
            //Example. 01010100 if we shift it left one time it will be
            //10101000
            data[j/8]<<=1;
            if (counter >= 40) {    //If it was high for more than 40uS
                data[j/8]|=1;       //it means it is bit '1' so make a logic
            }                       //OR with the value (save it)
            j++;                    //making an OR by 1 to this value 10101000
        }                           //we will have the resault 10101001
                                    //1 in 8-bit binary is 00000001
        //j/8 changes table record every 8 bits which means a byte has been saved
        //so change to next record. 0/8=0 1/8=0 ... 7/8=0 8/8=1 ... 15/8=1 16/8=2
        laststate=DHT_Read_Pin;     //save current state
        counter=0;                  //reset counter
        
    }
    
    
    //Check if DHT-11 is connected
    if (data[0]==0 && data[1]==0 && data[2]==0 && data[3]==0)
	 {
        UART_puts("\r\nDHT not pluged!");
        return(-1);
    }
	
    //Check if data received are correct by checking the CheckSum
    if (data[0] + data[1] + data[2] + data[3] == data[4])
	 {
        if (select==DHT_Temp)
		 {     //Return the value has been choosen
            return(data[2]);
        }
		else if(select==DHT_RH)
		{
            return(data[0]);
        }
    }
	else
	{
        UART_puts("\r\nCheck Sum Error");
    }
 
    
}





//******************************dht.h***************************************




#include<stdlib.h>
#include<avr/io.h>
#include<util/delay.h>

//DHT Pin and Port
#define DHT_PORT    PORTC
#define DHT_PIN     0

//The packet size is 40bit but each bit consists of low and high state(8x5   5byte of 8 bit)
//so 40 x 2 = 80 transitions. Also we have 2 transistions DHT response(ack form low to high of 80us)
//and 2 transitions which indicates End Of Frame. In total 84
#define MAXTIMINGS  84

//Select between Temp and Humidity Read
#define DHT_Temp    0
#define DHT_RH      1

#define DHT_Read_Pin    (PINC & _BV(DHT_PIN))      //(PINC &(1<<0))

//This is the main function which requests and reads the packet
uint8_t DHT_Read_Data(uint8_t select);

 

 

 

//************************main.c**********************************************
int main(void)
{
    MCUCSR|=(1<<JTD);             //DISABLE JTAG AT PORT C
    MCUCSR|=(1<<JTD);
    
    char buffer_t[10];
    char buffer_h[10];
    uint16_t t,h;
    UART_init();                 //init uart functiom 16mhz //2400 baud rate
    UART_puts("\r\n");
    UART_puts("DHT 22 test");
    UART_puts("\r\n");
    _delay_ms(1000);            //1 sec delay
    
    while(1)
    {
        UART_puts(" Temperature:");        //DISPLAY TEMP 
        t=DHT_Read_Data(DHT_Temp);
        itoa(t,buffer_t,10);
        UART_puts(buffer_t);
        
        UART_puts("\r\n");
        
        UART_puts(" Humidity:");          //DISPLY HUMIDITY
        h=DHT_Read_Data(DHT_RH);
        itoa(h,buffer_h,10);
        UART_puts(buffer_h);
        UART_puts("\r\n");
        _delay_ms(250);
        UART_puts("\r\n");
    }
    
     
}

 

This topic has a solution.

mahendra_ms

Last Edited: Fri. Jan 16, 2015 - 06:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

. What speed is your AVR actually running at and have you told the compiler? Otherwise the code wont work.

Last Edited: Fri. Jan 16, 2015 - 07:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've defined F_CPU 16000000ul and i'm sending data with 2400 baud rate

mahendra_ms

Last Edited: Fri. Jan 16, 2015 - 07:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Measure the data coming out of the dht22. It may not be quite to spec.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've defined F_CPU 16000000ul

Yes BUT

What speed is your AVR actually running at

Simply because you define the frequency it means nothing. Do you have a properly wired 16MHz crystal and have you set the fuses to use it?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes i wired 16 mhz crystal and my fuse bits are LOW=0xEF and HIGH =0x99

mahendra_ms

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Have you run a simple blink-LED program to confirm that your m16 is running at the correct speed?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, now i'm approaching to fuse bit of AVR. As i told earlier my fuse bits are 0xEF and 0x99 at this fuse bit i'd made code of led blink with 1sec delay, but i'm not able to got exact 1sec blink at led(as it was blinking little bit fast )

 

 

#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000ul

void main(void)
{
    DDRB=0XFF;
    PORTB=0XFF;
    while(1)
    {
        PORTB=~PORTB;
        _delay_ms(1000);
    }
}

 

 

it may problem at fuse bit!

any suggestion guys! 

 

mahendra_ms

Last Edited: Sat. Jan 17, 2015 - 07:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
as it was blinking little bit fast
How fast?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

that was the problem of accurate timing, solved using timer 0 and 8mhz internal RC oscillator 

Attachment(s): 

mahendra_ms