diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e6913..28c09b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.4.21] - 2024-10-09 +- add dhtnew_pulse_diag_ext.ino for extended diagnosis. + ## [0.4.20] - 2024-03-24 - fix #95, units of T & H - update GitHub actions @@ -14,7 +17,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - update unit test (minor) - minor edits - ## [0.4.19] - 2023-10-25 - update readme.md diff --git a/README.md b/README.md index ec3b727..9f08458 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,11 @@ Note there are differences e.g. DHT11 has no negative temperature, no decimals, The DHTNew library returns Temperature in degrees Celsius and Humidity in 0.0 - 100.0 %RH. For converting temperature to Fahrenheit or Kelvin, see https://github.com/RobTillaart/Temperature. +For diagnosis if a DHT sensor gives strange readings one can use **dhtnew_pulse_diag_ext.ino** +to compare timing with the datasheet. -#### Sonoff Si7021 + +### Sonoff Si7021 Since 0.4.14 there is **experimental** support for the Sonoff Si7021. No hardware yet to test this myself, but it is confirmed to work. @@ -39,7 +42,7 @@ To use the library one should call **setType(70)**. Feedback (both positive and negative) about the Sonoff Si7021 sensors is welcome. -#### AM2320, AM2321 and AM2322 +### AM2320, AM2321 and AM2322 Since 0.4.18 there is **experimental** support for the AM2320, AM2321 and AM2322. Not tested myself, but AM2320 is confirmed to work, see https://github.com/RobTillaart/AM232X/issues/26 @@ -54,7 +57,7 @@ Feedback (both positive and negative) about the AM232X sensors is welcome. **Note: check the datasheet how to connect!** -#### Related +### Related - https://github.com/RobTillaart/DHTNew - https://github.com/RobTillaart/DHTStable @@ -174,7 +177,7 @@ This is used to keep spikes out of your plotter / graphs / logs. See examples -#### TIME_OUT +### TIME_OUT If consistent TIMOUT_C or TIMEOUT_D occur during reading a sensor, one could try if allowing interrupts solves the issue **DHT.setDisableIRQ(false)**. @@ -188,7 +191,7 @@ In version 0.4.10 the TIMEOUT_C is extended from 70-90 us to even suppress the T even more. See discussion and tests in https://github.com/RobTillaart/DHTNew/issues/67. -#### Serial +### Serial The MKR1010Wifi board need to wait for Serial at startup if you want to monitor it from the IDE. Adding the line ```while(!Serial):``` fixes this. (added to the examples). @@ -196,14 +199,14 @@ from the IDE. Adding the line ```while(!Serial):``` fixes this. (added to the ex There might be more boards that need this line to work properly. -#### DHT22 and ESP8266 +### DHT22 and ESP8266 - The DHT22 sensor has some problems in combination with specific pins of the ESP8266. See more details - https://github.com/RobTillaart/DHTNew/issues/31 (message Jan 3, 2021) - https://github.com/arendst/Tasmota/issues/3522 -#### Voltage AM2301 and ESP8266 +### Voltage AM2301 and ESP8266 In a test an AM2301 had problems giving no humidity (99.9% overflow) when the DHTStable library was used with an ESP8266. (Reported by mail, no GH issue). @@ -313,10 +316,12 @@ Fix #84 correct the reading of type 70 for Sonoff Si7021. fix #86, define constants explicit as float. 34. (0.4.18) Update readme.md and library.\* about support for AM2320/21/22. -35. (0.4.19) +35. (0.4.19) Update readme.md -36. (0.4.20) +36. (0.4.20) Update GitHub actions and readme.md +36. (0.4.21) +Add dhtnew_pulse_diag_ext.ino ## Future diff --git a/dhtnew.cpp b/dhtnew.cpp index 96d19a4..b97fed6 100644 --- a/dhtnew.cpp +++ b/dhtnew.cpp @@ -1,7 +1,7 @@ // // FILE: dhtnew.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.4.20 +// VERSION: 0.4.21 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // URL: https://github.com/RobTillaart/DHTNEW // diff --git a/dhtnew.h b/dhtnew.h index 5d50dd5..f57dcf4 100644 --- a/dhtnew.h +++ b/dhtnew.h @@ -2,7 +2,7 @@ // // FILE: dhtnew.h // AUTHOR: Rob Tillaart -// VERSION: 0.4.20 +// VERSION: 0.4.21 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // URL: https://github.com/RobTillaart/DHTNEW // @@ -18,7 +18,7 @@ #include "Arduino.h" -#define DHTNEW_LIB_VERSION (F("0.4.20")) +#define DHTNEW_LIB_VERSION (F("0.4.21")) #define DHTLIB_OK 0 diff --git a/examples/dhtnew_pulse_diag_ext/dhtnew_pulse_diag_ext.ino b/examples/dhtnew_pulse_diag_ext/dhtnew_pulse_diag_ext.ino new file mode 100644 index 0000000..a6c2f65 --- /dev/null +++ b/examples/dhtnew_pulse_diag_ext/dhtnew_pulse_diag_ext.ino @@ -0,0 +1,262 @@ +// +// FILE: dhtnew_pulse_diag_ext.ino +// AUTHOR: Rob Tillaart +// PURPOSE: DHTNEW library pulse measurement tool - diagnostics +// URL: https://github.com/RobTillaart/DHTNew + +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND + + +#include "Arduino.h" + +#define DHTLIB_DHT11_WAKEUP 18 +#define DHTLIB_DHT_WAKEUP 1 + +#define BIT_THRESHOLD 50 + + +uint8_t _dataPin = 5; +uint8_t _wakeupDelay = DHTLIB_DHT_WAKEUP; + +uint16_t count = 0; +uint32_t times[100], t = 0; +uint8_t idx = 0; +uint32_t value; + +void setup() +{ + while (!Serial); // MKR1010 needs this + + Serial.begin(115200); + Serial.println("dhtnew_pulse_diag_ext.ino"); + Serial.println(); + + // default should be HIGH + pinMode(_dataPin, OUTPUT); + digitalWrite(_dataPin, HIGH); +} + + +void loop() +{ + measure(); + dump(); + delay(5000); +} + + +void dump() +{ + int i = 1; + Serial.println(); + Serial.print("RUN:\t"); Serial.println(count); + Serial.print("IDX:\t"); Serial.println(idx); + + Serial.println("WAKEUP"); + for (int n = 0; n < 6; n++) + { + Serial.print("\t"); + Serial.print(times[i++]); + } + Serial.println(); + + Serial.println("HUM"); + uint16_t raw_humidity = 0; + for (int n = 0; n < 32; n++) + { + Serial.print("\t"); + if (n % 2) + { + raw_humidity <<= 1; + if (times[i] > BIT_THRESHOLD) raw_humidity |= 1; + } + Serial.print(times[i++]); + if ((n & 15) == 15) Serial.println(); + } + Serial.print("RAW H:\t0b"); + Serial.print(raw_humidity, BIN); + Serial.print(" = 0x"); + Serial.print(raw_humidity, HEX); + Serial.print(" = "); + Serial.print(raw_humidity, DEC); + Serial.print(" = "); + Serial.print(raw_humidity * 0.1, 1); + Serial.print("%"); + Serial.println(); + Serial.println(); + + Serial.println("TEMP"); + uint16_t raw_temperature = 0; + for (int n = 0; n < 32; n++) + { + Serial.print("\t"); + if (n % 2) + { + raw_temperature <<= 1; + if (times[i] > BIT_THRESHOLD) raw_temperature |= 1; + } + Serial.print(times[i++]); + if ((n & 15) == 15) Serial.println(); + } + Serial.print("RAW T:\t0b"); + Serial.print(raw_temperature, BIN); + Serial.print(" = 0x"); + Serial.print(raw_temperature, HEX); + Serial.print(" = "); + Serial.print(raw_temperature, DEC); + Serial.print(" = "); + Serial.print(raw_temperature * 0.1, 1); + Serial.print("C"); + Serial.println(); + Serial.println(); + + Serial.println("CRC"); + uint8_t raw_CRC = 0; + for (int n = 0; n < 16; n++) + { + Serial.print("\t"); + if (n % 2) + { + raw_CRC <<= 1; + if (times[i] > BIT_THRESHOLD) raw_CRC |= 1; + } + Serial.print(times[i++]); + if ((n & 7) == 7) Serial.println(); + } + Serial.print("CRC:\t0b"); + Serial.print(raw_CRC, BIN); + Serial.print(" = "); + Serial.print(raw_CRC, DEC); + Serial.print(" <=> "); + uint8_t calc_crc = 0; + calc_crc += (raw_humidity >> 8) + (raw_humidity & 0xFF); + calc_crc += (raw_temperature >> 8) + (raw_temperature & 0xFF); + Serial.print(calc_crc, DEC); + Serial.println(); + Serial.println(); + Serial.println(); + + Serial.println("BYE"); + for (int n = 0; n < 2; n++) + { + Serial.print("\t"); + Serial.print(times[i++]); + } + Serial.println(); + Serial.println(); +} + + +void measure() +{ + Serial.print(F("\n===================================\n")); + + count++; + // yield(); // handle pending interrupts + + // reset measurements table + idx = 0; + t = micros(); + for (int i = 0; i < 100; i++) times[i] = 0; + + times[idx++] = micros(); + // REQUEST SAMPLE - SEND WAKEUP TO SENSOR + pinMode(_dataPin, OUTPUT); + digitalWrite(_dataPin, LOW); + // add 10% extra for timing inaccuracies in sensor. + delayMicroseconds(_wakeupDelay * 1100UL); + + Serial.print("awake "); + + times[idx++] = micros(); + // HOST GIVES CONTROL TO SENSOR + pinMode(_dataPin, INPUT_PULLUP); + + // DISABLE INTERRUPTS when clock in the bits + // noInterrupts(); // gives problems on AVR + + + times[idx++] = micros(); + // SENSOR PULLS LOW after 20-40 us => if stays HIGH ==> device not ready + while (digitalRead(_dataPin) == HIGH); + + Serial.print("2 "); + + times[idx++] = micros(); + // SENSOR STAYS LOW for ~80 us => or TIMEOUT + while (digitalRead(_dataPin) == LOW); + + Serial.print("3 "); + + + times[idx++] = micros(); + // SENSOR STAYS HIGH for ~80 us => or TIMEOUT + while (digitalRead(_dataPin) == HIGH); + times[idx++] = micros(); + + Serial.print("4 "); + + + // SENSOR HAS NOW SEND ACKNOWLEDGE ON WAKEUP + // NOW IT SENDS THE BITS + + // READ THE OUTPUT - 40 BITS => 5 BYTES + uint32_t start = micros(); + uint8_t i = 40; + for (i = 40; i != 0; i--) + { + times[idx++] = micros(); + // EACH BIT START WITH ~50 us LOW + while (digitalRead(_dataPin) == LOW) + { + if (micros() - start > 10000) + { + Serial.print(" <"); + Serial.println(i); + Serial.println("> "); + break; + } + } + + times[idx++] = micros(); + // DURATION OF HIGH DETERMINES 0 or 1 + // 26-28 us ==> 0 + // 70 us ==> 1 + while (digitalRead(_dataPin) == HIGH) + { + if (micros() - start > 10000) + { + Serial.println(i); + break; + } + } + } + + Serial.print("5 "); + + times[idx++] = micros(); + // After 40 bits the sensor pulls the line LOW for 50 us + // TODO: should we wait? + while (digitalRead(_dataPin) == LOW); + + Serial.print("6 "); + + times[idx++] = micros(); + times[idx++] = micros(); + + for (int n = 100; n > 0; n--) + { + times[n] -= times[n - 1]; + } + + Serial.println("7 "); +} + + +// -- END OF FILE -- diff --git a/examples/dhtnew_pulse_diag_ext/example_DHT22.txt b/examples/dhtnew_pulse_diag_ext/example_DHT22.txt new file mode 100644 index 0000000..0854ab8 --- /dev/null +++ b/examples/dhtnew_pulse_diag_ext/example_DHT22.txt @@ -0,0 +1,69 @@ +dhtnew_pulse_diag_ext.ino + + +=================================== +awake 2 3 4 14 +13 +12 +11 +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 +5 6 7 + +RUN: 1 +IDX: 89 +WAKEUP + 1280 8 76 56 60 24 +HUM + 20 80 52 76 64 24 52 36 44 36 44 28 52 28 52 28 + 52 24 52 28 72 64 72 16 56 68 52 80 52 68 52 80 +RAW H: 0b1100000000101111 = 0xC02F = 49199 = 4919.9% + +TEMP + 52 28 52 72 60 28 52 76 52 80 52 68 52 80 52 72 + 52 68 64 16 52 7340 8 152 12 144 12 136 12 144 12 92 +RAW T: 0b101111110111111 = 0x5FBF = 24511 = 2451.1C + +CRC + 20 92 12 92 12 92 12 100 + 16 96 8 96 8 96 8 124 +CRC: 0b11111111 = 255 <=> 13 + + +BYE + 28 4 + + +=================================== +awake 2 3 4 5 6 7 + +RUN: 2 +IDX: 89 +WAKEUP + 1296 12 44 76 60 24 +HUM + 36 20 68 20 52 24 56 24 52 28 52 36 44 76 52 28 + 60 80 52 24 56 68 52 88 44 72 52 88 44 68 60 16 +RAW H: 0b1010111110 = 0x2BE = 702 = 70.2% + +TEMP + 72 24 52 28 52 28 60 20 60 20 52 24 56 24 56 24 + 72 72 52 28 52 76 52 72 52 72 52 80 56 68 56 24 +RAW T: 0b10111110 = 0xBE = 190 = 19.0C + +CRC + 64 28 52 68 64 72 52 68 + 52 80 52 72 52 72 52 44 +CRC: 0b1111110 = 126 <=> 126 + + +BYE + 52 4 diff --git a/library.json b/library.json index 1445a46..878b32b 100644 --- a/library.json +++ b/library.json @@ -13,7 +13,7 @@ "type": "git", "url": "https://github.com/RobTillaart/DHTNEW.git" }, - "version": "0.4.20", + "version": "0.4.21", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index 66d3ec8..02fdeab 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DHTNEW -version=0.4.20 +version=0.4.21 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for DHT temperature and humidity sensor, with automatic sensortype recognition.