Interface DHT11 Module With Arduino

Want to keep a log of the climate in your greenhouse, build a humidor control system, or track temperature and humidity data for a weather station project? AOSONG’s DHT11 Temperature and Humidity sensor module could be the perfect fit for you!

This sensor is factory-calibrated and does not require any external components to function. With just a few connections and a bit of Arduino code, you can begin measuring relative humidity and temperature right away.

DHT11 Module Hardware Overview

At the heart of the module is the digital temperature and humidity sensor manufactured by AOSONG – DHT11.

DHT11 Sensor

DHT11 can measure temperature from 0°C to 50°C with a ±2.0°C accuracy, and humidity from 20 to 80% with a 5% accuracy.

DHT11 Module Hardware Overview Front

Note that the DHT11 has a sampling rate of 1Hz, which means it can provide new data once every second.

Supporting Circuitry

The module includes all of the necessary supporting circuitry, so it can be used straight out of the box.

DHT11 Module Hardware Overview Back

DHT11 sensors typically require an external 10K pull-up resistor on the output pin for proper communication between the sensor and the Arduino. However, because the module already includes a pull-up resistor, you do not need to add one.

The module also includes a decoupling capacitor for filtering power supply noise.

Inside the DHT11 Sensor

If you remove the sensor’s casing, you will find an NTC thermistor and a humidity sensing component inside.

Inside DHT11 Sensor Front

The humidity sensing component has two electrodes with a moisture-holding substrate (usually a salt or conductive plastic polymer) in between.

As the humidity rises, the substrate absorbs water vapor, resulting in the release of ions and a decrease in the resistance between the two electrodes.

This change in resistance is proportional to the humidity, which can be measured to estimate relative humidity.

Internal Structure of Humidity Sensor in DHT11 DHT22

DHt11 also includes a NTC thermistor for measuring temperature. A thermistor is a type of resistor whose resistance varies with temperature.

Technically, all resistors are thermistors in the sense that their resistance changes slightly with temperature, but this change is typically very small and difficult to measure. Thermistors are designed so that their resistance changes dramatically with temperature (by 100 ohms or more per degree). The term “NTC” stands for “Negative Temperature Coefficient,” which means that resistance decreases as temperature rises.

NTC Thermistor Temperature Resistance Characteristic Curve

The sensor also includes an 8-bit SOIC-14 packaged IC. This IC measures and processes the analog signal using stored calibration coefficients, converts the analog signal to digital, and outputs a digital signal containing the temperature and humidity.

Inside DHT11 Sensor Back

DHT11 Module Pinout

The DHT11 module is relatively simple to connect. There are only three pins:

DHT11 Module Pinout

+ (VCC) pin provides power to the sensor. Despite the fact that the supply voltage of the module ranges from 3.3V to 5.5V, a 5V supply is recommended. With a 5V power supply, the sensor can be placed up to 20 meters away. With 3.3V supply voltage, the sensor can be placed just 1 meter away; otherwise, the line voltage drop will cause measurement errors.

Out pin is used for communication between the sensor and the microcontroller.

– (GND) is the ground pin.

Wiring DHT11 Module to Arduino

Now it’s time to connect the DHT11 module to the Arduino!

Connections are relatively simple. Begin by connecting the + (VCC) pin to the Arduino’s 5V output and the – (GND) pin to ground. Finally, connect the Out pin to digital pin #8.

The diagram below shows how to connect everything.

Wiring DHT11 Module with Arduino

Installing DHT library

The DHT sensors has their own proprietary single-wire data transfer protocol. This protocol requires precise timing. We don’t have to worry too much about this, though, because we’ll be using the DHTlib library, which handles almost everything.

To install the library, navigate to Sketch > Include Library > Manage Libraries… Wait for the Library Manager to download the libraries index and update the list of installed libraries.

manage libraries

Filter your search by entering ‘dhtlib’.There should only be a single entry. Click on that and then choose Install.

dhtlib library installation

Arduino Example 1 – Displaying Readings on Serial Monitor

After installing the library, copy and paste this sketch into the Arduino IDE.

The following test sketch will print the temperature and relative humidity values to the serial monitor. Try out the sketch, and then we’ll go over it in more detail.

#include <dht.h>        // Include library
#define outPin 8        // Defines pin number to which the sensor is connected

dht DHT;                // Creates a DHT object

void setup() {
	Serial.begin(9600);
}

void loop() {
	int readData = DHT.read11(outPin);

	float t = DHT.temperature;        // Read temperature
	float h = DHT.humidity;           // Read humidity

	Serial.print("Temperature = ");
	Serial.print(t);
	Serial.print("°C | ");
	Serial.print((t*9.0)/5.0+32.0);        // Convert celsius to fahrenheit
	Serial.println("°F ");
	Serial.print("Humidity = ");
	Serial.print(h);
	Serial.println("% ");
	Serial.println("");

	delay(2000); // wait two seconds
}

After uploading the sketch, you should see the following output on the serial monitor.

DHT11 Module Output

Code Explanation:

The sketch begins by including the DHT library. Following that, we specify the Arduino pin number to which our sensor’s Data pin is connected and create a DHT object.

#include <dht.h>
#define outPin 8

dht DHT;

In the setup, we initialize the serial communication.

void setup() {
  Serial.begin(9600);
}

In the loop, we use the read11() function to read the DHT11 module. This function takes as a parameter the sensor’s Data pin number.

int readData = DHT.read11(outPin);

We can now retrieve the humidity and temperature values by accessing the DHT object’s properties using dot . notation.

float t = DHT.temperature;        // Read temperature
float h = DHT.humidity;           // Read humidity

The DHT object returns the temperature in degrees Celsius (°C). It is easy to convert to Fahrenheit (°F) using the following formula:

T(°F) = T(°C) × 9/5 + 32

Serial.print((t * 9.0) / 5.0 + 32.0);

Arduino Example 2 – Displaying Readings on LCD

If you’re constructing your own incubator or a similar project, you’ll need a 16×2 character LCD rather than a serial monitor to display the current temperature and humidity levels. So, in this example, we’ll also connect the LCD to the Arduino in addition to the DHT11 module.

This is what the output looks like.

DHT11 Module Output on LCD

If you are unfamiliar with 16×2 character LCDs, consider reading the tutorial below.

Wiring

Following that, connect the LCD as shown below.

Wiring DHT11 Module with Arduino and LCD

Arduino Code

The sketch below will display the temperature and relative humidity values on the 16×2 character LCD. This sketch is similar to the previous one, except that the values are printed on the LCD.

#include <LiquidCrystal.h>      // Include LiquidCrystal Library
#include <dht.h>

#define outPin 8

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an LCD object.
dht DHT;                // Create a DHT object

void setup() {
	lcd.begin(16,2); 	// Initialize the LCD
}

void loop() {
	int readData = DHT.read11(outPin);
	
	float t = DHT.temperature;
	float h = DHT.humidity;
	
	lcd.setCursor(0,0);
	lcd.print("Temp.: ");
	lcd.print(t);
	lcd.print((char)223);	//shows degrees character
	lcd.print("C");

	lcd.setCursor(0,1);
	lcd.print("Humi.: ");
	lcd.print(h);
	lcd.print("%");
	
	delay(2000);
}