Humidity and temperature (DHT11/DHT22) in Arduino - Mini Project: Build & Apply
DHT.h library at the top of your sketch. Create a DHT object called dht using pin 2 and sensor type DHT11. Also, start the Serial communication at 9600 baud in the setup() function.Use #include <DHT.h> to include the library. Define DHTPIN as 2 and DHTTYPE as DHT11. Create the dht object with these. In setup(), start serial with Serial.begin(9600); and call dht.begin();.
loop() function, create two float variables called humidity and temperature. Use dht.readHumidity() to set humidity and dht.readTemperature() to set temperature.Use float humidity = dht.readHumidity(); and float temperature = dht.readTemperature(); inside loop().
if statement to check if humidity or temperature is NaN using isnan(). If either is NaN, print "Failed to read from DHT sensor!" and return from loop(). Otherwise, print "Humidity: " followed by humidity and " %", then print "Temperature: " followed by temperature and " *C". Add a delay of 2000 milliseconds at the end.Use if (isnan(humidity) || isnan(temperature)) to check for errors. Print the error message and return; if true. Otherwise, print humidity and temperature with labels and units. Use delay(2000); to wait 2 seconds before next reading.
Humidity: 45.00 %Temperature: 23.00 *CIf the sensor fails, it should print:
Failed to read from DHT sensor!Run your program and open the Serial Monitor at 9600 baud. You should see humidity and temperature printed every 2 seconds. If the sensor fails, the error message appears.
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
