Commit 2c2e16d8 authored by João Lino's avatar João Lino

DHT library

parent deeddd43
//
// FILE: dht.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.28
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// 0.1.28 2018-04-03 refactor
// 0.1.27 2018-03-26 added _disableIRQ flag
// 0.1.26 2017-12-12 explicit support for AM23XX series and DHT12
// 0.1.25 2017-09-20 FIX https://github.com/RobTillaart/Arduino/issues/80
// 0.1.24 2017-07-27 FIX https://github.com/RobTillaart/Arduino/issues/33 double -> float
// 0.1.23 2017-07-24 FIX https://github.com/RobTillaart/Arduino/issues/31
// 0.1.22 undo delayMicroseconds() for wakeups larger than 8
// 0.1.21 replace delay with delayMicroseconds() + small fix
// 0.1.20 Reduce footprint by using uint8_t as error codes. (thanks to chaveiro)
// 0.1.19 masking error for DHT11 - FIXED (thanks Richard for noticing)
// 0.1.18 version 1.16/17 broke the DHT11 - FIXED
// 0.1.17 replaced micros() with adaptive loopcount
// removed DHTLIB_INVALID_VALUE
// added DHTLIB_ERROR_CONNECT
// added DHTLIB_ERROR_ACK_L DHTLIB_ERROR_ACK_H
// 0.1.16 masking unused bits (less errors); refactored bits[]
// 0.1.15 reduced # micros calls 2->1 in inner loop.
// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines.
//
// 0.1.13 fix negative temperature
// 0.1.12 support DHT33 and 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 for timeout based upon clockspeed
// 0.1.07 added support for DHT21
// 0.1.06 minimize footprint (2012-12-27)
// 0.1.05 fixed negative temperature bug (thanks to Roseman)
// 0.1.04 improved readability of code using DHTLIB_OK in code
// 0.1.03 added error values for temp and humidity when read failed
// 0.1.02 added error codes
// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
// 0.1.00 by Rob Tillaart (01/04/2011)
//
// inspired by DHT11 library
//
// Released to the public domain
//
#include "dht.h"
/////////////////////////////////////////////////////
//
// PUBLIC
//
int8_t dht::read11(uint8_t pin)
{
// READ VALUES
if (_disableIRQ) noInterrupts();
int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
if (_disableIRQ) interrupts();
// these bits are always zero, masking them reduces errors.
bits[0] &= 0x7F;
bits[2] &= 0x7F;
// CONVERT AND STORE
humidity = bits[0]; // bits[1] == 0;
temperature = bits[2]; // bits[3] == 0;
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return result;
}
int8_t dht::read12(uint8_t pin)
{
// READ VALUES
if (_disableIRQ) noInterrupts();
int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
if (_disableIRQ) interrupts();
// CONVERT AND STORE
humidity = bits[0] + bits[1] * 0.1;
temperature = (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];
if (bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return result;
}
int8_t dht::read(uint8_t pin)
{
// READ VALUES
if (_disableIRQ) noInterrupts();
int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);
if (_disableIRQ) interrupts();
// these bits are always zero, masking them reduces errors.
bits[0] &= 0x03;
bits[2] &= 0x83;
// CONVERT AND STORE
humidity = (bits[0]*256 + bits[1]) * 0.1;
temperature = ((bits[2] & 0x7F)*256 + 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];
if (bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return result;
}
/////////////////////////////////////////////////////
//
// PRIVATE
//
int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits)
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
uint8_t data = 0;
uint8_t state = LOW;
uint8_t pstate = LOW;
uint16_t zeroLoop = DHTLIB_TIMEOUT;
uint16_t delta = 0;
leadingZeroBits = 40 - leadingZeroBits; // reverse counting...
// replace digitalRead() with Direct Port Reads.
// reduces footprint ~100 bytes => portability issue?
// direct port read is about 3x faster
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *PIR = portInputRegister(port);
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); // T-be
if (wakeupDelay > 8) delay(wakeupDelay);
else delayMicroseconds(wakeupDelay * 1000UL);
// digitalWrite(pin, HIGH); // T-go
pinMode(pin, INPUT);
uint16_t loopCount = DHTLIB_TIMEOUT * 2; // 200uSec max
// while(digitalRead(pin) == HIGH)
while ((*PIR & bit) != LOW )
{
if (--loopCount == 0)
{
return DHTLIB_ERROR_CONNECT;
}
}
// GET ACKNOWLEDGE or TIMEOUT
loopCount = DHTLIB_TIMEOUT;
// while(digitalRead(pin) == LOW)
while ((*PIR & bit) == LOW ) // T-rel
{
if (--loopCount == 0)
{
return DHTLIB_ERROR_ACK_L;
}
}
loopCount = DHTLIB_TIMEOUT;
// while(digitalRead(pin) == HIGH)
while ((*PIR & bit) != LOW ) // T-reh
{
if (--loopCount == 0)
{
return DHTLIB_ERROR_ACK_H;
}
}
loopCount = DHTLIB_TIMEOUT;
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (uint8_t i = 40; i != 0; )
{
// WAIT FOR FALLING EDGE
state = (*PIR & bit);
if (state == LOW && pstate != LOW)
{
if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !! DHT11 only 1
{
zeroLoop = min(zeroLoop, loopCount);
delta = (DHTLIB_TIMEOUT - zeroLoop)/4;
}
else if ( loopCount <= (zeroLoop - delta) ) // long -> one
{
data |= mask;
}
mask >>= 1;
if (mask == 0) // next byte
{
mask = 128;
bits[idx] = data;
idx++;
data = 0;
}
// next bit
--i;
// reset timeout flag
loopCount = DHTLIB_TIMEOUT;
}
pstate = state;
// Check timeout
if (--loopCount == 0)
{
return DHTLIB_ERROR_TIMEOUT;
}
}
// pinMode(pin, OUTPUT);
// digitalWrite(pin, HIGH);
return DHTLIB_OK;
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.28
// PURPOSE: DHT Temperature & Humidity Sensor library for 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>
#include <pins_arduino.h> // fix for broken pre 1.0 version - TODO TEST
#else
#include <Arduino.h>
#endif
#define DHT_LIB_VERSION "0.1.28"
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2
#define DHTLIB_ERROR_CONNECT -3
#define DHTLIB_ERROR_ACK_L -4
#define DHTLIB_ERROR_ACK_H -5
#define DHTLIB_DHT11_WAKEUP 18
#define DHTLIB_DHT_WAKEUP 1
#define DHTLIB_DHT11_LEADING_ZEROS 1
#define DHTLIB_DHT_LEADING_ZEROS 6
// max timeout is 100 usec.
// For a 16 Mhz proc 100 usec is 1600 clock cycles
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
#ifndef F_CPU
#define DHTLIB_TIMEOUT 1000 // ahould be approx. clock/40000
#else
#define DHTLIB_TIMEOUT (F_CPU/40000)
#endif
class dht
{
public:
dht() { _disableIRQ = false; };
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
// DHTLIB_ERROR_CONNECT
// DHTLIB_ERROR_ACK_L
// DHTLIB_ERROR_ACK_H
int8_t read11(uint8_t pin);
int8_t read(uint8_t pin);
int8_t read12(uint8_t pin);
inline int8_t read21(uint8_t pin) { return read(pin); };
inline int8_t read22(uint8_t pin) { return read(pin); };
inline int8_t read33(uint8_t pin) { return read(pin); };
inline int8_t read44(uint8_t pin) { return read(pin); };
inline int8_t read2301(uint8_t pin) { return read(pin); };
inline int8_t read2302(uint8_t pin) { return read(pin); };
inline int8_t read2303(uint8_t pin) { return read(pin); };
inline int8_t read2320(uint8_t pin) { return read(pin); };
inline int8_t read2322(uint8_t pin) { return read(pin); };
bool getDisableIRQ() { return _disableIRQ; };
void setDisableIRQ(bool b ) { _disableIRQ = b; };
float humidity;
float temperature;
private:
uint8_t bits[5]; // buffer to receive data
int8_t _readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits);
bool _disableIRQ;
};
#endif
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht11_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
// URL:
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT11_PIN 5
void setup()
{
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 (C)");
}
void loop()
{
// 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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(2000);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht12_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: DHT library test sketch for DHT12 && Arduino
// URL:
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT12_PIN 5
void setup()
{
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 (C)");
}
void loop()
{
// READ DATA
Serial.print("DHT12, \t");
int chk = DHT.read12(DHT12_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;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(2000);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht21_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// PURPOSE: DHT library test sketch for DHT21 && Arduino
// URL:
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT21_PIN 5
void setup()
{
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 (C)");
}
void loop()
{
// READ DATA
Serial.print("DHT21, \t");
int chk = DHT.read21(DHT21_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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(2000);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht22_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.03
// PURPOSE: DHT library test sketch for DHT22 && Arduino
// URL:
// HISTORY:
// 0.1.03 extended stats for all errors
// 0.1.02 added counters for error-regression testing.
// 0.1.01
// 0.1.00 initial version
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT22_PIN 5
struct
{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
} stat = { 0,0,0,0,0,0,0,0};
void setup()
{
Serial.begin(115200);
Serial.println("dht22_test.ino");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}
void loop()
{
// READ DATA
Serial.print("DHT22, \t");
uint32_t start = micros();
int chk = DHT.read22(DHT22_PIN);
uint32_t stop = micros();
stat.total++;
switch (chk)
{
case DHTLIB_OK:
stat.ok++;
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
stat.crc_error++;
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
stat.time_out++;
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
stat.connect++;
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
stat.ack_l++;
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
stat.ack_h++;
Serial.print("Ack High error,\t");
break;
default:
stat.unknown++;
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
if (stat.total % 20 == 0)
{
Serial.println("\nTOT\tOK\tCRC\tTO\tCON\tACK_L\tACK_H\tUNK");
Serial.print(stat.total);
Serial.print("\t");
Serial.print(stat.ok);
Serial.print("\t");
Serial.print(stat.crc_error);
Serial.print("\t");
Serial.print(stat.time_out);
Serial.print("\t");
Serial.print(stat.connect);
Serial.print("\t");
Serial.print(stat.ack_l);
Serial.print("\t");
Serial.print(stat.ack_h);
Serial.print("\t");
Serial.print(stat.unknown);
Serial.println("\n");
}
delay(2000);
}
//
// END OF FILE
//
//
// FILE: dht22_test_noIRQ.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: DHT library test sketch for DHT22 && Arduino
// URL:
// HISTORY:
// 0.1.0 initial version
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT22_PIN 5
struct
{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
uint32_t sumtime;
uint32_t mintime;
} stat = { 0,0,0,0,0,0,0,0, 0, 9999};
void setup()
{
Serial.begin(115200);
Serial.println("dht22_test.ino");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.print("IRQ:\t");
Serial.println( DHT.getDisableIRQ() );
DHT.setDisableIRQ(true);
Serial.print("IRQ:\t");
Serial.println( DHT.getDisableIRQ() );
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}
void loop()
{
// READ DATA
Serial.print("DHT22, \t");
uint32_t start = micros();
int chk = DHT.read22(DHT22_PIN);
uint32_t stop = micros();
stat.total++;
switch (chk)
{
case DHTLIB_OK:
stat.ok++;
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
stat.crc_error++;
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
stat.time_out++;
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
stat.connect++;
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
stat.ack_l++;
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
stat.ack_h++;
Serial.print("Ack High error,\t");
break;
default:
stat.unknown++;
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
uint32_t duration = stop - start;
stat.sumtime += duration;
stat.mintime = min(stat.mintime, duration);
Serial.print(duration);
Serial.println();
if (stat.total % 20 == 0)
{
Serial.println("\nTOT\tOK\tCRC\tTIMO\tCON\tACKL\tACKH\tAVG\MIN");
Serial.print(stat.total);
Serial.print("\t");
Serial.print(stat.ok);
Serial.print("\t");
Serial.print(stat.crc_error);
Serial.print("\t");
Serial.print(stat.time_out);
Serial.print("\t");
Serial.print(stat.connect);
Serial.print("\t");
Serial.print(stat.ack_l);
Serial.print("\t");
Serial.print(stat.ack_h);
Serial.print("\t");
Serial.print(stat.unknown);
Serial.print("\t");
Serial.print(1.0 * stat.sumtime / stat.total);
Serial.print("\t");
Serial.print(1.0 * stat.mintime);
Serial.println("\n");
}
delay(500);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht33_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT33 && Arduino
// URL:
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT33_PIN 5
void setup()
{
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 (C)\tTime (us)");
}
void loop()
{
// READ DATA
Serial.print("DHT33, \t");
uint32_t start = micros();
int chk = DHT.read33(DHT33_PIN);
uint32_t stop = micros();
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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
// FOR UNO + DHT33 400ms SEEMS TO BE MINIMUM DELAY BETWEEN SENSOR READS,
// ADJUST TO YOUR NEEDS
delay(1000);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht44_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT44 && Arduino
// URL:
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT44_PIN 5
void setup()
{
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 (C)\tTime (us)");
}
void loop()
{
// READ DATA
Serial.print("DHT44, \t");
uint32_t start = micros();
int chk = DHT.read44(DHT44_PIN);
uint32_t stop = micros();
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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
// FOR UNO + DHT44 500ms SEEMS TO BE MINIMUM DELAY BETWEEN SENSOR READS,
// ADJUST TO YOUR NEEDS
delay(1000);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.08
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT11_PIN 4
#define DHT21_PIN 5
#define DHT22_PIN 6
void setup()
{
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 (C)");
}
void loop()
{
// READ DATA
Serial.print("DHT22, \t");
int chk = DHT.read22(DHT22_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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(1000);
// READ DATA
Serial.print("DHT21, \t");
chk = DHT.read21(DHT21_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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(1000);
// READ DATA
Serial.print("DHT11, \t");
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;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity,1);
Serial.print(",\t");
Serial.println(DHT.temperature,1);
delay(1000);
}
//
// END OF FILE
//
\ No newline at end of file
//
// FILE: dht_tuning.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: DHT test sketch for DHT22 && Arduino => find minimum time between reads
// URL:
//
// Released to the public domain
//
#include <dht.h>
dht DHT;
#define DHT22_PIN 2
void setup()
{
Serial.begin(115200);
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\t\tHumidity (%),\tTemperatur (C),\tT_Read (us)\tT_Cycle (us)\tT_Delay (us)");
}
int del = 500;
uint32_t startRead = 0;
uint32_t stopRead = 0;
uint32_t startCycle = 0;
uint32_t stopCycle = 0;
uint32_t tempCycle = 0;
void loop()
{
while(del > 0){
// READ DATA
Serial.print("DHT22, \t");
startRead = micros();
int chk = DHT.read22(DHT22_PIN);
stopRead = micros();
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t\t");
del -= 10;
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out,\t");
del += 10;
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High,\t");
break;
default:
Serial.print("Unknown,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t\t");
Serial.print(stopRead - startRead);
Serial.print(",\t\t");
stopCycle = micros();
tempCycle = micros();
Serial.print(stopCycle - startCycle);
startCycle = tempCycle;
Serial.print(",\t\t");
Serial.print(del);
Serial.print("000");
Serial.println();
delay(del);
}
while(1);
}
//
// END OF FILE
//
#######################################
# Syntax Coloring Map For DHT
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
DHT KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
read KEYWORD2
read11 KEYWORD2
read12 KEYWORD2
read21 KEYWORD2
read22 KEYWORD2
read33 KEYWORD2
read44 KEYWORD2
read2301 KEYWORD2
read2302 KEYWORD2
read2303 KEYWORD2
read2320 KEYWORD2
read2322 KEYWORD2
humidity KEYWORD2
temperature KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################
{
"name": "DHTlib",
"keywords": "DHT11 DHT22 DHT33 DHT44 AM2301 AM2302 AM2303 AM2320 AM2322",
"description": "Optimized Library for DHT Temperature & Humidity Sensor on AVR only.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"version":"0.1.28",
"frameworks": "arduino",
"platforms": "*",
"export": {
"include": "libraries/DHTlib"
}
}
name=DHTlib
version=0.1.28
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Optimized Library for DHT Temperature & Humidity Sensor on AVR only.
paragraph=
category=Sensors
url=https://github.com/RobTillaart/Arduino/tree/master/libraries
architectures=avr
\ No newline at end of file
# DHT Library
## Description
The DHT11, 21, 22, 33 and 44 are relative inexpensive sensors for measuring temperature and humidity.
This library can be used for reading both values from these DHT sensors.
The DHT11 only returns integers (e.g. 20) and does not support negative values.
The others are quite similar and provide one decimal digit (e.g. 20.2)
The hardware pins of the sensors and handshake are identical so ideal to combine in one lib.
The library (0.1.13 version) is confirmed to work on:
*UNO (tested myself)
*2009 (tested myself)
*MEGA2560
*DUE
*attiny85 @8MHz
*Digistump Digix @ 84 MHz
Later 0.1.xx versions are optimized for AVR.
More information - http://playground.arduino.cc//Main/DHTLib -
## Operational
TODO pin description
## Versions:
==> See DHTStable library for latest stable version for all platforms.
* version 0.1.13 is the last stable version for both AVR and ARM
* version 0.1.14 and up are not compatible anymore with pre 1.0 Arduino
* version 0.1.14 and up have breaking changes wrt ARM based arduino's e.g DUE.
* version 0.1.15 is stable version for AVR only
* version 0.1.16 and 0.1.17 have breaking changes for DHT11
* version 0.1.18 works again for DHT11 (avr only)
* version 0.1.19 fixed masking bug DHT11 (avr only)
* version 0.1.20 Reduce footprint (34 bytes) by using int8_t as error codes. (thanks to chaveiro)
* version 0.1.21 replace delay with delayMicroseconds() + small fix (thanks to codingforfun)
* version 0.1.22 undo delayMicroseconds() for wakeups larger than 8
* version 0.1.23 FIX https://github.com/RobTillaart/Arduino/issues/31
* version 0.1.24 FIX https://github.com/RobTillaart/Arduino/issues/33
* version 0.1.25 FIX https://github.com/RobTillaart/Arduino/issues/80
* version 0.1.26 Explicit support for AM23XX series and DHT12
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment