Hello everyone.
After three days trying in many ways how to use correctly the DHT11 with no luck, I open a new post. I would like help from this forum.
Tests: all the combination from the below:
I own (and I have tested all of them):
- 3 DHT11 sensors.
- 2 Am2302 (DHT22) sensors.
- Multiple wires.
- 1 breadboard (I tried with and without breadboard).
- 2 official Arduino UNO r3 + 1 Funduino Uno.
And I have tried 6 different libraries:
- DHT-sensor-library-1.3.0 + Adafruit_Sensor-1.0.2: by Adafruit Industries, using DHT and DHT_U.
- arduino-DHT-master: by Mark Ruys (2013), using DHT.
- SimpleDHT: by winlin (2016), using SimpleDHT. (DHT11 only).
- dht11: by George Hadjikyriacou (0.4.1). (DHT11 only)
- DHTstable: by Rob Tillaart (0.2.4)
- DHTlib: by Rob Tillaart (0.1.28)
I am tired of trying every possible combination. It does not matter the board, the component, the model (11 or 22), the pin connected (I even tried to change the V-Data pins), changing the max timeout, the library... it always fails.
Even my "own" library as a experiment, modifying times, delays, quering methods (more polling), different advices found on the Web, reading issues in the GitHub libraries, etc. With zero results.
Of course, those devices work in the basic Blink example, can use the Serial, led+resistor in breadboard, etc. It is everything related with the DHT11 or 22.
Example of output from one of this libraries:
Max clock cycles: 16000
DHTxx Unified Sensor Example
------------------------------------
Temperature
Sensor: DHT11
Driver Ver: 1
Unique ID: -1
Max Value: 50.00 *C
Min Value: 0.00 *C
Resolution: 2.00 *C
------------------------------------
------------------------------------
Humidity
Sensor: DHT11
Driver Ver: 1
Unique ID: -1
Max Value: 80.00%
Min Value: 20.00%
Resolution: 5.00%
------------------------------------
Timeout waiting for start signal low pulse.
Error reading temperature!
Error reading humidity!
Error reading temperature!
Error reading humidity!
Timeout waiting for start signal low pulse.
...
What could be wrong?
My own test If I connect the data pin from the DHT11 with 2 wires to pin 7 and 8 of Arduino:
#include <Arduino.h>
const int DHTLIB_OK = 0;
const int DHTLIB_ERROR_CHECKSUM = -1;
const int DHTLIB_ERROR_TIMEOUT = -2;
const int DHTLIB_INVALID_VALUE = -999;
const int DHTLIB_TIMEOUT = 30000;
class dht {
public:
int test11(uint8_t pin, uint8_t pinWr); // DHT11 & DHT12
float humidity;
float temperature;
bool getDisableIRQ() { return _disableIRQ; };
void setDisableIRQ(bool b) { _disableIRQ = b; };
private:
uint8_t bits[5]; // buffer to receive data
int _readSensor(uint8_t pin, uint8_t wakeupDelay);
int _querySensor(uint8_t pin, uint8_t pinWr);
bool _disableIRQ = false;
};
int dht::test11(uint8_t pin, uint8_t pinWr) {
// READ VALUES
if (_disableIRQ)
noInterrupts();
int rv = _querySensor(pin, pinWr);
if (_disableIRQ)
interrupts();
if (rv != DHTLIB_OK) {
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv;
}
// CONVERT AND STORE
humidity = bits[0] + bits[1] * 0.1;
temperature = (bits[2] & 0x7F) + bits[3] * 0.1;
if (bits[2] & 0x80) // negative temperature
{
temperature = -temperature;
}
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum) {
return DHTLIB_ERROR_CHECKSUM;
}
return DHTLIB_OK;
}
int dht::_querySensor(uint8_t pin, uint8_t pinWr) {
int ret = 0;
// uint8_t pinWr = 8;
// uint8_t pin = 7;
pinWr = 8;
pin = 7;
// EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++)
bits[i] = 0;
// noInterrupts();
delay(2000);
pinMode(pin, INPUT);
pinMode(pinWr, OUTPUT);
digitalWrite(pinWr, HIGH);
delay(1000);
// pinMode(pin, INPUT);
// Serial.print(digitalRead(pin));
// delay(5);
// Serial.print(digitalRead(pin));
// REQUEST SAMPLE
pinMode(pinWr, OUTPUT);
digitalWrite(pinWr, LOW);
delay(18);
digitalWrite(pinWr, HIGH);
// pinMode(pin, INPUT_PULLUP);
// pinMode(pin, INPUT);
// delayMicroseconds(30);
// Serial.print(digitalRead(pin));
// delayMicroseconds(40);
// Serial.print(digitalRead(pin));
uint32_t loopCnt;
uint32_t t1, t2;
loopCnt = DHTLIB_TIMEOUT;
t1 = micros();
while (digitalRead(pin) == HIGH) {
if (--loopCnt == 0) {
ret = DHTLIB_ERROR_TIMEOUT;
break;
}
}
t2 = micros();
Serial.print(t2 - t1);
Serial.print("us ");
if (ret != 0) {
Serial.print("Time L1 ");
return ret;
}
loopCnt = DHTLIB_TIMEOUT;
t1 = micros();
while (digitalRead(pin) == LOW) {
if (--loopCnt == 0) {
ret = DHTLIB_ERROR_TIMEOUT;
break;
}
}
t2 = micros();
Serial.print(t2 - t1);
Serial.print("us ");
Serial.print(DHTLIB_TIMEOUT - loopCnt);
Serial.print(" ");
if (ret != 0) {
Serial.print("Time H1 ");
return ret;
}
t2 = micros();
loopCnt = DHTLIB_TIMEOUT;
t1 = micros();
while (digitalRead(pin) == HIGH) {
if (--loopCnt == 0) {
ret = DHTLIB_ERROR_TIMEOUT;
break;
}
delayMicroseconds(5);
}
t2 = micros();
Serial.print(t2 - t1);
Serial.print("us ");
Serial.print(DHTLIB_TIMEOUT - loopCnt);
Serial.print(" ");
if (ret != 0) {
Serial.print("Time L2 ");
return ret;
}
Serial.println();
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (uint8_t i = 40; i != 0; i--) {
Serial.print(i);
Serial.print(" L ");
loopCnt = DHTLIB_TIMEOUT;
t1 = t2;
while (digitalRead(pin) == LOW) {
if (--loopCnt == 0) {
ret = DHTLIB_ERROR_TIMEOUT;
break;
}
}
t2 = micros();
Serial.print(t2 - t1);
Serial.print("us ");
Serial.print(" ");
Serial.print(DHTLIB_TIMEOUT - loopCnt);
Serial.print(" ");
if (ret != 0) {
Serial.print("Time Hx ");
return ret;
}
uint8_t mask = 128;
uint8_t idx = 0;
uint32_t t = micros();
Serial.print("H ");
loopCnt = DHTLIB_TIMEOUT;
t1 = t2;
while (digitalRead(pin) == HIGH) {
if (--loopCnt == 0) {
ret = DHTLIB_ERROR_TIMEOUT;
break;
}
}
t2 = micros();
Serial.print(t2 - t1);
Serial.print("us ");
Serial.print(" ");
Serial.print(DHTLIB_TIMEOUT - loopCnt);
Serial.print(" ");
if (ret != 0) {
Serial.print("Time Lx ");
return ret;
}
if ((micros() - t) > 40) {
Serial.print("|");
bits[idx] |= mask;
}
mask >>= 1;
if (mask == 0) // next byte?
{
mask = 128;
idx++;
}
Serial.println();
}
return DHTLIB_OK;
}
void setup(){
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
delay(1000);
}
void loop(){
Serial.print("<");
Serial.print(DHTLIB_TIMEOUT);
Serial.print(">");
int chk = DHT.test11(DHT11_PIN, DHT11_PINWR);
switch (chk) {
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
Serial.print(chk);
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(2000);
}
Serial output:
<30000>DHT11, 8us 124524us 30000 Time H1 Time out error, -999.0, -999.0
<30000>DHT11, 4us 124528us 30000 Time H1 Time out error, -999.0, -999.0
8 or 4us could be sort of OK for the protocol, because the DHT11 should put it LOW in between 20-40us.
Then, it fails to put it HIGH (Time H1, reaches 124ms, 30000 loops), when it was expected to be done in 80us.