Challenge - 5 Problems
DHT Sensor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
The code reads temperature in Celsius and prints it if the sensor reading is valid.
✗ Incorrect
The code uses dht.readTemperature() which returns temperature in Celsius. If the sensor reading fails, it prints an error message. Assuming the sensor works and temperature is 23°C, option B matches the output.
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
The code prints humidity with two decimals and a percent sign if reading is valid.
✗ Incorrect
The dht.readHumidity() returns a float with decimal precision. The code prints the value followed by a space and percent sign. Option A matches exactly.
🔧 Debug
advanced2: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); }
Attempts:
2 left
💡 Hint
Check the second argument passed to the DHT constructor.
✗ Incorrect
The DHT constructor expects a constant like DHT11 or DHT22, not a string like "11". Passing "11" causes a compilation error due to invalid argument type.
📝 Syntax
advanced2: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); }
Attempts:
2 left
💡 Hint
Look carefully at the end of the line where the variable h is assigned.
✗ Incorrect
The line 'float h = dht.readHumidity()' is missing a semicolon at the end, causing a syntax error. Adding the semicolon fixes it.
🚀 Application
expert3: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); }
Attempts:
2 left
💡 Hint
Use the full formula for heat index in Celsius involving temperature and humidity.
✗ Incorrect
Option D uses the full Rothfusz regression formula adapted for Celsius to calculate heat index accurately. Option D is a simplified formula for Fahrenheit. Option D uses undefined variable. Option D is a wrong formula.
