Skip to content

Commit d9e9c89

Browse files
committed
Initial commit
0 parents  commit d9e9c89

File tree

7 files changed

+2691
-0
lines changed

7 files changed

+2691
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*~
2+
doc

Doxyfile

Lines changed: 2435 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Arduino-DHT
2+
The DHT11 library is an Arduino-GPIO based device driver for the DHTXX
3+
Humidity and Temperature Sensor (DHT11, DHT21 and DHT22).
4+
5+
Version: 1.0
6+
7+
## Classes
8+
9+
* [DHTXX Device Driver, DHT](./src/DHT.h)
10+
11+
## Example Sketches
12+
13+
* [DHT11](./examples/DHT11)
14+
15+
## Dependencies
16+
17+
* [General Purpose Input/Output library for Arduino, GPIO](https://github.com/mikaelpatel/Arduino-GPIO)

examples/DHT11/DHT11.ino

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "GPIO.h"
2+
#include "DHT.h"
3+
4+
#if defined(ARDUINO_attiny)
5+
#include "Software/Serial.h"
6+
Software::Serial<BOARD::D0> Serial;
7+
DHT11<BOARD::D1> dht;
8+
#else
9+
DHT11<BOARD::D7> dht;
10+
#endif
11+
12+
void setup()
13+
{
14+
Serial.begin(57600);
15+
while (!Serial);
16+
}
17+
18+
void loop()
19+
{
20+
float humidity, temperature;
21+
int res = dht.read(humidity, temperature);
22+
if (res > 0) {
23+
Serial.print(millis());
24+
Serial.print(F(": "));
25+
Serial.print(humidity);
26+
Serial.print(F(" RH, "));
27+
Serial.print(temperature);
28+
Serial.println(F(" C"));
29+
}
30+
else if (res < 0) {
31+
Serial.print(millis());
32+
Serial.print(F(": res = "));
33+
Serial.println(res);
34+
}
35+
delay(1000);
36+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Arduino-DHT
2+
version=1.0
3+
author=Mikael Patel
4+
maintainer=Mikael Patel <mikael.patel@gmail.com>
5+
sentence=Digital Humidity and Temperature (DHT) Sensor library for Arduino.
6+
paragraph=The DHT library is an Arduino-GPIO based device driver for the DHTXX Humidity & Temperature Sensors (DHT11, DHT21 and DHT22).
7+
category=Sensors
8+
url=https://github.com/mikaelpatel/Arduino-DHT
9+
architectures=avr

mainpage.dox

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** @mainpage Arduino-DHT
2+
3+
The DHT library is an Arduino-GPIO based device driver for the DHTXX
4+
Humidity and Temperature Sensors (DHT11, DHT21 and DHT22).
5+
6+
Version: 1.0
7+
*/
8+
9+
/** @page License
10+
11+
Copyright (C) 2017, Mikael Patel
12+
13+
This library is free software; you can redistribute it and/or
14+
modify it under the terms of the GNU Lesser General Public
15+
License as published by the Free Software Foundation; either
16+
version 2.1 of the License, or (at your option) any later version.
17+
18+
This library is distributed in the hope that it will be useful,
19+
but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
Lesser General Public License for more details.
22+
23+
You should have received a copy of the GNU Lesser General
24+
Public License along with this library; if not, write to the
25+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
26+
Boston, MA 02111-1307 USA
27+
*/

src/DHT.h

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* @file DHT.h
3+
* @version 1.0
4+
*
5+
* @section License
6+
* Copyright (C) 2017, Mikael Patel
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*/
18+
19+
#ifndef DHT_H
20+
#define DHT_H
21+
22+
#include "GPIO.h"
23+
24+
/**
25+
* DHT Humidity & Temperature Sensor device driver template class.
26+
* @param[in] DATA_PIN board pin for data signal.
27+
* @param[in] DEVICE type number (11, 21 or 22).
28+
* @section Circuit
29+
* @code
30+
* DHTXX
31+
* +------------+
32+
* (VCC)---------------1-|VCC ====== |
33+
* (DATA_PIN)----------2-|DATA ====== |
34+
* 3-| ====== |
35+
* (GND)---------------4-|GND ====== |
36+
* +------------+
37+
* @endcode
38+
* Connect DHT to data pin, VCC and ground. A pullup resistor from
39+
* the pin to VCC should be used. Most DHT modules have a built-in
40+
* pullup resistor.
41+
*/
42+
template<BOARD::pin_t DATA_PIN, uint8_t DEVICE>
43+
class DHT {
44+
public:
45+
/**
46+
* Initiate device communication pin.
47+
*/
48+
DHT() :
49+
m_humidity(0),
50+
m_temperature(0)
51+
{
52+
m_data.input();
53+
m_data.low();
54+
}
55+
56+
/**
57+
* Return latest humidity reading.
58+
* @return humidity.
59+
*/
60+
float humidity()
61+
{
62+
return (m_humidity);
63+
}
64+
65+
/**
66+
* Return latest temperature reading.
67+
* @return temperature.
68+
*/
69+
float temperature()
70+
{
71+
return (m_temperature);
72+
}
73+
74+
/**
75+
* Read humidity and temperature from device. Returns number of changed
76+
* values (0-2) otherwise negative error code (-1: check sum error,
77+
* -2: response pulse error)
78+
*/
79+
int read(float& humidity, float& temperature)
80+
{
81+
// Issue start signal and wait for device to respond
82+
m_data.output();
83+
delay(START_SIGNAL);
84+
m_data.input();
85+
uint8_t retry = 16;
86+
do {
87+
delayMicroseconds(PULLUP);
88+
} while (m_data != 0 && --retry);
89+
if (retry == 0 || m_data.pulse() < THRESHOLD) return (-2);
90+
91+
// Read data from the device. Each bit is pulse width coded;
92+
// low for 50 us, and high 26-28 us for zero(0), and 70 us for one(1).
93+
uint8_t chsum = 0;
94+
uint8_t d[5];
95+
for (int i = 0; i < 5; i++) {
96+
uint8_t v = 0;
97+
for (int j = 0; j < 8; j++)
98+
v = (v << 1) | (m_data.pulse() > THRESHOLD);
99+
d[i] = v;
100+
if (i < 4) chsum += v;
101+
}
102+
103+
// Validate check sum
104+
if (chsum != d[4]) return (-1);
105+
106+
// Check type of device and convert data
107+
if (DEVICE == 11) {
108+
humidity = d[0];
109+
temperature = d[2];
110+
}
111+
else {
112+
humidity = ((d[0] << 8) | d[1]) / 10.0;
113+
temperature = (((d[2] & 0x7f) << 8) | d[3]) / 10.0;
114+
if (d[2] & 0x80) temperature *= -1;
115+
}
116+
117+
// Check for value changes
118+
int res = (humidity != m_humidity) + (temperature != m_temperature);
119+
if (res > 0) {
120+
m_humidity = humidity;
121+
m_temperature = temperature;
122+
}
123+
124+
// Return number of changed values: 0, 1 och 2
125+
return (res);
126+
}
127+
128+
protected:
129+
/** Start signal pulse width in milliseconds. */
130+
static const uint16_t START_SIGNAL = 18;
131+
132+
/** Pullup delay in microseconds. */
133+
static const uint16_t PULLUP = 4;
134+
135+
/** Pulse threshold in microseconds. */
136+
static const int THRESHOLD = 60;
137+
138+
/** Board pin for data communication. */
139+
GPIO<DATA_PIN> m_data;
140+
141+
/** Latest humidity reading. */
142+
float m_humidity;
143+
144+
/** Latest temperature reading. */
145+
float m_temperature;
146+
};
147+
148+
/**
149+
* DHT11 Humidity & Temperature Sensor device driver template class.
150+
* @param[in] DATA_PIN board pin for data signal.
151+
*/
152+
template<BOARD::pin_t DATA_PIN> class DHT11 : public DHT<DATA_PIN, 11> {};
153+
154+
/**
155+
* DHT21 Humidity & Temperature Sensor device driver template class.
156+
* @param[in] DATA_PIN board pin for data signal.
157+
*/
158+
template<BOARD::pin_t DATA_PIN> class DHT21 : public DHT<DATA_PIN, 21> {};
159+
160+
/**
161+
* DHT22 Humidity & Temperature Sensor device driver template class.
162+
* @param[in] DATA_PIN board pin for data signal.
163+
*/
164+
template<BOARD::pin_t DATA_PIN> class DHT22 : public DHT<DATA_PIN, 22> {};
165+
#endif

0 commit comments

Comments
 (0)