README.md (8210B)
1 2 [![Arduino CI](https://github.com/RobTillaart/DHTNew/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) 3 [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DHTNew/blob/master/LICENSE) 4 [![GitHub release](https://img.shields.io/github/release/RobTillaart/DHTNew.svg?maxAge=3600)](https://github.com/RobTillaart/DHTNew/releases) 5 6 # DHTNew 7 8 Arduino library for DHT11 and DHT22 with automatic sensortype recognition. 9 10 11 ## Description 12 13 DHTNEW is stable for both ARM and AVR. 14 It is based upon the well tested DHTlib code. 15 16 17 ## History 18 19 DHTNEW has some new features compared to the DHTlib code. 20 21 1. The constructor has a pin number, so the one sensor - one object paradigm is chosen. 22 So you can now make a DHTNEW object bathroom(4), kitchen(3), etc. 23 2. The **read()** function now reads both DHT11 and DHT22 sensors and selects the right 24 math per sensor based upon the bit patterns. 25 3. An **offset** can be set for both temperature and humidity to have a first-order linear 26 calibration in the class itself. Of course, this introduces a possible risk of 27 under- or overflow. 28 For a more elaborated or non-linear offset, I refer to my multimap class. 29 4. **lastRead()** keeps track of the last time the sensor is read. If this is not too long ago 30 one can decide not to read the sensors but use the current values for temperature and humidity. 31 This saves up to 20+ milliseconds for a DHT11 or 5+ millis for a DHT22. Note that these sensors 32 should have 1-2 seconds between reads according to specification. 33 In the future, this functionality could be inside the library by setting a time threshold 34 (e.g. 1 second by default) to give more stable results. 35 5. Added **interrupt enable/disable flag** to prevent interrupts disturb timing of DHT protocol. 36 Be aware that this may affect other parts of your application. 37 6. (0.1.7) added an automatic check of lastRead in the read call. If request a read to fast it will just return OK. 38 7. (0.1.7) added **waitForReading flag** (kudos to Mr-HaleYa) to let the sensor explicitly 39 wait until a new value can be read. 40 8. (0.2.0) Temperature and humidity are private now, use **getTemperature()** and **getHumidity()** 41 9. (0.2.1) Adjusted the bit timing threshold to work around issue #11 42 10. (0.2.2) added **ERROR_SENSOR_NOT_READY** and differentiated timeout errors. 43 11. (0.3.0) 44 removed interrupt flag, now the library always disables interrupts during 45 the clocking of the bits. 46 Added getReadDelay & setReadDelay to tune reading interval. Check the example code. 47 Adjusted the timing in the wake-up part of the protocol. 48 Added more comments to describe the protocol. 49 12. (0.3.1) 50 added **powerDown()** and **powerUp()** for low power applications. Note that after **powerUp()** 51 the user must wait for two seconds before doing a read(). Just like after a (re)boot. 52 Note: The lib does not (yet) control the power pin of the sensor. 53 Discussion see https://github.com/RobTillaart/DHTNew/issues/13 54 13. (0.3.2) 55 Added **setSuppressError()** and **getSuppressError()** so the library will not output -999 56 but the last known valid value for temperature and humidity. 57 This flag is usefull to suppress 'negative spikes' in graphs or logs. 58 Default the error values are not suppressed to be backwards compaible. 59 Added **#ifndef** around **DHTLIB_INVALID_VALUE** so the default -999 can be overruled 60 compile time to set another error value e.g. -127 or -1 whatever suits the project. 61 14. (0.3.3) 62 Refactored the low level **readSensor()** as the **BIT SHIFT ERROR** issue #29 and issue #11 popped up again. 63 It was reproduced "efficiently" with an ESP32 and by using long wires. 64 Fixed with an explicit digitalWrite(datapin, HIGH) + delayMicroseconds() to have enough time between 65 pulling the line HIGH and poiling for the line LOW. 66 15. (0.3.4) 67 Added **waitFor(state, timeout)** to more precisely follow the datasheet in terms of timing. 68 Reintroduced the **interrupt enable/disable flag** as forced noInterrupts() 69 could break the timing of the DHT protocol / micros() - seen on AVR. 70 16. (0.4.0) 71 Added **DHTLIB_WAITING_FOR_READ** as return value of read => minor break of interface 72 17. (0.4.1) 73 Added Arduino-CI support + **gettype()** now tries to determine type if not known. 74 18. (0.4.2) 75 Fix negative temperatures. Tested with DHTNew_debug.ino and hexdump in .cpp and a freezer. 76 Note: testing in a freezer is not so good for humidity readings. 77 19. (0.4.3) 78 Added **reset()** to reset internal variables when a sensor blocks this might help. 79 Added **lastRead()** to return time the sensor is last read. (in millis). 80 81 82 ## DHT PIN layout from left to right 83 84 | FRONT | | DESCRIPTION | 85 |:----|:----:|:----| 86 | pin 1 | | VCC | 87 | pin 2 | | DATA | 88 | pin 3 | | Not Connected | 89 | pin 4 | | GND | 90 91 92 ## Specification DHT22 93 94 | Model | DHT22 | Notes | 95 |:----|:----|:----| 96 | Power supply | 3.3 - 6 V DC | 97 | Output signal | digital signal via single-bus | 98 | Sensing element | Polymer capacitor | 99 | Operating range | humidity 0-100% RH | temperature -40~80° Celsius | 100 | Accuracy humidity | ±2% RH(Max ±5% RH) | temperature < ±0.5° Celsius | 101 | Resolution or sensitivity | humidity 0.1% RH | temperature 0.1° Celsius | 102 | Repeatability humidity | ±1% RH | temperature ±0.2° Celsius | 103 | Humidity hysteresis | ±0.3% RH | 104 | Long-term Stability | ±0.5% RH/year | 105 | Sensing period | Average: 2s | 106 | Interchangeability | fully interchangeable | 107 | Dimensions | small 14 x 18 x 5.5 mm | big 22 x 28 x 5 mm | 108 109 110 ## Interface 111 112 113 ### Constructor 114 115 - **DHTNEW(uint8_t pin)** defines the datapin of the sensor. 116 - **reset()** might help to reset a sensor behaving badly. 117 - **getType()** 0 = unknown, 11 or 22. 118 In case of 0, **getType()** will try to determine type. 119 - **setType(uint8_t type = 0)** allows to force the type of the sensor. 120 121 122 ### Base interface 123 124 - **read()** reads a new temperature and humidity from the sensor 125 - **lastRead()** returns milliseconds since last **read()** 126 - **getHumidity()** returns last read value (float) or -999 in case of error. 127 Note this error value can be suppressed by **setSuppressError(bool)**. 128 - **getTemperature()** returns last read value (float) or -999 in case of error. Note this error value can be suppressed by **setSuppressError(bool)**. 129 130 131 ### Offset 132 133 Adding offsets works well in normal range however they might introduce under- or overflow at the ends of the sensor range. 134 135 - **setHumOffset(float offset)** typical < ±5% RH. 136 - **setTempOffset(float offset)** typical < ±2°C. 137 - **getHumOffset()** idem. 138 - **getTempOffset()** idem. 139 140 141 ### Control 142 143 Functions to adjust the communication with the sensor. 144 145 - **setDisableIRQ(bool b )** allows or suppresses interrupts during core read function to keep timing as correct as possible. **Note AVR only** 146 - **getDisableIRQ()** returns the above setting. Default **false** 147 - **setWaitForReading(bool b )** flag to enforce a blocking wait. 148 - **getWaitForReading()** returns the above setting. 149 - **setReadDelay(uint16_t rd = 0)** To tune the time it waits before actual read. This reduces the blocking time. Default depends on type. 1000 ms (dht11) or 2000 ms (dht22). set readDelay to 0 will reset to datasheet values AFTER a call to **read()**. 150 - **getReadDelay()** returns the above setting. 151 - **powerDown()** pulls datapin down to reduce power consumption 152 - **powerUp()** restarts the sensor, note one must wait up to two seconds. 153 - **setSuppressError(bool b)** suppress error values of -999 => check return value of read() instead. 154 - **getSuppressError()** returns the above setting. 155 156 157 ## Operation 158 159 See examples 160 161 If consistent problems occur with reading a sensor, one should allow interrupts 162 **DHT.setDisableIRQ(true)** 163 164 165 ## ESP8266 & DHT22 166 167 - The DHT22 sensor has some problems in combination with specific pins of the ESP8266. See more details 168 - https://github.com/RobTillaart/DHTNew/issues/31 (message jan 3, 2021) 169 - https://github.com/arendst/Tasmota/issues/3522 170