Skip to content

Commit e2d0b3b

Browse files
committed
+ version 0.1.13 (version 0.1.12 not usable -> skipped)
+ added support for DHT33 + DHT44 + added test sketches for DHT33 + DHT44 + improved protocol handling + refactored code style, + refactored footprint (part interface inline) + renamed private _read => _readSensor + added dht_tuning sketch to see timing
1 parent ba5e9bd commit e2d0b3b

File tree

6 files changed

+242
-38
lines changed

6 files changed

+242
-38
lines changed

libraries/DHTlib/dht.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//
22
// FILE: dht.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.11
4+
// VERSION: 0.1.13
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: http://arduino.cc/playground/Main/DHTLib
77
//
88
// HISTORY:
9+
// 0.1.13 fix negative temperature
10+
// 0.1.12 support DHT33 and DHT44 initial version
911
// 0.1.11 renamed DHTLIB_TIMEOUT
1012
// 0.1.10 optimized faster WAKEUP + TIMEOUT
1113
// 0.1.09 optimize size: timeout check + use of mask
@@ -38,7 +40,7 @@
3840
int dht::read11(uint8_t pin)
3941
{
4042
// READ VALUES
41-
int rv = read(pin, DHTLIB_DHT11_WAKEUP);
43+
int rv = _readSensor(pin, DHTLIB_DHT11_WAKEUP);
4244
if (rv != DHTLIB_OK)
4345
{
4446
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -58,24 +60,15 @@ int dht::read11(uint8_t pin)
5860
return DHTLIB_OK;
5961
}
6062

61-
// return values:
62-
// DHTLIB_OK
63-
// DHTLIB_ERROR_CHECKSUM
64-
// DHTLIB_ERROR_TIMEOUT
65-
int dht::read21(uint8_t pin)
66-
{
67-
// dataformat & wakeup identical to DHT22
68-
return read22(pin);
69-
}
7063

7164
// return values:
7265
// DHTLIB_OK
7366
// DHTLIB_ERROR_CHECKSUM
7467
// DHTLIB_ERROR_TIMEOUT
75-
int dht::read22(uint8_t pin)
68+
int dht::read(uint8_t pin)
7669
{
7770
// READ VALUES
78-
int rv = read(pin, DHTLIB_DHT22_WAKEUP);
71+
int rv = _readSensor(pin, DHTLIB_DHT_WAKEUP);
7972
if (rv != DHTLIB_OK)
8073
{
8174
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -85,20 +78,18 @@ int dht::read22(uint8_t pin)
8578

8679
// CONVERT AND STORE
8780
humidity = word(bits[0], bits[1]) * 0.1;
88-
89-
if (bits[2] & 0x80) // negative temperature
90-
{
91-
temperature = -0.1 * word(bits[2] & 0x7F, bits[3]);
92-
}
93-
else
81+
temperature = word(bits[2] & 0x7F, bits[3]) * 0.1;
82+
if (bits[2] & 0x80) // negative temperature
9483
{
95-
temperature = 0.1 * word(bits[2], bits[3]);
84+
temperature = -temperature;
9685
}
9786

9887
// TEST CHECKSUM
9988
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
100-
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
101-
89+
if (bits[4] != sum)
90+
{
91+
return DHTLIB_ERROR_CHECKSUM;
92+
}
10293
return DHTLIB_OK;
10394
}
10495

@@ -110,7 +101,7 @@ int dht::read22(uint8_t pin)
110101
// return values:
111102
// DHTLIB_OK
112103
// DHTLIB_ERROR_TIMEOUT
113-
int dht::read(uint8_t pin, uint8_t wakeupDelay)
104+
int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay)
114105
{
115106
// INIT BUFFERVAR TO RECEIVE DATA
116107
uint8_t mask = 128;
@@ -128,7 +119,7 @@ int dht::read(uint8_t pin, uint8_t wakeupDelay)
128119
pinMode(pin, INPUT);
129120

130121
// GET ACKNOWLEDGE or TIMEOUT
131-
unsigned int loopCnt = DHTLIB_TIMEOUT;
122+
uint16_t loopCnt = DHTLIB_TIMEOUT;
132123
while(digitalRead(pin) == LOW)
133124
{
134125
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
@@ -141,30 +132,36 @@ int dht::read(uint8_t pin, uint8_t wakeupDelay)
141132
}
142133

143134
// READ THE OUTPUT - 40 BITS => 5 BYTES
144-
for (uint8_t i = 0; i < 40; i++)
135+
for (uint8_t i = 40; i != 0; i--)
145136
{
146137
loopCnt = DHTLIB_TIMEOUT;
147138
while(digitalRead(pin) == LOW)
148139
{
149140
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
150141
}
151142

152-
unsigned long t = micros();
143+
uint32_t t = micros();
153144

154145
loopCnt = DHTLIB_TIMEOUT;
155146
while(digitalRead(pin) == HIGH)
156147
{
157148
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
158149
}
159150

160-
if ((micros() - t) > 40) bits[idx] |= mask;
151+
if ((micros() - t) > 40)
152+
{
153+
bits[idx] |= mask;
154+
}
161155
mask >>= 1;
162156
if (mask == 0) // next byte?
163157
{
164158
mask = 128;
165159
idx++;
166160
}
167161
}
162+
pinMode(pin, OUTPUT);
163+
digitalWrite(pin, HIGH);
164+
168165
return DHTLIB_OK;
169166
}
170167
//

libraries/DHTlib/dht.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: dht.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.11
4+
// VERSION: 0.1.13
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: http://arduino.cc/playground/Main/DHTLib
77
//
@@ -18,15 +18,15 @@
1818
#include <Arduino.h>
1919
#endif
2020

21-
#define DHT_LIB_VERSION "0.1.11"
21+
#define DHT_LIB_VERSION "0.1.13"
2222

2323
#define DHTLIB_OK 0
2424
#define DHTLIB_ERROR_CHECKSUM -1
2525
#define DHTLIB_ERROR_TIMEOUT -2
2626
#define DHTLIB_INVALID_VALUE -999
2727

2828
#define DHTLIB_DHT11_WAKEUP 18
29-
#define DHTLIB_DHT22_WAKEUP 1
29+
#define DHTLIB_DHT_WAKEUP 1
3030

3131
// max timeout is 100usec.
3232
// For a 16Mhz proc that is max 1600 clock cycles
@@ -38,16 +38,24 @@
3838
class dht
3939
{
4040
public:
41+
// return values:
42+
// DHTLIB_OK
43+
// DHTLIB_ERROR_CHECKSUM
44+
// DHTLIB_ERROR_TIMEOUT
4145
int read11(uint8_t pin);
42-
int read21(uint8_t pin);
43-
int read22(uint8_t pin);
46+
int read(uint8_t pin);
47+
48+
inline int read21(uint8_t pin) { return read(pin); };
49+
inline int read22(uint8_t pin) { return read(pin); };
50+
inline int read33(uint8_t pin) { return read(pin); };
51+
inline int read44(uint8_t pin) { return read(pin); };
4452

4553
double humidity;
4654
double temperature;
4755

4856
private:
4957
uint8_t bits[5]; // buffer to receive data
50-
int read(uint8_t pin, uint8_t wakeupDelay);
58+
int _readSensor(uint8_t pin, uint8_t wakeupDelay);
5159
};
5260
#endif
5361
//
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// FILE: dht33_test.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE: DHT library test sketch for DHT33 && Arduino
6+
// URL:
7+
//
8+
// Released to the public domain
9+
//
10+
11+
#include <dht.h>
12+
13+
dht DHT;
14+
15+
#define DHT33_PIN 5
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println("DHT TEST PROGRAM ");
21+
Serial.print("LIBRARY VERSION: ");
22+
Serial.println(DHT_LIB_VERSION);
23+
Serial.println();
24+
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
25+
}
26+
27+
void loop()
28+
{
29+
// READ DATA
30+
Serial.print("DHT33, \t");
31+
32+
uint32_t start = micros();
33+
int chk = DHT.read33(DHT33_PIN);
34+
uint32_t stop = micros();
35+
36+
switch (chk)
37+
{
38+
case DHTLIB_OK:
39+
Serial.print("OK,\t");
40+
break;
41+
case DHTLIB_ERROR_CHECKSUM:
42+
Serial.print("Checksum error,\t");
43+
break;
44+
case DHTLIB_ERROR_TIMEOUT:
45+
Serial.print("Time out error,\t");
46+
break;
47+
default:
48+
Serial.print("Unknown error,\t");
49+
break;
50+
}
51+
// DISPLAY DATA
52+
Serial.print(DHT.humidity, 1);
53+
Serial.print(",\t");
54+
Serial.print(DHT.temperature, 1);
55+
Serial.print(",\t");
56+
Serial.print(stop - start);
57+
Serial.println();
58+
59+
// FOR UNO + DHT33 400ms SEEMS TO BE MINIMUM DELAY BETWEEN SENSOR READS,
60+
// ADJUST TO YOUR NEEDS
61+
delay(1000);
62+
}
63+
//
64+
// END OF FILE
65+
//
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// FILE: dht44_test.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE: DHT library test sketch for DHT44 && Arduino
6+
// URL:
7+
//
8+
// Released to the public domain
9+
//
10+
11+
#include <dht.h>
12+
13+
dht DHT;
14+
15+
#define DHT44_PIN 5
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println("DHT TEST PROGRAM ");
21+
Serial.print("LIBRARY VERSION: ");
22+
Serial.println(DHT_LIB_VERSION);
23+
Serial.println();
24+
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
25+
}
26+
27+
void loop()
28+
{
29+
// READ DATA
30+
Serial.print("DHT44, \t");
31+
32+
uint32_t start = micros();
33+
int chk = DHT.read44(DHT44_PIN);
34+
uint32_t stop = micros();
35+
36+
switch (chk)
37+
{
38+
case DHTLIB_OK:
39+
Serial.print("OK,\t");
40+
break;
41+
case DHTLIB_ERROR_CHECKSUM:
42+
Serial.print("Checksum error,\t");
43+
break;
44+
case DHTLIB_ERROR_TIMEOUT:
45+
Serial.print("Time out error,\t");
46+
break;
47+
default:
48+
Serial.print("Unknown error,\t");
49+
break;
50+
}
51+
// DISPLAY DATA
52+
Serial.print(DHT.humidity, 1);
53+
Serial.print(",\t");
54+
Serial.print(DHT.temperature, 1);
55+
Serial.print(",\t");
56+
Serial.print(stop - start);
57+
Serial.println();
58+
59+
// FOR UNO + DHT44 500ms SEEMS TO BE MINIMUM DELAY BETWEEN SENSOR READS,
60+
// ADJUST TO YOUR NEEDS
61+
62+
delay(1000);
63+
}
64+
//
65+
// END OF FILE
66+
//
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// FILE: dht_tuning.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE: DHT test sketch for DHT22 && Arduino => find minimum time between reads
6+
// URL:
7+
//
8+
// Released to the public domain
9+
//
10+
11+
#include <dht.h>
12+
13+
dht DHT;
14+
15+
#define DHT22_PIN 5
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println("DHT TEST PROGRAM ");
21+
Serial.print("LIBRARY VERSION: ");
22+
Serial.println(DHT_LIB_VERSION);
23+
Serial.println();
24+
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
25+
}
26+
27+
int del = 500;
28+
29+
void loop()
30+
{
31+
// READ DATA
32+
Serial.print("DHT22, \t");
33+
34+
uint32_t start = micros();
35+
int chk = DHT.read22(DHT22_PIN);
36+
uint32_t stop = micros();
37+
38+
switch (chk)
39+
{
40+
case DHTLIB_OK:
41+
Serial.print("OK,\t");
42+
del -= 10;
43+
break;
44+
case DHTLIB_ERROR_CHECKSUM:
45+
Serial.print("Checksum error,\t");
46+
break;
47+
case DHTLIB_ERROR_TIMEOUT:
48+
Serial.print("Time out error,\t");
49+
del += 10;
50+
break;
51+
default:
52+
Serial.print("Unknown error,\t");
53+
break;
54+
}
55+
// DISPLAY DATA
56+
Serial.print(DHT.humidity, 1);
57+
Serial.print(",\t");
58+
Serial.print(DHT.temperature, 1);
59+
Serial.print(",\t");
60+
Serial.print(stop - start);
61+
Serial.print(",\t");
62+
Serial.print(del);
63+
Serial.println();
64+
65+
delay(del);
66+
}
67+
//
68+
// END OF FILE
69+
//

0 commit comments

Comments
 (0)