Skip to content

Commit 973e7c7

Browse files
authored
fix #102, support for KY015 (#103)
- fix #102, add support for KY015 - add three examples, dhtnew_dht11.ino, dhtnew_dht22.ino, dhtnew_simple.ino - optimize decimal part DHT11 (type == 11) - minor fix setType(23), could be bug. - minor fix temperature conversion (-0.0) - add offset functions with full name version (readability), - short ones will become obsolete in the future - update readme.md
1 parent d3cff61 commit 973e7c7

File tree

14 files changed

+466
-94
lines changed

14 files changed

+466
-94
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.1] - 2024-11-24
10+
- fix #102, add support for KY015
11+
- add three examples, dhtnew_dht11.ino, dhtnew_dht22.ino, dhtnew_simple.ino
12+
- optimize decimal part DHT11 (type == 11)
13+
- minor fix setType(23), could be bug.
14+
- minor fix temperature conversion (-0.0)
15+
- add offset functions with full name version (readability),
16+
- short ones will become obsolete in the future
17+
- update readme.md
18+
919
## [0.5.0] - 2024-11-22
1020
- fix #100, different negative algorithm support
1121
- time to bump version

README.md

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,27 @@ As the AM2321 and AM2322 are quite identical according to the datasheet, those a
5050

5151
To use the library one should call **setType(22)** as the protocol is identical to the DHT22.
5252
If there are differences in operation type (23) will be elaborated.
53-
The value 23 is now mapped upon 22 code.
53+
The type 23 is now mapped upon type 22.
5454

5555
Feedback (both positive and negative) about the AM232X sensors is welcome.
5656

5757
**Note: check the datasheet how to connect!**
5858

5959

60+
### KY-015 (dht11)
61+
62+
Since 0.5.1 there is support for the KY-015. Although it is a DHT11, the KY-015 behaves slightly
63+
different (faster wakeup) and was recognized as a DHT22 resulting in faulty conversions.
64+
The library sensor recognition code has been adapted so it should be recognized now.
65+
In the case the recognition fails, one can use **setType(11)** to force the type.
66+
67+
The KY-015 is confirmed to work, see https://github.com/RobTillaart/DHTNew/issues/102
68+
69+
Feedback about the KY-015 sensors is welcome.
70+
71+
**Note: check the datasheet how to connect!**
72+
73+
6074
### 0.5.0 Negative temperature
6175

6276
Apparently there are DHT22's which use another representation for negative temperatures.
@@ -69,9 +83,13 @@ See also https://arduino.stackexchange.com/questions/86448/dht22-sensor-reading-
6983

7084
### Related
7185

72-
- https://github.com/RobTillaart/DHTNew
73-
- https://github.com/RobTillaart/DHTStable
86+
- https://github.com/RobTillaart/DHTNew DHT11/22 etc
87+
- https://github.com/RobTillaart/DHTStable DHT11/22 etc
7488
- https://github.com/RobTillaart/DHT_Simulator
89+
- https://github.com/RobTillaart/DS18B20_INT OneWire temperature sensor
90+
- https://github.com/RobTillaart/DS18B20_RT OneWire temperature sensor
91+
- https://github.com/RobTillaart/SHT31 Sensirion humidity / temperature sensor
92+
- https://github.com/RobTillaart/SHT85 Sensirion humidity / temperature sensor
7593
- https://www.kandrsmith.org/RJS/Misc/Hygrometers/calib_many.html (interesting)
7694
- https://github.com/RobTillaart/Temperature (conversions, dewPoint, heat index etc.)
7795

@@ -85,11 +103,13 @@ See also https://arduino.stackexchange.com/questions/86448/dht22-sensor-reading-
85103
| pin 3 | | Not Connected |
86104
| pin 4 | | GND |
87105

88-
**Note: check the datasheet how to connect!**
106+
**Note: check the datasheet of the sensor how to connect!** as some have only 3 pins.
89107

90108

91109
## Specification DHT22
92110

111+
As most used model, for other sensors I refer to datasheet.
112+
93113
| Model | DHT22 | Notes |
94114
|:----------------------------|:-------------------------|:--------|
95115
| Power supply | 3.3 - 6.0 V DC |
@@ -115,21 +135,22 @@ See also https://arduino.stackexchange.com/questions/86448/dht22-sensor-reading-
115135
### Constructor
116136

117137
- **DHTNEW(uint8_t pin)** defines the dataPin of the sensor.
118-
- **void reset()** might help to reset a sensor behaving badly. It does reset the library settings to default,
138+
- **void reset()** might help to reset a sensor behaving badly.
139+
It resets the library internal settings to default,
119140
however it does not reset the sensor in a hardware way.
120141
- **uint8_t getType()** 0 = unknown, 11 or 22.
121142
In case of 0, **getType()** will try to determine type.
122143
Since 0.4.14 type 70 is added for **experimental** Sonoff Si7021 support.
123144
- **void setType(uint8_t type = 0)** allows to force the type of the sensor.
124145

