Bird
Raised Fist0
Arduinoprogramming~10 mins

Humidity and temperature (DHT11/DHT22) in Arduino - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to include the DHT sensor library.

Arduino
#include [1]
Drag options to blanks, or click blank then click option'
A<Wire.h>
B<DHT.h>
C<SPI.h>
D<Servo.h>
Attempts:
3 left
💡 Hint
Common Mistakes
Including the wrong library like Wire.h or SPI.h which are for other sensors.
Forgetting the angle brackets <> around the library name.
2fill in blank
medium

Complete the code to define the DHT sensor type as DHT22.

Arduino
#define DHTTYPE [1]
Drag options to blanks, or click blank then click option'
ADHT11
BDHT12
CDHT22
DDHT33
Attempts:
3 left
💡 Hint
Common Mistakes
Using DHT11 when the sensor is actually DHT22.
Using undefined sensor types like DHT33.
3fill in blank
hard

Fix the error in initializing the DHT sensor object on pin 2.

Arduino
DHT dht([1], DHTTYPE);
Drag options to blanks, or click blank then click option'
A2
BDHTTYPE
C3
DDHT
Attempts:
3 left
💡 Hint
Common Mistakes
Using the sensor type or class name instead of the pin number.
Using a pin number that does not match the hardware connection.
4fill in blank
hard

Fill both blanks to read humidity and temperature values from the sensor.

Arduino
float humidity = dht.[1]();
float temperature = dht.[2]();
Drag options to blanks, or click blank then click option'
AreadHumidity
BreadTemperature
CreadTemp
DgetHumidity
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like readTemp or getHumidity which do not exist.
Mixing up humidity and temperature function calls.
5fill in blank
hard

Fill all three blanks to check if readings are valid and print them to the serial monitor.

Arduino
if (isnan([1]) || isnan([2])) {
  Serial.println("Failed to read from DHT sensor!");
  return;
}
Serial.print("Humidity: ");
Serial.print([3]);
Serial.println(" %");
Drag options to blanks, or click blank then click option'
Atemperature
Bhumidity
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Checking variables that do not exist or are misspelled.
Printing temperature instead of humidity in the last print statement.

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