Sub topic 13
Displaying and Logging Readings
Assume you know the basic Arduino setup() and loop() structure, can upload a sketch, and have already wired the DHT sensor and installed the Adafruit DHT library. This chapter focuses on presenting readings clearly and producing data that another program can process.
Display readings in the Serial Monitor
The Serial Monitor displays text sent through Arduino's serial connection. Start it at the same baud rate used by Serial.begin(). A baud rate is the communication speed, measured in symbols per second.
Human-readable output is useful while testing:
Temperature: 23.40 C, Humidity: 48.10 %CSV, or comma-separated values, is better for logging. Each line contains fields in a fixed order:
time_ms,temperature_c,humidity_percent
2350,23.40,48.10
4350,23.50,48.00The first line is a header. It names each column and makes the file easier to import into a spreadsheet or data-analysis program.
Create a regular CSV log
This complete sketch prints one valid CSV row every two seconds. It uses millis() so the sketch does not stop while waiting between readings.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const unsigned long READ_INTERVAL_MS = 2000;
unsigned long lastReadMs = 0;
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println(F("time_ms,temperature_c,humidity_percent"));
}
void loop() {
unsigned long now = millis();
if (now - lastReadMs < READ_INTERVAL_MS) {
return;
}
lastReadMs = now;
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
if (isnan(humidity) || isnan(temperatureC)) {
Serial.print(now);
Serial.println(F(",NaN,NaN"));
return;
}
Serial.print(now);
Serial.print(',');
Serial.print(temperatureC, 2);
Serial.print(',');
Serial.println(humidity, 2);
}The second argument to Serial.print() sets the number of digits after the decimal point. Using two digits gives consistent columns, even though the sensor does not measure that precisely.
The timestamp is the number of milliseconds since the Arduino started. It is useful for calculating elapsed time, but it is not calendar time. An Arduino without a real-time clock cannot know the current date after power is removed.
Keep logged data machine-readable
Avoid adding labels to CSV rows:
Temperature: 23.40 C, Humidity: 48.10 %A spreadsheet can display this text, but it must first remove the labels and units. Put units in the header instead. Keep the field order and number of fields constant.
| Field | Meaning | Example |
|---|---|---|
| `time_ms` | Milliseconds since startup | `2350` |
| `temperature_c` | Temperature in Celsius | `23.40` |
| `humidity_percent` | Relative humidity percentage | `48.10` |
Common logging errors
- A baud-rate mismatch produces unreadable characters. Set the Serial Monitor to
9600. - Repeated headers make later analysis harder. Print the header once in
setup(). - Calling
delay()for a long period prevents other tasks from running. Usemillis()for scheduling. - Treating
NaNas a number can corrupt calculations. Checkisnan()before writing normal values. - Disconnecting or resetting the Arduino loses the Serial Monitor output unless a computer records it. Serial output is not permanent storage.
Logging data flow
Exercise
Why should units be placed in the CSV header instead of appended to every value?
Show answer
A header such as temperature_c identifies the unit while leaving each data field numeric, so spreadsheet and analysis programs can process it directly.