Problems with reading DHT11

Hello everyone. I am looking for some help with a DHT11 sensor. This sensor is part of a Greenhouse Monitoring system that I have up and running successfully. However, I want to power off the sensor in between readings, which are taken every 30 minutes(ish).

I am running a script on a NodeMCU which has 3 moisture sensors and the DHT11. I am using a transistor (2N2222) to turn the moisture sensors on and off and wanted to add the DHT11 so it too is powered on/off at the same time.

I had not been able to get this to work in any way in the bigger build so I have moved to a test build on my bench with a NodeMCU, DHT11, a couple of LED's and the 2N2222. I can get the T&H readings no problem when the VCC is connected to 3v pin. But moving the supply to a Digital pin or transistor results in the standard "Failed to read from DHT sensor!" message. The LED shows that the sensor is powered up, but it fails to read.

Here is my cut down code script. Works fine when DHT is powered from 3v pin of NMCU (NodeMCU DHT FT Power.png) but not when connecting VCC directly to D5 ((NodeMCU DHT SW Power.png) tried other pins too) or connecting ground through the transistor, it fails to read.

#include "DHT.h"
#define DHTPIN 12     // Digital pin connected to the DHT sensor
#define TRANS D5
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void TransOn()
{
  delay(100);
  digitalWrite(TRANS, HIGH);
  Serial.println("DHT On");
  delay(2000);
}

void TransOff()
{
  delay(100);
  digitalWrite(TRANS, LOW);
  Serial.println("DHT Off");
  delay(10000); //delay to next SensorRead run
}


void SensorRead()
{  
  delay(3500);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.println(t);
}

void setup() {
 Serial.begin(9600);
 dht.begin();
 pinMode(TRANS, OUTPUT);
 pinMode(DHTPIN, INPUT);
}

void loop() 
{
TransOn();
SensorRead();
TransOff();
}

I have tried various permutations of coding and physical setup but it seems that what I thought should be a simple adaptation and having the ability to turn the DHT on and off is in somewhat complicated by something I have failed to understand or overlooked. Any help would be most appreciated.

Many Thanks

I should add that a pullup resistor across VCC/Data makes no difference and have tried the same setup with a DHT11 module instead of the bare DHT11 all without success.

If you switch the DHT off then back on, you may have to do "DHT.begin" again and wait for it to initialize?

JCA34F:
If you switch the DHT off then back on, you may have to do "DHT.begin" again and wait for it to initialize?

Thanks for that. So I moved dht.begin out of Setup() and into SensorRead() before the initial delay;

void SensorRead()
{  
  dht.begin();
  delay(3500);

Unfortunately, it makes no difference and still produces the same error. With the LED's in the circuit I can see that the D5 pin is going HIGH and the sensor is powered on and therefore should read, I just can't get my head around why it gives the error when operating like this, but when connected to 3v instead of D5, it works fine.
I have added some extra serial prints through the sketch to see if it would help identify where it's going wrong. The output looks like this;

13:29:58.686 -> DHT Begin
13:29:58.686 -> Delay 3.5s
13:30:02.166 -> Floats
13:30:02.271 -> Failed to read from DHT sensor!
13:30:02.271 -> nan
13:30:02.271 -> nan
13:30:02.339 -> DHT Off

So you can see that the dht.begin is running and it's tried to read the sensor but the values returned are Not A Number (nan). This very frustrating!!

Any further ideas?

I was reading over a datasheet I found DHT sensor (attached below) and found the following info:

◎Peripheral reading step
Communication between master and slave can be completed by the following steps (peripherals
(such as a microprocessor) to read step DHT11 data).

Step one:
DHT11 after power (power after DHT11 1S to wait to cross the unstable state during this period
can’t send any commands), test environment temperature and humidity data, and record data while
the data lines DATA DHT11 pulled by a pull-up resistor remains high; DHT11 this time the DATA
pin is the input state, always detect external signals.

Step two:
Microprocessor I / O output while the output is set to low, and low retention time can’t be less
than 18ms, then the microprocessor I / O is set to enter the state, due to the pull-up resistor, the
microprocessor I / O that the data lines DHT11 also will go high, waiting to answer DHT11 signals
transmitted signal as shown: (See images in datasheet attached below)
The host sends a start signal

Step three:
DHT11 the DATA pin when external signals detected low, waiting for the external signal low
end, after a delay DHT11 the DATA pin is an output, the output low as 80 microseconds response
signal, followed by the output of 80 micro-notify the second high peripheral is ready to receive data,
the microprocessor I / O at this time in the input state detecting I / O with low (DHT11 echo signal)
to the wait for 80 microseconds high data receiving and sending signals as shown: (See images in datasheet attached below)

Step four:
The 40 bit data output by the DHT11 DATA pin, the microprocessor according to the change of
I/O level receive 40 bits of data, a data format of "0": high level and low level of 50 microseconds
and 26-28 microsecond, format data "1": low level 50 microsecond plus 70 microsecond high. Bit
data "0", "1" format signal as shown in fig: (See images in datasheet attached below)
End signal:
DHT11 the DATA pin output 40-bit data, the continued output low 50 microseconds after the
entry into the state, due to the pull-up resistor attendant goes high. But DHT11 temperature and
humidity inside the test-retest data, and record the data, awaiting the arrival of an external signal

Now I make no pretence in fully understanding the above, but I think I have a basic grasp of it. But this raises the question; to be able to power off the sensor when it is not being read, when I power it back up, do I need to do some fancy switching of the data pin High and Low to effectively tell it to reset? If so does anyone have any idea what needs to be done?

DHT11-Temperature-Sensor Data Sheet.pdf (670 KB)

Hi all, I am sorry it has been a while since I last updated this thread with my progress; I have been quite busy with work.

I thought I would let you know that I have now solved the problem and have the GreenHouse monitor up and running using a transistor to switch both the soil moisture sensors and the DHT sensor on and off. Not only that but I have implemented the Deep Sleep mode between reads to increase battery life (I was getting about 2 - 2.5 days out of a 6v Lead Acid battery).

I ended up doing a bit more learning and testing and failing and re-learning etc. The final solution came in the form of using the existing NPN transistor that was switching the soil sensors on and off and using its output to switch a PNP transistor to send the 5v to the DHT. I think the required resistor on the N side may have been the source of my initial failure as once I had read more, done some calculations and a test build or two, I got it working and reading the DHT correctly.

I changed the sketch around a bit to incorporate the Deep Sleep and it is currently running in the greenhouse giving me sensible reads etc.

I would like to thank all those offered info and solutions.

Good work! And a Karma point for persistence. :slight_smile: