What if your project could 'feel' the air around it and act all by itself?
Why Humidity and temperature (DHT11/DHT22) in Arduino? - Purpose & Use Cases
Imagine you want to measure the air temperature and humidity in your room using simple sensors. Without special tools, you might try guessing or using separate devices and writing down numbers by hand.
Manually checking temperature and humidity is slow and can be inaccurate. Writing down numbers wastes time and can lead to mistakes. You can't easily track changes over time or react quickly to the environment.
Using DHT11 or DHT22 sensors with Arduino lets you automatically read temperature and humidity values. The sensors send data directly to your program, which can display, log, or use it instantly without errors or delays.
// No code, just guessing or writing numbers by hand#include <DHT.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); Serial.print("Humidity: " + String(h) + "% Temperature: " + String(t) + "C"); delay(2000); }
You can build smart projects that sense and respond to the environment automatically and accurately.
For example, a plant watering system that checks soil humidity and air temperature to water plants only when needed, saving water and keeping plants healthy.
Manual tracking of temperature and humidity is slow and error-prone.
DHT11/DHT22 sensors automate reading these values with Arduino.
This enables smart, responsive projects that react to real-world conditions.
Practice
Solution
Step 1: Understand sensor purpose
The DHT11 sensor is designed to measure environmental conditions.Step 2: Identify measured values
It specifically measures humidity and temperature, not light, sound, or distance.Final Answer:
Humidity and temperature -> Option AQuick Check:
DHT11 measures humidity and temperature [OK]
- Confusing DHT11 with light or sound sensors
- Thinking it measures distance
- Assuming it measures pressure
Solution
Step 1: Recall library inclusion syntax
Arduino libraries are included with #include and angle brackets for installed libraries.Step 2: Identify correct library name
The standard DHT library is included as#include <DHT.h>.Final Answer:
#include <DHT.h> -> Option DQuick Check:
Use #include <DHT.h> to include DHT library [OK]
- Using wrong file names
- Using quotes instead of angle brackets
- Incorrect capitalization
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");Solution
Step 1: Understand sensor reading functions
The functionsreadHumidity()andreadTemperature()return float values if successful.Step 2: Analyze Serial output
The code prints humidity and temperature values with labels and units, separated by tab.Final Answer:
Humidity: 45.00 % Temperature: 23.00 *C -> Option CQuick Check:
Successful read prints values with labels [OK]
- Expecting error message on success
- Ignoring units in output
- Confusing NaN with valid numbers
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (humidity == NULL || temperature == NULL) {
Serial.println("Failed to read from DHT sensor!");
}Solution
Step 1: Understand data types and checks
readHumidity() and readTemperature() return float, not pointers, so NULL is invalid.Step 2: Correct way to check errors
Useisnan()function to check if readings failed (NaN means not a number).Final Answer:
Using NULL to check float values is incorrect -> Option AQuick Check:
Check floats with isnan(), not NULL [OK]
- Checking floats against NULL
- Assuming int type for sensor data
- Syntax errors in Serial print
float tempC = dht.readTemperature();
float tempF = ?;
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" *F");Solution
Step 1: Recall Celsius to Fahrenheit formula
The correct formula is Fahrenheit = Celsius * 9/5 + 32.Step 2: Apply formula in code
AssigntempF = tempC * 9 / 5 + 32;to convert temperature correctly.Final Answer:
tempF = tempC * 9 / 5 + 32; -> Option BQuick Check:
Celsius to Fahrenheit = C * 9/5 + 32 [OK]
- Mixing multiplication and division order
- Adding 32 before multiplying
- Using wrong formula
Ready to prove it?
Pass the test and this pattern moves unknown
