Yonghwan Shin
Yonghwan Shin

Reputation: 89

How can I get accurate value of temperature and humidity from DHT11 and ESP8266

I would like to measure the temperature and humidity from DHT11 sensor and ESP8266.
To do so, I made some code by using two libraries.

But none of these can give me an accurate number of humidity and temperature value.
All I can get an output are like the following:

Temperature = 21.0000
Humidity = 48.0000

First code

#include <DHTesp.h>

DHTesp dht;

void setup(){

  Serial.begin(9600);
  pinMode(0, INPUT);
  dht.setup(0, DHTesp::DHT11);
}
void loop(){
  Serial.print("Temperature = ");
  Serial.println(dht.getTemperature(), 4);
  Serial.print("Humidity = ");
  Serial.println(dht.getHumidity(), 4);
  delay(1000);
}

Second code

#include <DHT.h>
DHT dht(0, DHT11);


void setup() {
  Serial.begin(9600);
  pinMode(0, INPUT);
  dht.begin();
}

void loop() {
 
    float humidity = dht.readHumidity();
    float temp = dht.readTemperature();
    delay(500);
    Serial.println(humidity);

}

Upvotes: -1

Views: 1504

Answers (1)

Alan Cheung
Alan Cheung

Reputation: 243

From DHT11 datasheet, it only supports resolution to 1 degree Celsius and 8bits for humidity leading to the integer like floats.

Also something to keep in mind is the units that the DHT11 and DHT22 output.

Temperature is returned as Celsius and humidity is a value between 0 - 100 representing the relative humidity.

To convert between Celsius and Fahrenheit, you can use this formula

Serial.print((int)round(1.8*temp+32));

Upvotes: 2

Related Questions