Bird
Raised Fist0
Arduinoprogramming~20 mins

Humidity and temperature (DHT11/DHT22) in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DHT Sensor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Reading temperature from DHT11 sensor
What will be the output on the Serial Monitor when this Arduino code runs with a DHT11 sensor connected correctly?
Arduino
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float temp = dht.readTemperature();
  if (isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    Serial.print("Temperature: ");
    Serial.print(temp);
    Serial.println(" *C");
  }
  delay(2000);
}
AFailed to read from DHT sensor!
BTemperature: 23.00 *C
CTemperature: 23 *F
DError: DHT library not found
Attempts:
2 left
💡 Hint
The code reads temperature in Celsius and prints it if the sensor reading is valid.
❓ Predict Output
intermediate
2:00remaining
Humidity reading with DHT22 sensor
What will be printed on the Serial Monitor if the humidity reading is 55.5% and the sensor works correctly?
Arduino
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float humidity = dht.readHumidity();
  if (isnan(humidity)) {
    Serial.println("Error reading humidity");
  } else {
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
  }
  delay(1500);
}
AHumidity: 55.50 %
BHumidity: 55.5
CError reading humidity
DHumidity: 55 %
Attempts:
2 left
💡 Hint
The code prints humidity with two decimals and a percent sign if reading is valid.
🔧 Debug
advanced
2:00remaining
Identify the error in DHT sensor initialization
What error will this Arduino code produce when compiling or running?
Arduino
#include <DHT.h>
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, "11");

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float t = dht.readTemperature();
  Serial.println(t);
  delay(1000);
}
ARuns correctly and prints temperature
BRuntime error: sensor not found
CCompilation error: invalid argument for DHT constructor
DCompilation error: missing semicolon
Attempts:
2 left
💡 Hint
Check the second argument passed to the DHT constructor.
📝 Syntax
advanced
2:00remaining
Find the syntax error in this DHT sensor reading code
Which option correctly fixes the syntax error in this Arduino code snippet?
Arduino
#include <DHT.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity()
  if (isnan(h)) {
    Serial.println("Error");
  } else {
    Serial.println(h);
  }
  delay(2000);
}
AAdd a semicolon after 'float h = dht.readHumidity()'
BChange 'isnan(h)' to 'isNan(h)'
CRemove the parentheses from 'dht.readHumidity'
DReplace 'Serial.println(h);' with 'Serial.print(h);'
Attempts:
2 left
💡 Hint
Look carefully at the end of the line where the variable h is assigned.
🚀 Application
expert
3:00remaining
Calculate heat index from DHT22 readings
Given temperature in Celsius and humidity from a DHT22 sensor, which code snippet correctly calculates and prints the heat index in Celsius?
Arduino
#include <DHT.h>
#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t) || isnan(h)) {
    Serial.println("Sensor error");
    delay(2000);
    return;
  }
  // Calculate heat index here
  float hi;
  // ???
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *C");
  delay(3000);
}
Ahi = t * h / 100;
Bhi = 0.5 * (t + 61.0 + ((t-68.0)*1.2) + (h*0.094)); hi = (hi + t) / 2.0;
Chi = t + (0.33 * h) - (0.7 * windSpeed) - 4.0; // windSpeed undefined
Dhi = -8.784695 + 1.61139411*t + 2.338549*h - 0.14611605*t*h - 0.012308094*t*t - 0.016424828*h*h + 0.002211732*t*t*h + 0.00072546*t*h*h - 0.000003582*t*t*h*h;
Attempts:
2 left
💡 Hint
Use the full formula for heat index in Celsius involving temperature and humidity.

Practice

(1/5)
1. What does the DHT11 sensor measure in an Arduino project?
easy
A. Humidity and temperature
B. Light intensity
C. Sound levels
D. Distance

Solution

  1. Step 1: Understand sensor purpose

    The DHT11 sensor is designed to measure environmental conditions.
  2. Step 2: Identify measured values

    It specifically measures humidity and temperature, not light, sound, or distance.
  3. Final Answer:

    Humidity and temperature -> Option A
  4. Quick Check:

    DHT11 measures humidity and temperature [OK]
