Adruino 101 DHT11 Works only for about an hour


i have arduino 101 connected dht11 , after has been running hour can read pin high.  trying debug i've used 2 boards removed dht11 reconnected it, used different dh11 , swapped between 5v , 3.3v in.  i'm @ loss , @ point kind of pointing issue arduino thought reach out here , see if there might missing might lead me closer figuring out going on.

dht.h
code: [select]
//
//    file: dht.h
//  author: rob tillaart
// version: 0.1.13
// purpose: dht temperature & humidity sensor library arduino
//     url: http://arduino.cc/playground/main/dhtlib
//
// history:
// see dht.cpp file
//

#ifndef dht_h
#define dht_h

#if arduino < 100
#include <wprogram.h>
#else
#include <arduino.h>
#endif

#define dht_lib_version "0.1.13"

#define dhtlib_ok                0
#define dhtlib_error_checksum   -1
#define dhtlib_error_timeout    -2
#define dhtlib_invalid_value    -999

#define dhtlib_dht11_wakeup     18
#define dhtlib_dht_wakeup       1

// max timeout 100usec.
// 16mhz proc max 1600 clock cycles
// loops using timeout use @ least 4 clock cycli
// 100 takes max 400 loops
// dividing f_cpu 40000 "fail" fast possible
#define dhtlib_timeout (f_cpu/40000)

class dht
{
public:
    // return values:
    // dhtlib_ok
    // dhtlib_error_checksum
    // dhtlib_error_timeout
    int read11(uint8_t pin);
    int read(uint8_t pin);

    inline int read21(uint8_t pin) { return read(pin); };
    inline int read22(uint8_t pin) { return read(pin); };
    inline int read33(uint8_t pin) { return read(pin); };
    inline int read44(uint8_t pin) { return read(pin); };

    double humidity;
    double temperature;

private:
    uint8_t bits[5];  // buffer receive data
    int _readsensor(uint8_t pin, uint8_t wakeupdelay);
};
#endif
//
// end of file
//

dht.cpp
code: [select]
//
//    file: dht.cpp
//  author: rob tillaart
// version: 0.1.13
// purpose: dht temperature & humidity sensor library arduino
//     url: http://arduino.cc/playground/main/dhtlib
//
// history:
// 0.1.13 fix negative temperature
// 0.1.12 support dht33 , dht44 initial version
// 0.1.11 renamed dhtlib_timeout
// 0.1.10 optimized faster wakeup + timeout
// 0.1.09 optimize size: timeout check + use of mask
// 0.1.08 added formula timeout based upon clockspeed
// 0.1.07 added support dht21
// 0.1.06 minimize footprint (2012-12-27)
// 0.1.05 fixed negative temperature bug (thanks roseman)
// 0.1.04 improved readability of code using dhtlib_ok in code
// 0.1.03 added error values temp , humidity when read failed
// 0.1.02 added error codes
// 0.1.01 added support arduino 1.0, fixed typos (31/12/2011)
// 0.1.0 rob tillaart (01/04/2011)
//
// inspired dht11 library
//
// released public domain
//

#include "dht.h"

/////////////////////////////////////////////////////
//
// public
//

// return values:
// dhtlib_ok
// dhtlib_error_checksum
// dhtlib_error_timeout
int dht::read11(uint8_t pin)
{
    // read values
    int rv = _readsensor(pin, dhtlib_dht11_wakeup);
    if (rv != dhtlib_ok)
    {
        humidity    = dhtlib_invalid_value; // invalid value, or nan prefered?
        temperature = dhtlib_invalid_value; // invalid value
        return rv;
    }

    // convert , store
    humidity    = bits[0];  // bits[1] == 0;
    temperature = bits[2];  // bits[3] == 0;

    // test checksum
    // bits[1] && bits[3] both 0
    uint8_t sum = bits[0] + bits[2];
    if (bits[4] != sum) {
    serial.print("0 : ");
serial.println(bits[0], dec);
    serial.print("1 : ");
    serial.println(bits[1], dec);
    serial.print("2 : ");
    serial.println(bits[2], dec);
    serial.print("3 : ");
    serial.println(bits[3], dec);
    serial.print("4 : ");
    serial.println(bits[4], dec);
    serial.print("sum:");
    serial.println(sum, dec);

    return dhtlib_error_checksum;
    }

    return dhtlib_ok;
}


