mirror of
https://github.com/adafruit/DHT-sensor-library.git
synced 2023-10-23 22:20:38 +03:00
Compare commits
44 Commits
Author | SHA1 | Date | |
---|---|---|---|
f76a51243f | |||
8e0a94fff3 | |||
6332774b18 | |||
f566d58f47 | |||
9bf12551d8 | |||
7cec0cb06d | |||
1e752b12ee | |||
8b9624479e | |||
57fe95c1be | |||
d6488c1fbe | |||
d26e8ec729 | |||
b781f5998d | |||
01490aa8de | |||
261e997a90 | |||
bdd2756ed1 | |||
07da1cd001 | |||
b3b0b08d6d | |||
be8859e28c | |||
bf86a4068c | |||
f8d3497fac | |||
4bc4ec3fc4 | |||
efcca31a7f | |||
fa129747e8 | |||
dff65098bb | |||
73409c143c | |||
5550a040c2 | |||
dc71b006b6 | |||
562a4878a9 | |||
b429942ce8 | |||
7787bdd186 | |||
2dfa77993d | |||
f771939337 | |||
6b8b1ccdb9 | |||
a1169d20b4 | |||
3bba771f8c | |||
5211b2d701 | |||
a607a50475 | |||
b07db5af21 | |||
761f4f4fb9 | |||
16e61f1084 | |||
519393f42e | |||
cad6977ba8 | |||
53375d8d70 | |||
89937d5d25 |
114
DHT.cpp
114
DHT.cpp
@ -7,6 +7,7 @@ written by Adafruit Industries
|
|||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
|
|
||||||
#define MIN_INTERVAL 2000
|
#define MIN_INTERVAL 2000
|
||||||
|
#define TIMEOUT -1
|
||||||
|
|
||||||
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
|
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
|
||||||
_pin = pin;
|
_pin = pin;
|
||||||
@ -18,17 +19,20 @@ DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
|
|||||||
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
|
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
|
||||||
// reading pulses from DHT sensor.
|
// reading pulses from DHT sensor.
|
||||||
// Note that count is now ignored as the DHT reading algorithm adjusts itself
|
// Note that count is now ignored as the DHT reading algorithm adjusts itself
|
||||||
// basd on the speed of the processor.
|
// based on the speed of the processor.
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHT::begin(void) {
|
// Optionally pass pull-up time (in microseconds) before DHT reading starts.
|
||||||
|
// Default is 55 (see function declaration in DHT.h).
|
||||||
|
void DHT::begin(uint8_t usec) {
|
||||||
// set up the pins!
|
// set up the pins!
|
||||||
pinMode(_pin, INPUT_PULLUP);
|
pinMode(_pin, INPUT_PULLUP);
|
||||||
// Using this value makes sure that millis() - lastreadtime will be
|
// Using this value makes sure that millis() - lastreadtime will be
|
||||||
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
|
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
|
||||||
// but so will the subtraction.
|
// but so will the subtraction.
|
||||||
_lastreadtime = -MIN_INTERVAL;
|
_lastreadtime = millis() - MIN_INTERVAL;
|
||||||
DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
|
DEBUG_PRINT("DHT max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
|
||||||
|
pullTime = usec;
|
||||||
}
|
}
|
||||||
|
|
||||||
//boolean S == Scale. True == Fahrenheit; False == Celcius
|
//boolean S == Scale. True == Fahrenheit; False == Celcius
|
||||||
@ -39,15 +43,27 @@ float DHT::readTemperature(bool S, bool force) {
|
|||||||
switch (_type) {
|
switch (_type) {
|
||||||
case DHT11:
|
case DHT11:
|
||||||
f = data[2];
|
f = data[2];
|
||||||
|
if (data[3] & 0x80) {
|
||||||
|
f = -1 - f ;
|
||||||
|
}
|
||||||
|
f += (data[3] & 0x0f) * 0.1;
|
||||||
|
if(S) {
|
||||||
|
f = convertCtoF(f);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DHT12:
|
||||||
|
f = data[2];
|
||||||
|
f += (data[3] & 0x0f) * 0.1;
|
||||||
|
if (data[2] & 0x80) {
|
||||||
|
f *= -1;
|
||||||
|
}
|
||||||
if(S) {
|
if(S) {
|
||||||
f = convertCtoF(f);
|
f = convertCtoF(f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DHT22:
|
case DHT22:
|
||||||
case DHT21:
|
case DHT21:
|
||||||
f = data[2] & 0x7F;
|
f = ((word)(data[2] & 0x7F)) << 8 | data[3];
|
||||||
f *= 256;
|
|
||||||
f += data[3];
|
|
||||||
f *= 0.1;
|
f *= 0.1;
|
||||||
if (data[2] & 0x80) {
|
if (data[2] & 0x80) {
|
||||||
f *= -1;
|
f *= -1;
|
||||||
@ -71,16 +87,15 @@ float DHT::convertFtoC(float f) {
|
|||||||
|
|
||||||
float DHT::readHumidity(bool force) {
|
float DHT::readHumidity(bool force) {
|
||||||
float f = NAN;
|
float f = NAN;
|
||||||
if (read()) {
|
if (read(force)) {
|
||||||
switch (_type) {
|
switch (_type) {
|
||||||
case DHT11:
|
case DHT11:
|
||||||
f = data[0];
|
case DHT12:
|
||||||
|
f = data[0] + data[1] * 0.1;
|
||||||
break;
|
break;
|
||||||
case DHT22:
|
case DHT22:
|
||||||
case DHT21:
|
case DHT21:
|
||||||
f = data[0];
|
f = ((word)data[0]) << 8 | data[1];
|
||||||
f *= 256;
|
|
||||||
f += data[1];
|
|
||||||
f *= 0.1;
|
f *= 0.1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -89,7 +104,15 @@ float DHT::readHumidity(bool force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
|
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
|
||||||
float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit) {
|
float DHT::computeHeatIndex(bool isFahrenheit) {
|
||||||
|
float hi = computeHeatIndex(readTemperature(isFahrenheit), readHumidity(),
|
||||||
|
isFahrenheit);
|
||||||
|
return hi;
|
||||||
|
}
|
||||||
|
|
||||||
|
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
|
||||||
|
float DHT::computeHeatIndex(float temperature, float percentHumidity,
|
||||||
|
bool isFahrenheit) {
|
||||||
// Using both Rothfusz and Steadman's equations
|
// Using both Rothfusz and Steadman's equations
|
||||||
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
|
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
|
||||||
float hi;
|
float hi;
|
||||||
@ -120,11 +143,11 @@ float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFah
|
|||||||
return isFahrenheit ? hi : convertFtoC(hi);
|
return isFahrenheit ? hi : convertFtoC(hi);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean DHT::read(bool force) {
|
bool DHT::read(bool force) {
|
||||||
// Check if sensor was read less than two seconds ago and return early
|
// Check if sensor was read less than two seconds ago and return early
|
||||||
// to use last reading.
|
// to use last reading.
|
||||||
uint32_t currenttime = millis();
|
uint32_t currenttime = millis();
|
||||||
if (!force && ((currenttime - _lastreadtime) < 2000)) {
|
if (!force && ((currenttime - _lastreadtime) < MIN_INTERVAL)) {
|
||||||
return _lastresult; // return last correct measurement
|
return _lastresult; // return last correct measurement
|
||||||
}
|
}
|
||||||
_lastreadtime = currenttime;
|
_lastreadtime = currenttime;
|
||||||
@ -132,42 +155,55 @@ boolean DHT::read(bool force) {
|
|||||||
// Reset 40 bits of received data to zero.
|
// Reset 40 bits of received data to zero.
|
||||||
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
|
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
|
yield(); // Handle WiFi / reset software watchdog
|
||||||
|
#endif
|
||||||
|
|
||||||
// Send start signal. See DHT datasheet for full signal diagram:
|
// Send start signal. See DHT datasheet for full signal diagram:
|
||||||
// http://www.adafruit.com/datasheets/Digital%20humidity%20and%20temperature%20sensor%20AM2302.pdf
|
// http://www.adafruit.com/datasheets/Digital%20humidity%20and%20temperature%20sensor%20AM2302.pdf
|
||||||
|
|
||||||
// Go into high impedence state to let pull-up raise data line level and
|
// Go into high impedence state to let pull-up raise data line level and
|
||||||
// start the reading process.
|
// start the reading process.
|
||||||
digitalWrite(_pin, HIGH);
|
pinMode(_pin, INPUT_PULLUP);
|
||||||
delay(250);
|
delay(1);
|
||||||
|
|
||||||
// First set data line low for 20 milliseconds.
|
// First set data line low for a period according to sensor type
|
||||||
pinMode(_pin, OUTPUT);
|
pinMode(_pin, OUTPUT);
|
||||||
digitalWrite(_pin, LOW);
|
digitalWrite(_pin, LOW);
|
||||||
delay(20);
|
switch(_type) {
|
||||||
|
case DHT22:
|
||||||
|
case DHT21:
|
||||||
|
delayMicroseconds(1100); // data sheet says "at least 1ms"
|
||||||
|
break;
|
||||||
|
case DHT11:
|
||||||
|
default:
|
||||||
|
delay(20); //data sheet says at least 18ms, 20ms just to be safe
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t cycles[80];
|
uint32_t cycles[80];
|
||||||
{
|
{
|
||||||
// Turn off interrupts temporarily because the next sections are timing critical
|
|
||||||
// and we don't want any interruptions.
|
|
||||||
InterruptLock lock;
|
|
||||||
|
|
||||||
// End the start signal by setting data line high for 40 microseconds.
|
// End the start signal by setting data line high for 40 microseconds.
|
||||||
digitalWrite(_pin, HIGH);
|
pinMode(_pin, INPUT_PULLUP);
|
||||||
delayMicroseconds(40);
|
|
||||||
|
// Delay a moment to let sensor pull data line low.
|
||||||
|
delayMicroseconds(pullTime);
|
||||||
|
|
||||||
// Now start reading the data line to get the value from the DHT sensor.
|
// Now start reading the data line to get the value from the DHT sensor.
|
||||||
pinMode(_pin, INPUT_PULLUP);
|
|
||||||
delayMicroseconds(10); // Delay a bit to let sensor pull data line low.
|
// Turn off interrupts temporarily because the next sections
|
||||||
|
// are timing critical and we don't want any interruptions.
|
||||||
|
InterruptLock lock;
|
||||||
|
|
||||||
// First expect a low signal for ~80 microseconds followed by a high signal
|
// First expect a low signal for ~80 microseconds followed by a high signal
|
||||||
// for ~80 microseconds again.
|
// for ~80 microseconds again.
|
||||||
if (expectPulse(LOW) == 0) {
|
if (expectPulse(LOW) == TIMEOUT) {
|
||||||
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
|
DEBUG_PRINTLN(F("DHT timeout waiting for start signal low pulse."));
|
||||||
_lastresult = false;
|
_lastresult = false;
|
||||||
return _lastresult;
|
return _lastresult;
|
||||||
}
|
}
|
||||||
if (expectPulse(HIGH) == 0) {
|
if (expectPulse(HIGH) == TIMEOUT) {
|
||||||
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
|
DEBUG_PRINTLN(F("DHT timeout waiting for start signal high pulse."));
|
||||||
_lastresult = false;
|
_lastresult = false;
|
||||||
return _lastresult;
|
return _lastresult;
|
||||||
}
|
}
|
||||||
@ -191,8 +227,8 @@ boolean DHT::read(bool force) {
|
|||||||
for (int i=0; i<40; ++i) {
|
for (int i=0; i<40; ++i) {
|
||||||
uint32_t lowCycles = cycles[2*i];
|
uint32_t lowCycles = cycles[2*i];
|
||||||
uint32_t highCycles = cycles[2*i+1];
|
uint32_t highCycles = cycles[2*i+1];
|
||||||
if ((lowCycles == 0) || (highCycles == 0)) {
|
if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) {
|
||||||
DEBUG_PRINTLN(F("Timeout waiting for pulse."));
|
DEBUG_PRINTLN(F("DHT timeout waiting for pulse."));
|
||||||
_lastresult = false;
|
_lastresult = false;
|
||||||
return _lastresult;
|
return _lastresult;
|
||||||
}
|
}
|
||||||
@ -207,7 +243,7 @@ boolean DHT::read(bool force) {
|
|||||||
// stored data.
|
// stored data.
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_PRINTLN(F("Received:"));
|
DEBUG_PRINTLN(F("Received from DHT:"));
|
||||||
DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(F(", "));
|
DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(F(", "));
|
||||||
DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(F(", "));
|
DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(F(", "));
|
||||||
DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(F(", "));
|
DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(F(", "));
|
||||||
@ -221,7 +257,7 @@ boolean DHT::read(bool force) {
|
|||||||
return _lastresult;
|
return _lastresult;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
DEBUG_PRINTLN(F("Checksum failure!"));
|
DEBUG_PRINTLN(F("DHT checksum failure!"));
|
||||||
_lastresult = false;
|
_lastresult = false;
|
||||||
return _lastresult;
|
return _lastresult;
|
||||||
}
|
}
|
||||||
@ -235,14 +271,18 @@ boolean DHT::read(bool force) {
|
|||||||
// in the very latest IDE versions):
|
// in the very latest IDE versions):
|
||||||
// https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_pulse.c
|
// https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_pulse.c
|
||||||
uint32_t DHT::expectPulse(bool level) {
|
uint32_t DHT::expectPulse(bool level) {
|
||||||
|
#if (F_CPU > 16000000L)
|
||||||
uint32_t count = 0;
|
uint32_t count = 0;
|
||||||
|
#else
|
||||||
|
uint16_t count = 0; // To work fast enough on slower AVR boards
|
||||||
|
#endif
|
||||||
// On AVR platforms use direct GPIO port access as it's much faster and better
|
// On AVR platforms use direct GPIO port access as it's much faster and better
|
||||||
// for catching pulses that are 10's of microseconds in length:
|
// for catching pulses that are 10's of microseconds in length:
|
||||||
#ifdef __AVR
|
#ifdef __AVR
|
||||||
uint8_t portState = level ? _bit : 0;
|
uint8_t portState = level ? _bit : 0;
|
||||||
while ((*portInputRegister(_port) & _bit) == portState) {
|
while ((*portInputRegister(_port) & _bit) == portState) {
|
||||||
if (count++ >= _maxcycles) {
|
if (count++ >= _maxcycles) {
|
||||||
return 0; // Exceeded timeout, fail.
|
return TIMEOUT; // Exceeded timeout, fail.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
|
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
|
||||||
@ -250,7 +290,7 @@ uint32_t DHT::expectPulse(bool level) {
|
|||||||
#else
|
#else
|
||||||
while (digitalRead(_pin) == level) {
|
while (digitalRead(_pin) == level) {
|
||||||
if (count++ >= _maxcycles) {
|
if (count++ >= _maxcycles) {
|
||||||
return 0; // Exceeded timeout, fail.
|
return TIMEOUT; // Exceeded timeout, fail.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
12
DHT.h
12
DHT.h
@ -30,6 +30,7 @@ written by Adafruit Industries
|
|||||||
|
|
||||||
// Define types of sensors.
|
// Define types of sensors.
|
||||||
#define DHT11 11
|
#define DHT11 11
|
||||||
|
#define DHT12 12
|
||||||
#define DHT22 22
|
#define DHT22 22
|
||||||
#define DHT21 21
|
#define DHT21 21
|
||||||
#define AM2301 21
|
#define AM2301 21
|
||||||
@ -38,13 +39,14 @@ written by Adafruit Industries
|
|||||||
class DHT {
|
class DHT {
|
||||||
public:
|
public:
|
||||||
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
|
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
|
||||||
void begin(void);
|
void begin(uint8_t usec=55);
|
||||||
float readTemperature(bool S=false, bool force=false);
|
float readTemperature(bool S=false, bool force=false);
|
||||||
float convertCtoF(float);
|
float convertCtoF(float);
|
||||||
float convertFtoC(float);
|
float convertFtoC(float);
|
||||||
|
float computeHeatIndex(bool isFahrenheit=true);
|
||||||
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
|
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
|
||||||
float readHumidity(bool force=false);
|
float readHumidity(bool force=false);
|
||||||
boolean read(bool force=false);
|
bool read(bool force=false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t data[5];
|
uint8_t data[5];
|
||||||
@ -56,6 +58,7 @@ class DHT {
|
|||||||
#endif
|
#endif
|
||||||
uint32_t _lastreadtime, _maxcycles;
|
uint32_t _lastreadtime, _maxcycles;
|
||||||
bool _lastresult;
|
bool _lastresult;
|
||||||
|
uint8_t pullTime; // Time (in usec) to pull up data line before reading
|
||||||
|
|
||||||
uint32_t expectPulse(bool level);
|
uint32_t expectPulse(bool level);
|
||||||
|
|
||||||
@ -64,12 +67,15 @@ class DHT {
|
|||||||
class InterruptLock {
|
class InterruptLock {
|
||||||
public:
|
public:
|
||||||
InterruptLock() {
|
InterruptLock() {
|
||||||
|
#if !defined(ARDUINO_ARCH_NRF52)
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
~InterruptLock() {
|
~InterruptLock() {
|
||||||
|
#if !defined(ARDUINO_ARCH_NRF52)
|
||||||
interrupts();
|
interrupts();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
16
DHT_U.cpp
16
DHT_U.cpp
@ -37,6 +37,9 @@ void DHT_Unified::setName(sensor_t* sensor) {
|
|||||||
case DHT11:
|
case DHT11:
|
||||||
strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
|
strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
|
||||||
break;
|
break;
|
||||||
|
case DHT12:
|
||||||
|
strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1);
|
||||||
|
break;
|
||||||
case DHT21:
|
case DHT21:
|
||||||
strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
|
strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
|
||||||
break;
|
break;
|
||||||
@ -57,6 +60,9 @@ void DHT_Unified::setMinDelay(sensor_t* sensor) {
|
|||||||
case DHT11:
|
case DHT11:
|
||||||
sensor->min_delay = 1000000L; // 1 second (in microseconds)
|
sensor->min_delay = 1000000L; // 1 second (in microseconds)
|
||||||
break;
|
break;
|
||||||
|
case DHT12:
|
||||||
|
sensor->min_delay = 2000000L; // 2 second (in microseconds)
|
||||||
|
break;
|
||||||
case DHT21:
|
case DHT21:
|
||||||
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
|
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
|
||||||
break;
|
break;
|
||||||
@ -105,6 +111,11 @@ void DHT_Unified::Temperature::getSensor(sensor_t* sensor) {
|
|||||||
sensor->min_value = 0.0F;
|
sensor->min_value = 0.0F;
|
||||||
sensor->resolution = 2.0F;
|
sensor->resolution = 2.0F;
|
||||||
break;
|
break;
|
||||||
|
case DHT12:
|
||||||
|
sensor->max_value = 60.0F;
|
||||||
|
sensor->min_value = -20.0F;
|
||||||
|
sensor->resolution = 0.5F;
|
||||||
|
break;
|
||||||
case DHT21:
|
case DHT21:
|
||||||
sensor->max_value = 80.0F;
|
sensor->max_value = 80.0F;
|
||||||
sensor->min_value = -40.0F;
|
sensor->min_value = -40.0F;
|
||||||
@ -159,6 +170,11 @@ void DHT_Unified::Humidity::getSensor(sensor_t* sensor) {
|
|||||||
sensor->min_value = 20.0F;
|
sensor->min_value = 20.0F;
|
||||||
sensor->resolution = 5.0F;
|
sensor->resolution = 5.0F;
|
||||||
break;
|
break;
|
||||||
|
case DHT12:
|
||||||
|
sensor->max_value = 95.0F;
|
||||||
|
sensor->min_value = 20.0F;
|
||||||
|
sensor->resolution = 5.0F;
|
||||||
|
break;
|
||||||
case DHT21:
|
case DHT21:
|
||||||
sensor->max_value = 100.0F;
|
sensor->max_value = 100.0F;
|
||||||
sensor->min_value = 0.0F;
|
sensor->min_value = 0.0F;
|
||||||
|
19
README.md
19
README.md
@ -1,15 +1,14 @@
|
|||||||
This is an Arduino library for the DHT series of low cost temperature/humidity sensors.
|
# Adafruit DHT Humidity & Temperature Sensor Library
|
||||||
|
|
||||||
|
An Arduino library for the DHT series of low cost temperature/humidity sensors.
|
||||||
|
|
||||||
Tutorial: https://learn.adafruit.com/dht
|
Tutorial: https://learn.adafruit.com/dht
|
||||||
|
|
||||||
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder DHT. Check that the DHT folder contains DHT.cpp and DHT.h. Place the DHT library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
**You must have the following Arduino libraries installed to use this class:**
|
||||||
|
|
||||||
# Adafruit DHT Humidity & Temperature Unified Sensor Library
|
|
||||||
|
|
||||||
This library also includes an optional class for the
|
|
||||||
[DHT humidity and temperature sensor](https://learn.adafruit.com/dht/overview)
|
|
||||||
which is designed to work with the [Adafruit unified sensor library](https://learn.adafruit.com/using-the-adafruit-unified-sensor-driver/introduction).
|
|
||||||
|
|
||||||
You must have the following Arduino libraries installed to use this class:
|
|
||||||
|
|
||||||
- [Adafruit Unified Sensor Library](https://github.com/adafruit/Adafruit_Sensor)
|
- [Adafruit Unified Sensor Library](https://github.com/adafruit/Adafruit_Sensor)
|
||||||
|
|
||||||
|
Examples include both a "standalone" DHT example, and one that works along with the Adafruit Unified Sensor Library. Unified sensor library is required even if using the standalone version.
|
||||||
|
|
||||||
|
Recent Arduino IDE releases include the Library Manager for easy installation. Otherwise, to download, click the DOWNLOADS button in the top right corner, rename the uncompressed folder DHT. Check that the DHT folder contains DHT.cpp and DHT.h. Place the DHT library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
||||||
|
|
||||||
|
@ -3,20 +3,22 @@
|
|||||||
// Written by Tony DiCola for Adafruit Industries
|
// Written by Tony DiCola for Adafruit Industries
|
||||||
// Released under an MIT license.
|
// Released under an MIT license.
|
||||||
|
|
||||||
// Depends on the following Arduino libraries:
|
// REQUIRES the following Arduino libraries:
|
||||||
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
|
|
||||||
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
|
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
|
||||||
|
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
|
||||||
|
|
||||||
#include <Adafruit_Sensor.h>
|
#include <Adafruit_Sensor.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
#include <DHT_U.h>
|
#include <DHT_U.h>
|
||||||
|
|
||||||
#define DHTPIN 2 // Pin which is connected to the DHT sensor.
|
#define DHTPIN 2 // Digital pin connected to the DHT sensor
|
||||||
|
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
|
||||||
|
// Pin 15 can work but DHT must be disconnected during program upload.
|
||||||
|
|
||||||
// Uncomment the type of sensor in use:
|
// Uncomment the type of sensor in use:
|
||||||
//#define DHTTYPE DHT11 // DHT 11
|
//#define DHTTYPE DHT11 // DHT 11
|
||||||
#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||||
|
|
||||||
// See guide for details on sensor wiring and usage:
|
// See guide for details on sensor wiring and usage:
|
||||||
// https://learn.adafruit.com/dht/overview
|
// https://learn.adafruit.com/dht/overview
|
||||||
@ -26,33 +28,32 @@ DHT_Unified dht(DHTPIN, DHTTYPE);
|
|||||||
uint32_t delayMS;
|
uint32_t delayMS;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// Initialize device.
|
// Initialize device.
|
||||||
dht.begin();
|
dht.begin();
|
||||||
Serial.println("DHTxx Unified Sensor Example");
|
Serial.println(F("DHTxx Unified Sensor Example"));
|
||||||
// Print temperature sensor details.
|
// Print temperature sensor details.
|
||||||
sensor_t sensor;
|
sensor_t sensor;
|
||||||
dht.temperature().getSensor(&sensor);
|
dht.temperature().getSensor(&sensor);
|
||||||
Serial.println("------------------------------------");
|
Serial.println(F("------------------------------------"));
|
||||||
Serial.println("Temperature");
|
Serial.println(F("Temperature Sensor"));
|
||||||
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
|
||||||
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
|
||||||
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
|
||||||
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
|
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
|
||||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
|
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
|
||||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
|
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
|
||||||
Serial.println("------------------------------------");
|
Serial.println(F("------------------------------------"));
|
||||||
// Print humidity sensor details.
|
// Print humidity sensor details.
|
||||||
dht.humidity().getSensor(&sensor);
|
dht.humidity().getSensor(&sensor);
|
||||||
Serial.println("------------------------------------");
|
Serial.println(F("Humidity Sensor"));
|
||||||
Serial.println("Humidity");
|
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
|
||||||
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
|
||||||
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
|
||||||
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
|
||||||
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
|
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
|
||||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
|
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
|
||||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
|
Serial.println(F("------------------------------------"));
|
||||||
Serial.println("------------------------------------");
|
|
||||||
// Set delay between sensor readings based on sensor details.
|
// Set delay between sensor readings based on sensor details.
|
||||||
delayMS = sensor.min_delay / 1000;
|
delayMS = sensor.min_delay / 1000;
|
||||||
}
|
}
|
||||||
@ -61,24 +62,24 @@ void loop() {
|
|||||||
// Delay between measurements.
|
// Delay between measurements.
|
||||||
delay(delayMS);
|
delay(delayMS);
|
||||||
// Get temperature event and print its value.
|
// Get temperature event and print its value.
|
||||||
sensors_event_t event;
|
sensors_event_t event;
|
||||||
dht.temperature().getEvent(&event);
|
dht.temperature().getEvent(&event);
|
||||||
if (isnan(event.temperature)) {
|
if (isnan(event.temperature)) {
|
||||||
Serial.println("Error reading temperature!");
|
Serial.println(F("Error reading temperature!"));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.print("Temperature: ");
|
Serial.print(F("Temperature: "));
|
||||||
Serial.print(event.temperature);
|
Serial.print(event.temperature);
|
||||||
Serial.println(" *C");
|
Serial.println(F("°C"));
|
||||||
}
|
}
|
||||||
// Get humidity event and print its value.
|
// Get humidity event and print its value.
|
||||||
dht.humidity().getEvent(&event);
|
dht.humidity().getEvent(&event);
|
||||||
if (isnan(event.relative_humidity)) {
|
if (isnan(event.relative_humidity)) {
|
||||||
Serial.println("Error reading humidity!");
|
Serial.println(F("Error reading humidity!"));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.print("Humidity: ");
|
Serial.print(F("Humidity: "));
|
||||||
Serial.print(event.relative_humidity);
|
Serial.print(event.relative_humidity);
|
||||||
Serial.println("%");
|
Serial.println(F("%"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,15 @@
|
|||||||
// Example testing sketch for various DHT humidity/temperature sensors
|
// Example testing sketch for various DHT humidity/temperature sensors
|
||||||
// Written by ladyada, public domain
|
// Written by ladyada, public domain
|
||||||
|
|
||||||
|
// REQUIRES the following Arduino libraries:
|
||||||
|
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
|
||||||
|
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
|
||||||
|
|
||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
|
|
||||||
#define DHTPIN 2 // what digital pin we're connected to
|
#define DHTPIN 2 // Digital pin connected to the DHT sensor
|
||||||
|
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
|
||||||
|
// Pin 15 can work but DHT must be disconnected during program upload.
|
||||||
|
|
||||||
// Uncomment whatever type you're using!
|
// Uncomment whatever type you're using!
|
||||||
//#define DHTTYPE DHT11 // DHT 11
|
//#define DHTTYPE DHT11 // DHT 11
|
||||||
@ -25,7 +31,7 @@ DHT dht(DHTPIN, DHTTYPE);
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("DHTxx test!");
|
Serial.println(F("DHTxx test!"));
|
||||||
|
|
||||||
dht.begin();
|
dht.begin();
|
||||||
}
|
}
|
||||||
@ -44,7 +50,7 @@ void loop() {
|
|||||||
|
|
||||||
// Check if any reads failed and exit early (to try again).
|
// Check if any reads failed and exit early (to try again).
|
||||||
if (isnan(h) || isnan(t) || isnan(f)) {
|
if (isnan(h) || isnan(t) || isnan(f)) {
|
||||||
Serial.println("Failed to read from DHT sensor!");
|
Serial.println(F("Failed to read from DHT sensor!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,17 +59,15 @@ void loop() {
|
|||||||
// Compute heat index in Celsius (isFahreheit = false)
|
// Compute heat index in Celsius (isFahreheit = false)
|
||||||
float hic = dht.computeHeatIndex(t, h, false);
|
float hic = dht.computeHeatIndex(t, h, false);
|
||||||
|
|
||||||
Serial.print("Humidity: ");
|
Serial.print(F("Humidity: "));
|
||||||
Serial.print(h);
|
Serial.print(h);
|
||||||
Serial.print(" %\t");
|
Serial.print(F("% Temperature: "));
|
||||||
Serial.print("Temperature: ");
|
|
||||||
Serial.print(t);
|
Serial.print(t);
|
||||||
Serial.print(" *C ");
|
Serial.print(F("°C "));
|
||||||
Serial.print(f);
|
Serial.print(f);
|
||||||
Serial.print(" *F\t");
|
Serial.print(F("°F Heat index: "));
|
||||||
Serial.print("Heat index: ");
|
|
||||||
Serial.print(hic);
|
Serial.print(hic);
|
||||||
Serial.print(" *C ");
|
Serial.print(F("°C "));
|
||||||
Serial.print(hif);
|
Serial.print(hif);
|
||||||
Serial.println(" *F");
|
Serial.println(F("°F"));
|
||||||
}
|
}
|
||||||
|
14
keywords.txt
14
keywords.txt
@ -12,11 +12,11 @@ DHT KEYWORD1
|
|||||||
# Methods and Functions (KEYWORD2)
|
# Methods and Functions (KEYWORD2)
|
||||||
###########################################
|
###########################################
|
||||||
|
|
||||||
begin KEYWORD2
|
begin KEYWORD2
|
||||||
readTemperature KEYWORD2
|
readTemperature KEYWORD2
|
||||||
convertCtoF KEYWORD2
|
convertCtoF KEYWORD2
|
||||||
convertFtoC KEYWORD2
|
convertFtoC KEYWORD2
|
||||||
computeHeatIndex KEYWORD2
|
computeHeatIndex KEYWORD2
|
||||||
readHumidity KEYWORD2
|
readHumidity KEYWORD2
|
||||||
read KEYWORD2
|
read KEYWORD2
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=DHT sensor library
|
name=DHT sensor library
|
||||||
version=1.3.0
|
version=1.3.5
|
||||||
author=Adafruit
|
author=Adafruit
|
||||||
maintainer=Adafruit <info@adafruit.com>
|
maintainer=Adafruit <info@adafruit.com>
|
||||||
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
|
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
|
||||||
|
Reference in New Issue
Block a user