DHT11 Temperature & Humidity Sensor - Microcontroller

DHT11 Temperature & Humidity Sensor - Microcontroller

·

1 min read

I had a tough time getting the ESP8266 Microcontroller to read from a DHT11 sensor properly, mainly due to small errors in my code and process.

Learnings Along the Way:

  • setting the I/O pin number to '8' was not working and giving a nan reading. Changing to 'D8' however worked.
  • I am not able to upload a script while the input pin (D8) is connected to the board. Arduino IDE throws an error that it either can't find the port or it's in use.
    • First I had to disconnect the D8 pin from the board, leaving the ground and power pins in.
      • Then I'm able to upload the script to the microcontroller
      • After uploading, I reconnect the input pin to the microcontroller. I'm able to do this while the board has usb power
  • passing true in dht.readTemperature(true); changes the output to Fahrenheit from Celsius

Working code:

#include "DHT.h"
#define DHTTYPE DHT11 //change to DHT22 if needed

DHT dht(D8,DHTTYPE); 
void setup(void)
{

  dht.begin();
  Serial.begin(115200);
  Serial.println("setup");
  delay(1000);//Wait before accessing Sensor

}//end "setup()"

void loop() {

  float h =dht.readHumidity();
  float t =dht.readTemperature(true);

  Serial.print("Current Humidity = ");
  Serial.print(h);
  Serial.print("% ");
  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(800);
}