// return values:
// dhtlib_ok
// dhtlib_error_checksum
// dhtlib_error_timeout
int dht::read(uint8_t pin)
{
    // read values
    int rv = _readsensor(pin, dhtlib_dht_wakeup);
    if (rv != dhtlib_ok)
    {
        humidity    = dhtlib_invalid_value;  // invalid value, or nan prefered?
        temperature = dhtlib_invalid_value;  // invalid value
        return rv; // propagate error value
    }

    // convert , store
    humidity = word(bits[0], bits[1]) * 0.1;
    temperature = word(bits[2] & 0x7f, bits[3]) * 0.1;
    if (bits[2] & 0x80)  // negative temperature
    {
        temperature = -temperature;
    }

    // test checksum
    uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
    serial.println(bits[0], dec);
    serial.println(bits[1], dec);
    serial.println(bits[2], dec);
    serial.println(bits[3], dec);
    serial.println(bits[4], dec);
    serial.println(sum, dec);
    if (bits[4] != sum)
    {
        return dhtlib_error_checksum;
    }
    return dhtlib_ok;
}

/////////////////////////////////////////////////////
//
// private
//

// return values:
// dhtlib_ok
// dhtlib_error_timeout
int dht::_readsensor(uint8_t pin, uint8_t wakeupdelay)
{
    // init buffervar receive data
    uint8_t mask = 128;
    uint8_t idx = 0;

    // empty buffer
    (uint8_t = 0; < 5; i++) bits[i] = 0;

    // request sample
    pinmode(pin, output);
    digitalwrite(pin, low);
    delay(wakeupdelay);
    digitalwrite(pin, high);
    delaymicroseconds(40);
    pinmode(pin, input);

    // acknowledge or timeout
    uint16_t loopcnt = dhtlib_timeout;
    while(digitalread(pin) == low)
    {
        if (--loopcnt == 0) return dhtlib_error_timeout;
    }

    loopcnt = dhtlib_timeout;
    while(digitalread(pin) == high)
    {
        if (--loopcnt == 0) return dhtlib_error_timeout;
    }

    // read output - 40 bits => 5 bytes
    (uint8_t = 40; != 0; i--)
    {
        loopcnt = dhtlib_timeout;
        while(digitalread(pin) == low)
        {
            if (--loopcnt == 0) return dhtlib_error_timeout;
        }

        uint32_t t = micros();

        loopcnt = dhtlib_timeout;
        while(digitalread(pin) == high)
        {
            if (--loopcnt == 0) return dhtlib_error_timeout;
        }

        if ((micros() - t) > 40)
        {
            bits[idx] |= mask;
        }
        mask >>= 1;
        if (mask == 0)   // next byte?
        {
            mask = 128;
            idx++;
        }
    }
    pinmode(pin, output);
    digitalwrite(pin, high);

    return dhtlib_ok;
}
//
// end of file
//

main file
code: [select]
//
//    file: dht11_test.ino
//  author: rob tillaart
// version: 0.1.00
// purpose: dht library test sketch dht11 && arduino
//     url:
//
// released public domain
//

#include <dht.h>

dht dht;

#define dht11_pin 4
#define humidifer_pin
unsigned long pass = 0;

void setup()
{
  pinmode(5, output);
  serial.begin(115200);
  serial.println("dht test program ");
  serial.print("library version: ");
  serial.println(dht_lib_version);
  serial.println();
  serial.println("type,\tstatus,\thumidity (%),\ttemperature (f)");
}

void loop()
{
  pinmode(4, output);
  delay(100);
  digitalwrite(4, low);
  delay(1000);
  digitalwrite(4, high);
  delay(1000);
  serial.print("pass:");
  serial.println(pass++, dec);
  serial.println("type,\tstatus,\thumidity (%),\ttemperature (f)");
  // read data
  serial.print("dht11, \t");
  int chk = dht.read11(dht11_pin);
  switch (chk)
  {
    case dhtlib_ok: 
serial.print("ok,\t");
break;
    case dhtlib_error_checksum:
serial.print("checksum error,\t");
break;
    case dhtlib_error_timeout:
serial.print("time out error,\t");
break;
    default:
serial.print("unknown error,\t");
break;
  }
  if(chk==0) {
    serial.print(dht.humidity, 1);
    serial.print(",\t");
    serial.println(dht.temperature * 9 / 5 + 32, 1);
    digitalwrite(5, (dht.humidity<33 ? high : low));
  }
  else
  {
    // turn off on errors
    digitalwrite(5, low);
  }
  delay(2000);
}
//
// end of file
//


Arduino Forum > Using Arduino > Microcontrollers > Adruino 101 DHT11 Works only for about an hour


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor