Humidity and temperature (DHT11/DHT22) in Arduino - Time & Space Complexity
When reading humidity and temperature from sensors like DHT11 or DHT22, it's important to understand how the time to get data changes as we read more times or handle more sensors.
We want to know how the program's running time grows when we increase the number of readings.
Analyze the time complexity of the following code snippet.
#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();
delay(2000);
}
This code reads humidity and temperature once every 2 seconds from a single DHT11 sensor.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading humidity and temperature from the sensor.
- How many times: Once every 2 seconds, repeated indefinitely in the loop.
Each reading takes roughly the same time, so if we take more readings, the total time grows directly with the number of readings.
| Input Size (n readings) | Approx. Operations |
|---|---|
| 10 | 10 sensor reads |
| 100 | 100 sensor reads |
| 1000 | 1000 sensor reads |
Pattern observation: The time grows linearly as we increase the number of readings.
Time Complexity: O(n)
This means the total time to read data grows directly in proportion to how many times we read the sensor.
[X] Wrong: "Reading from the sensor is instant and does not affect program time."
[OK] Correct: Each sensor read takes a fixed amount of time (usually around 250 milliseconds to 1 second), so reading many times adds up and affects total running time.
Understanding how sensor reading time adds up helps you design efficient programs that handle data smoothly without delays piling up.
"What if we read from multiple DHT sensors in the same loop? How would the time complexity change?"
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
