Sub topic 8
Respecting the Sampling Interval
Assume you know the basic Arduino setup() and loop() structure, can upload a sketch, and can use the Serial Monitor. You also know basic C++ types such as float and unsigned long.
A DHT sensor cannot provide a new measurement every time your sketch asks for one. The sensor needs time to measure temperature and humidity, process the result, and prepare its next response. The minimum time between two read requests is the sampling interval.
Why the interval matters
The DHT11 usually supports about one reading per second. The DHT22 usually supports about one reading every two seconds. Check the datasheet for your exact sensor and follow the library's guidance.
| Sensor | Typical minimum interval | Practical interval |
|---|---|---|
| DHT11 | 1 second | 1–2 seconds |
| DHT22 | 2 seconds | 2 seconds or longer |
If you request data too quickly, several problems can occur:
- The sensor may return the previous measurement instead of a new one.
- The library may report a failed reading.
- The values may appear unchanged, even though the environment changed.
- Repeated requests can make debugging harder because failures seem random.
The sensor's response is also slower than a simple digital pin read. A DHT sensor uses one data wire and a timed protocol. The Arduino and sensor exchange a start signal, response pulses, and 40 data bits. The library handles this timing, but it cannot make the sensor measure faster than its hardware allows.
The simple approach: use `delay()`
For a sketch that only reads the sensor, a delay is easy to understand:
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
if (isnan(humidity) || isnan(temperatureC)) {
Serial.println(F("Reading failed"));
return;
}
Serial.print(F("Temperature: "));
Serial.print(temperatureC);
Serial.print(F(" C, Humidity: "));
Serial.print(humidity);
Serial.println(F(" %"));
}The delay(2000) statement waits 2,000 milliseconds before each read. This gives a DHT22 enough time between requests. It also works as a conservative interval for a DHT11.
The first reading happens after the initial two-second delay. After that, the sketch reads, prints, and waits again. The actual time between requests is slightly longer than two seconds because reading and printing also take time.
Choosing a useful interval
Temperature and humidity usually change slowly indoors. Reading every few seconds is normally sufficient. A shorter interval does not create more accurate data. It only asks the sensor for information faster than it can produce new measurements.
Use a longer interval when:
- Battery power matters.
- You only need a trend.
- The sensor is in a stable environment.
- Your program has other work to perform.
Use the sensor's minimum interval only when you need the fastest supported updates. For a weather display, one reading every 10 or 30 seconds may be enough. For a control system, choose an interval that matches how quickly the controlled environment changes.
The next step is to replace a blocking delay with millis(). That method keeps the interval while allowing other code to run during the wait.
Exercise
A DHT22 sketch reads the sensor every 500 milliseconds. What should you change, and why?
Show answer
Change the interval to at least 2,000 milliseconds. The DHT22 typically needs about two seconds between measurements, so faster requests can return stale data or fail.