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.