Hint: Remember DHT sensors measure air humidity and temperature [OK]
Common Mistakes:
  • Confusing DHT11 with light or sound sensors
  • Thinking it measures distance
  • Assuming it measures pressure
2. Which of the following is the correct way to include the DHT library in an Arduino sketch?
easy
A. #include "dht11.h"
B. #include
C. #include <dht_sensor>
D. #include

Solution

  1. Step 1: Recall library inclusion syntax

    Arduino libraries are included with #include and angle brackets for installed libraries.
  2. Step 2: Identify correct library name

    The standard DHT library is included as #include <DHT.h>.
  3. Final Answer:

    #include <DHT.h> -> Option D
  4. Quick Check:

    Use #include <DHT.h> to include DHT library [OK]
Hint: Use angle brackets and correct case for library names [OK]
Common Mistakes:
  • Using wrong file names
  • Using quotes instead of angle brackets
  • Incorrect capitalization
3. What will be the output of this Arduino code snippet if the sensor reading is successful?
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");
medium
A. Error reading sensor
B. Humidity: % Temperature: *C
C. Humidity: 45.00 % Temperature: 23.00 *C
D. Humidity: NaN % Temperature: NaN *C

Solution

  1. Step 1: Understand sensor reading functions

    The functions readHumidity() and readTemperature() return float values if successful.
  2. Step 2: Analyze Serial output

    The code prints humidity and temperature values with labels and units, separated by tab.
  3. Final Answer:

    Humidity: 45.00 % Temperature: 23.00 *C -> Option C
  4. Quick Check:

    Successful read prints values with labels [OK]
Hint: Successful reads print numbers, errors show NaN or messages [OK]
Common Mistakes:
  • Expecting error message on success
  • Ignoring units in output
  • Confusing NaN with valid numbers
4. Identify the error in this Arduino code snippet for reading DHT sensor data:
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (humidity == NULL || temperature == NULL) {
  Serial.println("Failed to read from DHT sensor!");
}
medium
A. Using NULL to check float values is incorrect
B. Missing semicolon after readTemperature()
C. Serial.println syntax is wrong
D. Variables humidity and temperature should be int

Solution

  1. Step 1: Understand data types and checks

    readHumidity() and readTemperature() return float, not pointers, so NULL is invalid.
  2. Step 2: Correct way to check errors

    Use isnan() function to check if readings failed (NaN means not a number).
  3. Final Answer:

    Using NULL to check float values is incorrect -> Option A
  4. Quick Check:

    Check floats with isnan(), not NULL [OK]
Hint: Use isnan() to check float errors, not NULL [OK]
Common Mistakes:
  • Checking floats against NULL
  • Assuming int type for sensor data
  • Syntax errors in Serial print
5. You want to display temperature in Fahrenheit using DHT22 sensor readings in Arduino. Which code snippet correctly converts Celsius to Fahrenheit after reading temperature?
float tempC = dht.readTemperature();
float tempF = ?;
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" *F");
hard
A. tempF = tempC * 5 / 9 + 32;
B. tempF = tempC * 9 / 5 + 32;
C. tempF = tempC / 9 * 5 + 32;
D. tempF = (tempC + 32) * 9 / 5;

Solution

  1. Step 1: Recall Celsius to Fahrenheit formula

    The correct formula is Fahrenheit = Celsius * 9/5 + 32.
  2. Step 2: Apply formula in code

    Assign tempF = tempC * 9 / 5 + 32; to convert temperature correctly.
  3. Final Answer:

    tempF = tempC * 9 / 5 + 32; -> Option B
  4. Quick Check:

    Celsius to Fahrenheit = C * 9/5 + 32 [OK]
Hint: Use C * 9/5 + 32 to convert Celsius to Fahrenheit [OK]
Common Mistakes:
  • Mixing multiplication and division order
  • Adding 32 before multiplying
  • Using wrong formula
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard