Interfacing DHT11 and DHT22 Sensors 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 or DHT22 Temperature and Humidity Sensor could be the perfect fit for you!

These sensors are factory-calibrated and do 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.

They provide temperature and humidity readings accurate to within one decimal place, which is a plus. The only drawback is that they only provide new data every second or two, but for the price and performance, it’s hard to complain.

DHT11 vs DHT22

The DHT11 and the DHT22 are the two most widely used sensors in the DHTxx series. They look kind of the same and have the same pinout, but their specs are different.

Of the two, the DHT22 is more expensive and, undoubtedly, has better specifications. The DHT22 can measure temperatures from -40°C to +125°C with an accuracy of ±0.5°C, while the DHT11 can measure temperatures from 0°C to 50°C with an accuracy of ±2°C. In addition, the DHT22 sensor can measure relative humidity from 0 to 100% with an accuracy of 2-5%, while the DHT11 sensor can only measure relative humidity from 20 to 80% with an accuracy of 5%.

Here are the specifications:

DHT11 Temperature Humidity Sensor Fritzing part Illustration
DHT22 Temperature Humodoty Sensor Fritzing part Illustration
Operating Voltage3 to 5V3 to 5V
Max Operating Current2.5mA max2.5mA max
Humidity Range20-80% / 5%0-100% / 2-5%
Temperature Range0-50°C / ± 2°C-40 to 80°C / ± 0.5°C
Sampling Rate1 Hz (reading every second)0.5 Hz (reading every 2 seconds)
Body size15.5mm x 12mm x 5.5mm15.1mm x 25mm x 7.7mm
AdvantageUltra low costMore Accurate

Despite the fact that the DHT22 is more accurate, precise, and capable of operating in a wider range of temperature and humidity, there are three areas where the DHT11 completely outperforms the DHT22 – It is more affordable, more compact, and has a higher sampling rate. DHT11 takes a reading once per second (or 1Hz sampling rate), while DHT22 takes a reading once every two seconds (or 0.5Hz sampling rate).

Despite these differences, the operating voltage of both sensors ranges from 3 to 5 volts, with a maximum current of 2.5mA (during conversion). The best part is that DHT11 and DHT22 sensors are swappable, which means that if you build your project with one, you can simply unplug it and replace it with another. Your code may need to be tweaked slightly, but the wiring remains the same!

Inside the DHT Sensor

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

Inside DHT11 DHT22 AM2302 Temperature Humidity Sensor

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
Internal Structure of Humidity Sensor

The sensor 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 the 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
NTC Thermistor with 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.

DHT11 and DHT22 Pinout

The DHT11 and DHT22 sensors are both relatively simple to connect. They have four pins:

DHT11 DHT22 AM2302 Temperature Humidity Sensor Pinout

VCC pin provides power to the sensor. Despite the fact that the supply voltage 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 up to 1 meter away; otherwise, the line voltage drop will cause measurement errors.

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

NC Not connected

GND is the ground pin.

Wiring DHT11 and DHT22 Sensors to an Arduino

Now it’s time to connect the sensor to the Arduino!

Connecting DHT sensors to Arduino is straightforward. They have fairly long 0.1′′-pitch pins, allowing them to be easily plugged into any breadboard. Connect the VCC pin to the Arduino’s 5V and the GND pin to ground. Finally, connect the Data pin to digital pin #8.

To ensure proper communication between the sensor and MCU, you must also add a 10K pull-up resistor between the Data line and VCC (to keep the signal HIGH). If you have a breakout board for the sensor, you do not need to add an external pull-up resistor, as it already contains one.

Arduino Wiring Fritzing Connections with DHT11
Wiring DHT11 to Arduino UNO

Arduino Wiring Fritzing Connections with DHT22
Wiring DHT22 to Arduino UNO

You’re now ready to upload some code and get it working.

Library Installation

The DHTxx sensors have 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>
#define dataPin 8 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object

void setup() 
void loop() 
	//Uncomment whatever type you're using!
	int readData = DHT.read22(dataPin); // DHT22/AM2302
	//int readData = DHT.read11(dataPin); // DHT11

	float t = DHT.temperature; // Gets the values of the temperature
	float h = DHT.humidity; // Gets the values of the humidity

	// Printing the results on the serial monitor
	Serial.print("Temperature = ");
	Serial.print(" ");
	Serial.print((char)176);//shows degrees character
	Serial.print("C | ");
	Serial.print((t * 9.0) / 5.0 + 32.0);//print the temperature in Fahrenheit
	Serial.print(" ");
	Serial.print((char)176);//shows degrees character
	Serial.println("F ");
	Serial.print("Humidity = ");
	Serial.println(" % ");

	delay(2000); // Delays 2 secods

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

DHT11 DHT22 AM2302 Sensor DHTlib library Output on Serial Monitor
Output on Serial Monitor

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 dataPin 8 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object

In the setup, we initialize the serial communication.

void setup() {

In the loop, we use the read22(dataPin) function to read the DHT22. This function takes as a parameter the sensor’s Data pin number. When working with DHT11, you must use the read11() function; to do so, you just need to uncomment the second line.

//Uncomment whatever type you're using!
int readData = DHT.read22(dataPin); // DHT22/AM2302
//int readData = DHT.read11(dataPin); // DHT11

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

float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the 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

//print the temperature in Fahrenheit
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 and DHT22 sensors.

This is what the output looks like.

DHT11 DHT22 Arduino Sketch Temperature Humidity Measurements Output on LCD
Temperature & humidity measurements on LCD

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


Following that, connect the LCD as shown below.

Arduino Wiring Fritzing Connections with DHT11 and 16x2 Character LCD
Wiring DHT11 and 16×2 Character LCD to Arduino UNO
Arduino Wiring Fritzing Connections with DHT22 and 16x2 Character LCD
Wiring DHT22 and 16×2 Character LCD to Arduino UNO

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> // includes the LiquidCrystal Library
#include <dht.h>
#define dataPin 8

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
dht DHT;
bool showcelciusorfarenheit = false;

void setup() 
	lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

void loop() 
	int readData = DHT.read22(dataPin);
	float t = DHT.temperature;
	float h = DHT.humidity;
	lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
	lcd.print("Temp.: "); // Prints string "Temp." on the LCD

	//Print temperature value in Celcius and Fahrenheit every alternate cycle
		lcd.print(t); // Prints the temperature value from the sensor
		lcd.print(" ");
		lcd.print((char)223);//shows degrees character
		showcelciusorfarenheit = false;
		lcd.print((t * 9.0) / 5.0 + 32.0); // print the temperature in Fahrenheit
		lcd.print(" ");
		lcd.print((char)223);//shows degrees character
		showcelciusorfarenheit = true;
	lcd.print("Humi.: ");
	lcd.print(" %");