We use DHT11 or DHT22 sensors to measure the air's humidity and temperature. This helps us understand the environment around us.
Humidity and temperature (DHT11/DHT22) in Arduino
#include <DHT.h> #define DHTPIN 2 // Pin where the sensor is connected #define DHTTYPE DHT11 // DHT11 or DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C"); delay(2000); }
Use #define DHTTYPE DHT11 for DHT11 sensor or DHT22 for DHT22 sensor.
Always check if the sensor reading is valid using isnan() before using the values.
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);true to readTemperature().float temperature = dht.readTemperature(true);float humidity = dht.readHumidity(); if (isnan(humidity)) { Serial.println("Error reading humidity"); }
This program reads humidity and temperature from a DHT11 sensor connected to pin 2. It prints the values every 2 seconds. If the sensor fails to read, it shows an error message.
#include <DHT.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); delay(2000); return; } Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C"); delay(2000); }
Make sure to install the DHT sensor library in the Arduino IDE before running the code.
Use a pull-up resistor (usually 10k ohms) between the data pin and 5V for stable sensor readings.
The DHT11 sensor is less accurate and slower than DHT22 but cheaper.
DHT11 and DHT22 sensors measure humidity and temperature.
Use the DHT library to read sensor data easily.
Always check if readings are valid before using them.
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
