Sub topic 12
Reading Without Blocking the Sketch
Assume you know the Arduino setup() and loop() structure, can upload a sketch, and have used the Serial Monitor. You also need a DHT sensor connected and the Adafruit DHT library installed.
A simple DHT sketch often uses delay(2000) before every reading:
delay(2000);
float humidity = dht.readHumidity();This respects the DHT sensor’s minimum sampling interval, but it stops the processor during the delay. The Arduino cannot check a button, update a display, or control a motor until the delay ends.
Non-blocking timing checks the clock instead. The sketch continues running and reads the sensor only when enough time has passed.
Use `millis()` as a timer
millis() returns the number of milliseconds since the Arduino started. Store the time of the previous reading, then compare it with the current time.
The subtraction form is important:
if (now - lastReadMs >= READ_INTERVAL_MS) {
// Read the sensor
}Because the variables are unsigned long, this comparison continues to work when millis() eventually rolls over to zero.
Complete non-blocking example
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
const unsigned long READ_INTERVAL_MS = 2000;
DHT dht(DHTPIN, DHTTYPE);
unsigned long lastReadMs = 0;
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
unsigned long now = millis();
if (now - lastReadMs >= READ_INTERVAL_MS) {
lastReadMs = now;
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
if (isnan(humidity) || isnan(temperatureC)) {
Serial.println(F("DHT read failed"));
} else {
Serial.print(F("Temperature C: "));
Serial.print(temperatureC);
Serial.print(F(", Humidity %: "));
Serial.println(humidity);
}
}
// This code runs on every loop pass.
// Check buttons, update outputs, or perform other tasks here.
}The interval is two seconds because common DHT sensors cannot provide reliable new readings faster than that. A DHT22 usually needs about two seconds between measurements. Check the data sheet for your exact sensor.
The example updates lastReadMs before reading. Therefore, a failed read does not cause immediate retries. The next attempt still waits for the complete interval. This prevents a disconnected sensor from making the sketch repeatedly call the library.
Coordinate several timed tasks
A sketch can use the same pattern for multiple jobs. Each job needs its own interval and previous-time variable.
const unsigned long DISPLAY_INTERVAL_MS = 250;
unsigned long lastDisplayMs = 0;
void updateDisplayWhenDue() {
unsigned long now = millis();
if (now - lastDisplayMs >= DISPLAY_INTERVAL_MS) {
lastDisplayMs = now;
// Refresh the display.
}
}Call updateDisplayWhenDue() from loop(). The DHT task can run every two seconds while the display task runs four times per second.
> callout tip > Non-blocking timing does not make the DHT measurement itself instantaneous. The library may briefly wait while it receives the sensor’s data. The important improvement is removing the long, repeated delay() that prevents all other sketch work.
Exercise
Why should a DHT reading task update its previous-reading time even when the sensor returns NaN?
Show answer
Updating the time prevents immediate retries. The sketch waits for the sensor's full minimum interval before trying again.