DHT 22 sensor not responding to AT mega16 on UART

  1. Mahendra_mrs
    Mahendra_mrs
    hello freaks,
    I'm newbie at this forum and this on one my first post, from couple of days I'm dealing with DHT 22 sensor to interfacing with avr, my application is to measure humidity and temperature from DHT 22 and send those data to PC using UART.
    Before that I've studied DHT 22 sensor datasheet and it's one wire interface and got some idea from internet to interface DHT 22 and developed code in c
    but the problem is sensor not responding to avr and i just getting error msg on serial terminal(as if checksum will not happy, then it will display 'error' msg at serial terminal )


    here is my complete code

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

    #ifndef DHT_H_
    #define DHT_H_

    #include<stdio.h>
    #include<avr/io.h>

    //setup port
    #define DHT_DDR DDRD
    #define DHT_PORT PORTD
    #define DHT_PIN PIND
    #define DHT_INPUTPIN PD6

    //sensor type
    #define DHT_DHT11 1
    #define DHT_DHT22 2
    #define DHT_TYPE DHT_DHT22

    //enable decimal precision (float)
    #if DHT_TYPE == DHT_DHT11
    #define DHT_FLOAT 0
    #elif DHT_TYPE == DHT_DHT22
    #define DHT_FLOAT 1
    #endif

    //timeout retries
    #define DHT_TIMEOUT 200

    //functions
    #if DHT_FLOAT == 1
    extern int8_t dht_gettemperature(float *temperature);
    extern int8_t dht_gethumidity(float *humidity);
    extern int8_t dht_gettemperaturehumidity(float *temperature, float *humidity);
    #elif DHT_FLOAT == 0
    extern int8_t dht_gettemperature(int8_t *temperature);
    extern int8_t dht_gethumidity(int8_t *humidity);
    extern int8_t dht_gettemperaturehumidity(int8_t *temperature, int8_t *humidity);
    #endif

    #endif



    //*****************************************main.c*** ************************************

    #include<stdio.h>
    #include<stdlib.h>
    #include<avr/io.h>
    #include<avr/interrupt.h>
    #include<util/delay.h>
    #define F_CPU 16000000ul
    #include "dht.h"

    void UART_init();
    void uart_puts(char*str);
    void uart_putch(unsigned char data);


    int main(void)
    {
    char printbuff[100];


    UART_init();

    #if DHT_FLOAT == 0
    int8_t temperature = 0;
    int8_t humidity = 0;
    #elif DHT_FLOAT == 1
    float temperature = 0;
    float humidity = 0;
    #endif


    uart_puts("DHT _22 test \r\n");
    _delay_ms(2000);

    for (;;) {
    if(dht_gettemperaturehumidity(&temperature, &humidity) != -1) {

    #if DHT_FLOAT == 0
    itoa(temperature, printbuff, 10);
    #elif DHT_FLOAT == 1
    dtostrf(temperature, 3, 3, printbuff);
    #endif
    uart_puts("temperature: "); uart_puts(printbuff); uart_puts("C");uart_puts("\r\n");
    #if DHT_FLOAT == 0
    itoa(humidity, printbuff, 10);
    #elif DHT_FLOAT == 1
    dtostrf(humidity, 3, 3, printbuff);
    #endif
    uart_puts("humidity: "); uart_puts(printbuff); uart_puts("%RH");uart_puts("\r\n");

    } else {
    uart_puts("error"); uart_puts("\r\n");
    }

    uart_puts("\r\n");

    _delay_ms(1500);
    }

    return 0;
    }


    void UART_init()
    {
    UCSRB= (1<<RXEN)|(1<<TXEN); // Enable Receiver and Transmitter
    UCSRC= 0x86; // Asynchronous mode 8-bit data and 1-stop bit
    UCSRA= 0x00; // Normal Baud rate(no doubling), Single processor commn
    UBRRH= 0x19;
    UBRRL= 0X0F; // 2400 Baud rate at 16 MhZMHz
    }

    void uart_putch(unsigned char data)
    {
    while((UCSRA &(1<<UDRE))==0);
    UDR=data;

    }

    void uart_puts(char *str)
    {
    while(*str!='\0')
    {
    uart_putch(*str);
    str++;
    }
    }

    //***********************************DHT.c********** *****************

    /*
    DHT Library 0x03

    copyright (c) Davide Gironi, 2012

    Released under GPLv3.
    Please refer to LICENSE file for licensing information.
    */


    #include <stdio.h>
    #include <string.h>
    #include <avr/io.h>
    #include <util/delay.h>

    #include "dht.h"

    /*
    * get data from sensor
    */
    #if DHT_FLOAT == 1
    int8_t dht_getdata(float *temperature, float *humidity) {
    #elif DHT_FLOAT == 0
    int8_t dht_getdata(int8_t *temperature, int8_t *humidity) {
    #endif
    uint8_t bits[5];
    uint8_t i,j = 0;

    memset(bits, 0, sizeof(bits));

    //reset port
    DHT_DDR |= (1<<DHT_INPUTPIN); //output
    DHT_PORT |= (1<<DHT_INPUTPIN); //high
    _delay_ms(100);

    //send request
    DHT_PORT &= ~(1<<DHT_INPUTPIN); //low
    #if DHT_TYPE == DHT_DHT11
    _delay_ms(18);
    #elif DHT_TYPE == DHT_DHT22
    _delay_us(500);
    #endif
    DHT_PORT |= (1<<DHT_INPUTPIN); //high
    DHT_DDR &= ~(1<<DHT_INPUTPIN); //input
    _delay_us(40);

    //check start condition 1
    if((DHT_PIN & (1<<DHT_INPUTPIN))) {
    return -1;
    }
    _delay_us(80);
    //check start condition 2
    if(!(DHT_PIN & (1<<DHT_INPUTPIN))) {
    return -1;
    }
    _delay_us(80);

    //read the data
    uint16_t timeoutcounter = 0;
    for (j=0; j<5; j++) { //read 5 byte
    uint8_t result=0;
    for(i=0; i<8; i++) {//read every bit
    timeoutcounter = 0;
    while(!(DHT_PIN & (1<<DHT_INPUTPIN))) { //wait for an high input (non blocking)
    timeoutcounter++;
    if(timeoutcounter > DHT_TIMEOUT) {
    return -1; //timeout
    }
    }
    _delay_us(30);
    if(DHT_PIN & (1<<DHT_INPUTPIN)) //if input is high after 30 us, get result
    result |= (1<<(7-i));
    timeoutcounter = 0;
    while(DHT_PIN & (1<<DHT_INPUTPIN)) { //wait until input get low (non blocking)
    timeoutcounter++;
    if(timeoutcounter > DHT_TIMEOUT) {
    return -1; //timeout
    }
    }
    }
    bits[j] = result;
    }

    //reset port
    DHT_DDR |= (1<<DHT_INPUTPIN); //output
    DHT_PORT |= (1<<DHT_INPUTPIN); //low
    _delay_ms(100);

    //check checksum
    if ((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) == bits[4]) {
    //return temperature and humidity
    #if DHT_TYPE == DHT_DHT11
    *temperature = bits[2];
    *humidity = bits[0];
    #elif DHT_TYPE == DHT_DHT22
    uint16_t rawhumidity = bits[0]<<8 | bits[1];
    uint16_t rawtemperature = bits[2]<<8 | bits[3];
    if(rawtemperature & 0x8000) {
    *temperature = (float)((rawtemperature & 0x7FFF) / 10.0) * -1.0;
    } else {
    *temperature = (float)(rawtemperature)/10.0;
    }
    *humidity = (float)(rawhumidity)/10.0;
    #endif
    return 0;
    }

    return -1;
    }

    /*
    * get temperature
    */
    #if DHT_FLOAT == 1
    int8_t dht_gettemperature(float *temperature) {
    float humidity = 0;
    #elif DHT_FLOAT == 0
    int8_t dht_gettemperature(int8_t *temperature) {
    int8_t humidity = 0;
    #endif
    return dht_getdata(temperature, &humidity);
    }

    /*
    * get humidity
    */
    #if DHT_FLOAT == 1
    int8_t dht_gethumidity(float *humidity) {
    float temperature = 0;
    #elif DHT_FLOAT == 0
    int8_t dht_gethumidity(int8_t *humidity) {
    int8_t temperature = 0;
    #endif
    return dht_getdata(&temperature, humidity);
    }

    /*
    * get temperature and humidity
    */
    #if DHT_FLOAT == 1
    int8_t dht_gettemperaturehumidity(float *temperature, float *humidity) {
    #elif DHT_FLOAT == 0
    int8_t dht_gettemperaturehumidity(int8_t *temperature, int8_t *humidity) {
    #endif
    return dht_getdata(temperature, humidity);
    }


    //************************************************** ****************************************

    can anyone help me to solve this trouble?
Results 1 to 1 of 1