Skip to content

Commit

Permalink
Add dhtnew_pulse_diag_ext.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Oct 9, 2024
1 parent cc46b2a commit 59cf4cb
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)**.
Expand All @@ -188,22 +191,22 @@ 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).

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).
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dhtnew.cpp
Original file line number Diff line number Diff line change
@@ -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
//
Expand Down
4 changes: 2 additions & 2 deletions dhtnew.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
Expand Down
262 changes: 262 additions & 0 deletions examples/dhtnew_pulse_diag_ext/dhtnew_pulse_diag_ext.ino
Original file line number Diff line number Diff line change
@@ -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 --
Loading

0 comments on commit 59cf4cb

Please sign in to comment.