DHT11 / DHT22 Code Generator
Generate ready Arduino code for the DHT11 or DHT22 temperature & humidity sensor in seconds. Pick the sensor type, pin and unit, then copy the code.
What is the DHT11 / DHT22 Temperature & Humidity Sensor?
The DHT11 and DHT22 are popular sensors that measure both temperature and humidity over a single digital pin. They are cheap, easy to use and form the basis of most Arduino weather stations, greenhouses and indoor-monitoring projects. This tool builds ready-to-flash code using the Adafruit DHT library, based on your chosen sensor type, pin and unit.
Wiring (Arduino Uno)
- VCC → 3.3V - 5V
- GND → GND
- DATA → digital pin (default 2)
A 10k ohm pull-up resistor between DATA and VCC is recommended. Three-pin breakout boards usually include it already.
Library Setup
In the Arduino IDE, open Tools → Library Manager and install the DHT sensor library (Adafruit) and Adafruit Unified Sensor.
DHT11 vs DHT22
| DHT11 | DHT22 | |
|---|---|---|
| Temperature range | 0-50°C | -40-80°C |
| Accuracy | ±2°C | ±0.5°C |
| Humidity range | 20-80% | 0-100% |
| Refresh rate | 1 Hz | 0.5 Hz |
Frequently Asked Questions
The sensor returns "nan" or fails to read.
Set the read interval to at least 2000 ms, and check the DATA pin wiring and pull-up resistor. Make sure you selected the correct DHTTYPE (DHT11 vs DHT22).
Does the same code work for both DHT11 and DHT22?
Yes. Just change the sensor type above; the code updates the #define DHTTYPE line automatically.
Can I get Fahrenheit output?
Yes, choose Fahrenheit as the unit; the code uses dht.readTemperature(true).