DHT11 temperature and humidity on an Arduino Uno
A DHT11 module on pin 2: the DHT library reads the temperature and humidity every two seconds and prints both. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
Build it as the Sensor Starter Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $44, free on Mokxi.
What you'll learn
- A one-wire sensor protocol
- Using a library to hide a timing-heavy handshake
- Checking for failed readings with
isnan - Reading temperature and humidity
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The DHT11 has a tiny chip inside and needs only three wires: 5V, GND, and DATA on pin 2. When pin 2 is pulled low, the sensor answers with forty bits: humidity, temperature and a checksum.
The DHT library handles that handshake, so the sketch only asks for two numbers. Every two seconds it reads humidity and temperature and prints them in whole degrees Celsius and whole percent. The sensor needs about a second to make a new reading, so asking faster would only return the old one.
Parts list
3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Temperature and humidity, DHT22In Sensor Starter Kit.
Kits that can build it
These 8 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Temperature and humidity, DHT22 pin GND | Arduino Uno R3 pin GND |
| Temperature and humidity, DHT22 pin DATA | Arduino Uno R3 pin 2 |
| Temperature and humidity, DHT22 pin VCC | Arduino Uno R3 pin 5V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// DHT11: temperature and humidity on one wire, printed every two seconds.
//
// DHT11 VCC -> 5V
// DHT11 DATA -> pin 2
// DHT11 GND -> GND
//
// The DHT11 has its own little chip inside. Pull its data line low and it
// answers with forty bits: humidity, temperature and a checksum. The DHT
// library does that handshake; the sketch only asks for the two numbers.
//
// The sensor takes about a second to make a new reading, so asking more often
// than every two seconds just returns the last one again. The DHT11 reads
// whole degrees and whole percent.
#include <DHT.h>
DHT dht(2, DHT11);
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: DHT11 temperature and humidity");
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float celsius = dht.readTemperature();
if (isnan(humidity) || isnan(celsius)) {
Serial.println("no reading: check the DATA wire");
return;
}
Serial.print("temperature ");
Serial.print(celsius, 0);
Serial.print(" C, humidity ");
Serial.print(humidity, 0);
Serial.println(" %");
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 9600, prints a title line, and calls dht.begin() to start the sensor. The object dht was made for pin 2 and the DHT11 type.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: DHT11 temperature and humidity");
dht.begin();
}loop sketch.ino
Waits 2000 ms, then calls dht.readHumidity() and dht.readTemperature(). If either result is isnan, it prints a warning to check the DATA wire and returns. Otherwise it prints both values with no decimals.
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float celsius = dht.readTemperature();
if (isnan(humidity) || isnan(celsius)) {
Serial.println("no reading: check the DATA wire");
return;
}
Serial.print("temperature ");
Serial.print(celsius, 0);
Serial.print(" C, humidity ");
Serial.print(humidity, 0);
Serial.println(" %");
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It prints "no reading: check the DATA wire" This project
DATA must go to pin 2, and VCC and GND must be connected. - The numbers update slowly This project
That is normal. The sensor makes a new reading only about once a second, and the sketch reads every 2 seconds. - Readings are NaN or fail, or never appear. Temperature and humidity
Put a 10 k ohm pull-up between DATA and VCC (bare sensors need it). A 4-pin DHT22 has pin 3 unused. Check the data pin number in your code. - Values freeze or errors show up when you read often. Temperature and humidity
The DHT22 can only be read about once every 2 seconds. Wait between reads, and check for NaN before using the numbers. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.