125-
| Type | Sensors | Notes |
126-
|:-------:|:---------------:|:--------|
127-
| 0 | not defined |
128-
| 11 | DHT11 |
129-
| 22 | DHT22 a.o | most others
130-
| 23 | DHT22 a.o | mapped to 22 for now
131-
| 70 | Sonoff Si7021 | experimental
132-
| other | sets to 0 | 0.4.20
146+
| Type | Sensors | Notes |
147+
|:-------:|:-------------------------:|:--------|
148+
| 0 | not defined |
149+
| 11 | DHT11 DHT12, KY015 | KY015 needs setType(11)
150+
| 22 | DHT22, DHT33, DHT44 a.o | most others
151+
| 23 | DHT23 | mapped to 22 for now
152+
| 70 | Sonoff Si7021 | experimental
153+
| other | sets to 0 | 0.4.20
133154

134155

135156
### Base interface
@@ -149,10 +170,19 @@ Note this error value can be suppressed by **setSuppressError(bool)**.
149170

150171
Adding offsets works well in normal range however they might introduce
151172
under- or overflow at the ends of the sensor range.
152-
Humidity is in % RH.
153-
Humidity is constrained to 0.0 - 100.0 % in the code.
154-
Temperature is in degrees Celsius.
155-
For temperature such constrain would be type dependant, so it is not done.
173+
174+
Humidity offset is in % RH and is constrained to 0.0 - 100.0 % in the code.
175+
176+
Temperature offset is in degrees Celsius.
177+
For temperature a constrain would be type dependant, so it is not done.
178+
Furthermore by setting the offset to -273.15 one get the **Kelvin** scale.
179+
180+
- **void setHumidityOffset(float offset)** typical < ±5% RH.
181+
- **void setTemperatureOffset(float offset)** typical < ±2°C.
182+
- **float getHumidityOffset()** idem.
183+
- **float getTemperatureOffset()** idem.
184+
185+
The "short-named" offset functions will become obsolete in the future (0.6.0).
156186

157187
- **void setHumOffset(float offset)** typical < ±5% RH.
158188
- **void setTempOffset(float offset)** typical < ±2°C.
@@ -311,7 +341,7 @@ updated readme.md - added badges and remarks after testing with MKR1010 Wifi.
311341
updated TIMEOUT_C from 70 -> 90 us to minimize its occurrence - See https://github.com/RobTillaart/DHTNew/issues/67.
312342
added ```while(!Serial);``` in examples to they work for MKR1010 Wifi.
313343
27. (0.4.11)
314-
update library.json, license, minor edits (cleanup), unit tests
344+
update library.json, license, minor edits (clean up), unit tests
315345
28. (0.4.12)
316346
Fix #72, delayMicroseconds() for wakeUp
317347
29. (0.4.13)
@@ -332,6 +362,10 @@ Update readme.md
332362
Update GitHub actions and readme.md
333363
36. (0.4.21)
334364
Add dhtnew_pulse_diag_ext.ino
365+
37. (0.5.0)
366+
Fix negative values
367+
38. (0.5.1)
368+
Support KY015 and more.
335369

336370

