DHT11 temperature and humidity on pin 10, on an Arduino Uno
A DHT11 on pin 10, with a 10k pull-up on its data line, reports the humidity and the temperature to the serial monitor once a second. 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.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Using a library with
#include - Reading a one-wire digital sensor
- Checking for bad readings with
isnan - Printing labeled values to the serial monitor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The sensor has three connections: 5V, GND, and a DATA line to pin 10. The sketch asks for a reading. The sensor answers with 40 bits of timed pulses that hold humidity, temperature and a checksum.
The DHT library does the timing. If the checksum fails, the library hands back NaN, which means "not a number". The sketch checks for that and prints "Read error". Otherwise it prints the humidity and the temperature in Celsius once a second. Click the sensor in the simulator to change the weather.
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, DHT22
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 10 |
| 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.
// Temperature and humidity: a DHT11 on pin 10.
//
// DHT11 VCC -> 5V DATA -> pin 10 (10k from DATA to 5V) GND -> GND
//
// The DHT11 answers a start signal with 40 bits of pulses: humidity,
// temperature and a checksum. The DHT library times them; a reading that
// fails its checksum comes back as NaN, and the sketch says so.
//
// Click the sensor to change the weather.
#include <DHT.h>
DHT dht(10, DHT11);
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("UNO is ready!");
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Read error");
} else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%, Temperature: ");
Serial.print(temperature);
Serial.println("C");
}
delay(1000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens serial at 9600 baud and calls dht.begin() to get the sensor ready. The sensor object dht was made at the top with pin 10 and the type DHT11. It then prints "UNO is ready!".
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("UNO is ready!");
}loop sketch.ino
Calls readHumidity and readTemperature. If either one is NaN, it prints "Read error". Otherwise it prints both values with labels. Then it waits 1000 ms, because these sensors are slow and need a pause between readings.
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Read error");
} else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%, Temperature: ");
Serial.print(temperature);
Serial.println("C");
}
delay(1000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- "Read error" keeps printing This project
Check the three wires: VCC to 5V, GND to GND, and DATA to pin 10. - The readings do not match the sensor type This project
The sketch createsDHT dht(10, DHT11). If you use a different sensor type, change that type in the code. - 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.