Bird
0
0
Arduinoprogramming~10 mins

Humidity and temperature (DHT11/DHT22) in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Humidity and temperature (DHT11/DHT22)
Start
Initialize DHT sensor
Read humidity
Read temperature
Check if readings are valid
Print values
Wait before next read
Repeat loop
The program initializes the sensor, reads humidity and temperature, checks if readings are valid, prints them or an error, then waits before repeating.
Execution Sample
Arduino
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  dht.begin();
}
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C");
  delay(2000);
}
This code reads humidity and temperature from a DHT11 sensor and prints the values every 2 seconds, or prints an error if reading fails.
Execution Table
StepActionHumidity (h)Temperature (t)ConditionOutput
1Start loopN/AN/AN/AN/A
2Read humidity45.3N/AN/AN/A
3Read temperature45.323.7N/AN/A
4Check if h or t is NaN45.323.7FalseProceed
5Print humidity45.323.7N/AHumidity: 45.3 %
6Print temperature45.323.7N/ATemperature: 23.7 *C
7Delay 2000ms45.323.7N/AWait 2 seconds
8Repeat loopN/AN/AN/ABack to step 1
9Read humidity (error case)NaNN/AN/AN/A
10Read temperature (error case)NaNNaNN/AN/A
11Check if h or t is NaNNaNNaNTruePrint error
12Print errorNaNNaNN/AFailed to read from DHT sensor!
13Return earlyNaNNaNN/ASkip printing values
💡 Loop repeats indefinitely; error case returns early to avoid printing invalid data.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 9 (error)After Step 10 (error)
h (humidity)N/A45.345.345.3NaNNaN
t (temperature)N/AN/A23.723.7N/ANaN
Key Moments - 3 Insights
Why do we check if humidity or temperature is NaN before printing?
Because the sensor might fail to read data sometimes, resulting in NaN (not a number). Checking prevents printing invalid values, as shown in steps 4 and 11 of the execution_table.
What happens if the sensor reading fails?
The program prints an error message and returns early from the loop iteration, skipping the printing of humidity and temperature values. This is shown in steps 11 to 13.
Why is there a delay after printing the values?
The delay of 2000 milliseconds (2 seconds) slows down the loop so readings are spaced out and the serial output is readable, as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the humidity value after step 2?
ANaN
B23.7
C45.3
DN/A
💡 Hint
Check the 'Humidity (h)' column at step 2 in the execution_table.
At which step does the program detect a failed sensor reading?
AStep 11
BStep 4
CStep 7
DStep 2
💡 Hint
Look for the step where the condition 'h or t is NaN' is True in the execution_table.
If the delay(2000) was removed, how would the execution_table change?
ATemperature would not be read
BStep 7 would be missing and loop would run faster
CHumidity values would become NaN
DError message would print every time
💡 Hint
Refer to step 7 where delay is applied before repeating the loop.
Concept Snapshot
Use DHT library to read humidity and temperature.
Check if readings are valid (not NaN) before printing.
Print values to Serial Monitor.
Use delay to space out readings.
Loop repeats indefinitely for continuous monitoring.
Full Transcript
This Arduino program reads humidity and temperature from a DHT11 or DHT22 sensor. It starts by initializing the sensor and serial communication. In each loop, it reads humidity and temperature values. It checks if either reading failed (NaN). If so, it prints an error message and skips printing values. Otherwise, it prints humidity and temperature to the Serial Monitor. Then it waits 2 seconds before repeating. This cycle continues indefinitely, providing updated sensor readings every 2 seconds.