DHT11 Sensor Review and code examples

The DHT11 sensor works as a basic temperature and humidity sensor at a low price. In this post I will review the sensor, show how it can be used and give a final judgement on it.

What can the DHT11 do?

The DHT11 sensor is a basic sensor that lets you measure the current temperature and humidity. This chip combines a thermistor and a humidity sensor and allows you to read both at through its digital output.

There are a number of datasheets for the DHT11 on the internet and some of them disagree so depending on where you buy yours from you may want to check the specific details from their website. A short summary of the datasheet is below.

 

 

Datasheet Specifications
Voltage 3V to 5V (some datasheets say 5.5 V)
Humidity range  20% to 80% relative humidity (some datasheets claim up to 95%)
Humidity accuracy ~5%
Temperature range 0 to 50ºC
Temperature accuracy ± 2ºC
Sample Rate 1 Hz (Once every second)

The key useful points here is that it operates in the 3 to 5 volt range. This makes it perfectly suitable for any home electronics projects (that typically operate on 3.3 or 5 volts).

One of the key things to remember when using a DHT11 is that the temperature is only accurate to plus or minus 2 degrees. This means if it reports  20 degrees its possible that the real temperature is between 18 degrees and 22 degrees. Similarly the humidity range reports a 5% accuracy which equates to a ± 2.5% accuracy.

Wiring it up to an Arduino

To test this sensor I am going to wire it up to an Arduino Uno. This will record the temperature and humidity over time so that I may review the data. For this test I am going to leave it on in an air conditioned lab for over 24 hours and check the data.

I have wired up the DHT11 as shown on the left.

The 10K resistor helps to pull the signal high where required which improves the signal received from the sensor. Having some form of pull up resistor is recommended for this sensor. This will ensure that if there are periods where the sensor is left floating its not likely to cause issues by sending data which can be interpreted as valid data. Depending on the sensor library used the pull up resistor may be more or less important.

 

 

Programming the Arduino with the DHT11

To talk to the DHT11 sensor I am going to use adafruit DHT library. This means that I don’t need to manually program the DHT11 protocol which requires a bit of finesse. This can be obtained by adding the “DHT sensor library” in the “manage library” section of the Arduino IDE or downloading it from github.

The data will be output in JSON so that I may easily programatically parse it on my computer. The computer will keep track of the data received and the time and allow me to plot the data.

Below is the source code I used for my test. Here you can see I define the DHTTYPE as DHT11 but this can be used for a DHT22 sensor as well. This library simplifies using the sensor as you have two helper methods readHumidity and readTemperature. Using these will then return the sensor readings as a floating point number (a decimal).

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    dht.begin();
}

void loop() {
    float h = dht.readHumidity();
    float t = dht.readTemperature();

    Serial.print("{\"humidity\": ");
    Serial.print(h);
    Serial.print(", \"temp\": ");
    Serial.print(t);
    Serial.print("}\n");

    delay(2000);
}

Using the above code I can now start to take measurements from the sensor and analyse the data I have obtained. I wait at the end of the loop so I only take a reading every two seconds.

Practical Measurements

Using python I set up the DHT11 temperature sensor in an air conditioned room. In this room the air conditioning was set to 23C.

During the period of the experiment it was cold outside which was slowly reducing the humidity of the room.

Here with the humidity measurements you can see that it was slowly dropping over the period of the experiment.

There were times when the humidity detected was oscillating between two values. It would be nice if there was more resolution on the humidity sensor so this didn’t occur as frequently.

The temperature stayed relatively constant during the period of the experiment as shown on the graph.

Again it would be useful if the sensor had a little more resolution in the measurements. This is more obvious on the temperature measurements as there were a lot of periods where it oscillated between two values.

The resolution of 1 degree however may be fine for experiments where a wider range of temperatures are likely to be recorded and precision isn’t too important.

 

Final Review

Overall this sensor is a very cheap and useful little sensor. The only negative point is that its temperature resolution is lower than some of its more expensive cousins. However since this is on the cheapest end of the temperature sensor and provides a humdity sensor as well this is definitely value for money. If you are looking for something a little more accurate you should have a look at the DHT22 which provides a very similar interface with a bit more accuracy.

Would buy again!

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.