337371
## Future
@@ -360,11 +394,17 @@ if (type == 11) temp = constrain(temp, 0, 100);
360394
if (type == 22) temp = constrain(temp, -40, 80);
361395
etc.
362396
```
363-
397+
- type parameter in constructor, default 0
398+
- reimplement the recognition algorithm (separate class?)
399+
- read as 22 => check the hum / temp data range to determine type
400+
- read as 11 => check idem
401+
- read as 70 => check idem
402+
- split wakeup time from bit interpretation used.
364403

365404
#### Wont
366405

367406
- move all code from .h to .cpp
407+
- derived classes for fixed type?
368408

369409

370410
## Support

dhtnew.cpp

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: dhtnew.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.5.0
4+
// VERSION: 0.5.1
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: https://github.com/RobTillaart/DHTNEW
77
//
@@ -13,6 +13,7 @@
1313

1414

1515
// these defines are not for user to adjust (microseconds)
16+
// it adds 10% above the data sheet to allow a margin.
1617
#define DHTLIB_DHT11_WAKEUP (18 * 1100UL)
1718
#define DHTLIB_DHT_WAKEUP (1 * 1100UL)
1819
// experimental 0.4.14
@@ -45,19 +46,20 @@ void DHTNEW::reset()
4546
pinMode(_dataPin, OUTPUT);
4647
digitalWrite(_dataPin, HIGH);
4748

48-
_wakeupDelay = 0;
49-
_type = 0;
50-
_humOffset = 0.0;
51-
_tempOffset = 0.0;
52-
_humidity = 0.0;
53-
_temperature = 0.0;
54-
_lastRead = 0;
55-
_disableIRQ = true;
56-
_waitForRead = false;
57-
_suppressError = false;
58-
_readDelay = 0;
49+
_wakeupDelay = 0;
50+
_type = 0;
51+
_humidityOffset = 0.0;
52+
_temperatureOffset = 0.0;
53+
_humidity = 0.0;
54+
_temperature = 0.0;
55+
_lastRead = 0;
56+
_disableIRQ = true;
57+
_waitForRead = false;
58+
_suppressError = false;
59+
_readDelay = 0;
5960
#if defined(__AVR__)
60-
_disableIRQ = false;
61+
// overrule
62+
_disableIRQ = false;
6163
#endif
6264
// #if defined(ARDUINO_SAMD_MKRWIFI1010) // fix for issue #67
6365
// _disableIRQ = false;
@@ -80,7 +82,7 @@ void DHTNEW::setType(uint8_t type)
8082

8183
if ((type == 22) || (type == 23))
8284
{
83-
_type = type;
85+
_type = 22; // cannot differentiate type;
8486
_wakeupDelay = DHTLIB_DHT_WAKEUP;
8587
}
8688
else if (type == 11)
@@ -125,27 +127,51 @@ int DHTNEW::read()
125127
}
126128

127129
// AUTODETECT
128-
// make sure sensor had time to wake up.
130+
// make sure sensor had time to wake up after start of sketch.
129131
while (millis() < 1000);
130132

133+
// NOTE: we could add conversion H and T here and
134+
// check if these are in the allowed range.
135+
// humidity 0..100%, temperature 0..80C or -40..80C
136+
// drawback: would increase footprint.
137+
131138
// NOTE: cannot differentiate between type 23 and 22
132139
_type = 22;
133140
_wakeupDelay = DHTLIB_DHT_WAKEUP;
134141
int rv = _read();
135-
if (rv == DHTLIB_OK) return rv;
142+
if (rv == DHTLIB_OK)
143+
{
144+
// see issue #102
145+
// test high humidity bits to check for KY015/ DHT11 encoding
146+
// in DHT22 encoding humidity cannot be over 100.0 % == 0x03E8
147+
// so the high bits cannot be over 0x03
148+
// test can incorrectly fail if there is an extreme low humidity
149+
// note desert air still has about ~20% RH
150+
if (_bits[0] <= 0x03)
151+
{
152+
return rv;
153+
}
154+
// fall through to test KY015 as DHT11
155+
}
136156

137157
_type = 11;
138158
_wakeupDelay = DHTLIB_DHT11_WAKEUP;
139159
rv = _read();
140-
if (rv == DHTLIB_OK) return rv;
160+
if (rv == DHTLIB_OK)
161+
{
162+
return rv;
163+
}
141164

142165
// experimental 0.4.14
143166
_type = 70;
144167
_wakeupDelay = DHTLIB_SI7021_WAKEUP;
145168
rv = _read();
146-
if (rv == DHTLIB_OK) return rv;
169+
if (rv == DHTLIB_OK)
170+
{
171+
return rv;
172+
}
147173

148-
_type = 0; // retry next time
174+
_type = 0; // retry next time
149175
return rv;
150176
}
151177

@@ -185,10 +211,12 @@ int DHTNEW::_read()
185211
return rv; // propagate error value
186212
}
187213

188-
if (_type == 11) // DHT11, DH12, compatible
214+
if (_type == 11) // DHT11, DH12, KY015 compatible
189215
{
190-
_humidity = _bits[0] + _bits[1] * 0.1;
191-
_temperature = _bits[2] + _bits[3] * 0.1;
216+
_humidity = _bits[0];
217+
if (_bits[1]) _humidity += _bits[1] * 0.1;
218+
_temperature = _bits[2];
219+
if (_bits[3]) _temperature += _bits[3] * 0.1;
192220
}
193221
else // DHT22, DHT33, DHT44, compatible + Si7021
194222
{
@@ -198,11 +226,15 @@ int DHTNEW::_read()
198226
if ((_bits[2] & 0x80) != 0x80 )
199227
{
200228
int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]);
229+
// prevent -0.0;
201230
if (t == 0)
202231
{
203-
_temperature = 0.0; // prevent -0.0;
232+
_temperature = 0.0;
233+
}
234+
else
235+
{
236+
_temperature = t * 0.1;
204237
}
205-
_temperature = t * 0.1;
206238
}
207239
else // negative temperature
208240
{
@@ -257,15 +289,16 @@ int DHTNEW::_read()
257289
}
258290
#endif
259291

260-
if (_humOffset != 0.0)
292+
if (_humidityOffset != 0.0)
261293
{
262-
_humidity += _humOffset;
294+
_humidity += _humidityOffset;
295+
// constrain range
263296
if (_humidity > 100) _humidity = 100;
264297
else if (_humidity < 0) _humidity = 0;
265298
}
266-
if (_tempOffset != 0.0)
299+
if (_temperatureOffset != 0.0)
267300
{
268-
_temperature += _tempOffset;
301+
_temperature += _temperatureOffset;
269302
}
270303

271304
// TEST CHECKSUM

0 commit comments

Comments
 (0)