How to use the DHT11 or DHT22 with an Arduino
and which one you should get…
All my content is free.
If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.
Select amount then click the “Donate” button.
One of the first tutorial I made was for the DHT11 sensor and that was quite a long time ago… So let’s revisit these temperature and humidity sensors.
These types of sensors are very popular in Arduino projects, so in the coming weeks I will make some tutorial videos exploring the many types available.
In this first part we will look at the most popular ones: The DHT11 and the DHT22.
On the surface these two sensors share a lot of similarities:
- Same pinout
- Exterior casing (although the DHT22 is a little larger)
- Both can measure temperature and humidity
In this tutorial we will be using a library designed to work with both the DHT11 and the DHT22, so it kinda confirms that these are pretty similar.
So let’s find out how to use them with the Arduino and which one you should choose for your project.
The DHT11 is the one that you probably have come across, it’s cheap and many tutorials have been made about it.
Here at the specs:
Temperature range: 0 – 50 degrees Celcius (+- 2 degrees)
Humidity range: 20 – 80% (+- 5%)
Sampling rate: 1HZ (One reading per second)
Operating voltage: 3 – 5V
When we compare it to the DHT22 it is better in two area:
- The sampling rate
- Physical size (it’s a little smaller…)
The DHT22 can be seen as a more expensive version of the DHT11 with better specs:
Temperature range: -40 to 125 degrees Celcius (+- 0.5 degrees)
Humidity range: 0 – 100% (+- 2-5%)
Sampling rate: 0.5HZ (One reading per 2 second)
Operating voltage: 3 – 5V
When we compare it to the DHT11 it is better in one area:
- Better range and accuracy in temperature and humidity readings
But worse in two areas:
- Slower sampling rate
- Bigger physical size
It’s also a bit more expensive than the DHT11, but if you need the greater range and accuracy it might be worth it.
DHT11 and DHT22 Pinout
Both the DHT11 and DTH22 share the same pinout.
The DATA pin of both of these need to be Pulled Up for reliable communication. Check out this tutorial for more information on PullUp resistors.
In this tutorial we will use the internal PullUp resistors of the Arduino, but in some cases you might need to use a physical resistor for these. Check out the tutorial video for more information on when that might be needed.
*NOTE: If you use a DTH11 or DHT22 module, these might already have a PullUp resistor installed onboard, in that case you shouldn’t use the internal PullUp resistor of the Arduino.
Note that NOT ALL modules have this resistor installed, so you need to check if it is present on the module PCB.
Also if you module includes a power LED than you would need to see 2 resistors onboard since the LED needs one.
The small OLED display uses I2C so the connections need to use the I2C pins of the Arduino:
SDA pin of the OLED is connected to the A4 pin of the Arduino
SCL pin is connected to the A5 pin of the Arduino.
The DHT11 Data pin is connected to pin 4
and the DHT22 Data pin is connected to pin 5
3.3v of the Arduino is connected to the VCC pins of both the DHT11 and DHT22
5v of the Arduino is connected to the VCC of the OLED
GND of the Arduino is connected to the Ground pins of the OLED and both sensors.
I’m using a small OLED display to show the readings of both the DHT11 and DHT22.
This information is updated every two seconds which is the recommended update time for the DHT22, although the DHT11 can be updated a little bit faster (once a second).
To check the results I’m using the NEST Thermostat I installed in a previous tutorial here, to see the accuracy of the temperature and humidity readings of both sensor.
You can find a link to download both library used in this tutorial at the bottom of this page.
As always don’t forget to check out the tutorial video for more information and the testing results.
If your project doesn’t rely on super accurate temperature readings and doesn’t need humidity then the DHT11 would do the trick.
Keep in mind that it only reads from 20-80 degrees celsius.
On the other hand if you need both temperature and humidity readings then the DHT22 is the one you want.
The DHT22 costs just a bit more, but it provides a greater temperature range, is more accurate and the humidity reading actually works unlike the DHT11.
Copy and Paste the above code/sketch in your Arduino IDE software.
Download the U8g library here:
Download the DHT library here:
Or use this direct link to download the DHTstable library