Lesson 12: Project: A Weather Station with Display and LoggingNew-age
This lesson is a project: you will combine everything from Unit 1 into a working weather station that measures temperature, humidity and light, shows the readings on an LCD, and logs them to the computer over serial. It is the first project in this course that is a complete device, not a single circuit — and it is the pattern for the larger projects in Unit 2.
Work through it in stages, testing each stage before adding the next. A weather station built all at once and tested once is a debugging nightmare; the same station built in four tested stages is a straightforward afternoon’s work. The stages are: sensors, display, logging, and the enclosure.
- Weather station — a device that measures temperature, humidity, light and related values.
- DHT11/DHT22 — common combined temperature and humidity sensors.
- Logging — recording readings over time, usually with a timestamp.
- Calibration — comparing a sensor’s reading with a known value and correcting for the difference.
- Enclosure — the case that protects a project and holds its parts in place.
- Sampling interval — how often a reading is taken, such as once every 10 seconds.
- Timestamp — a note of when a reading was taken, so readings can be compared over time.
- Range — the span of values a sensor can measure, such as −40 °C to 80 °C.
Stage 1: the sensors
Use a DHT22 for temperature and humidity and an LDR for light. The DHT22 is a digital sensor: it sends its readings over a single data pin, and a library (such as the Adafruit DHT library) does the timing for you. Wire its data pin to a digital pin with a 10 kΩ pull-up resistor, power it from 5 V, and read it once every two seconds — it is a slow sensor, and reading it faster returns errors.
Test the sensors before anything else. Print temperature, humidity and light level to the Serial Monitor every two seconds and check the values against reality: room temperature should read about 20–25 °C, humidity about 40–60% indoors, and the light reading should rise when you shine a torch on the LDR. If the DHT22 returns “nan” (not a number), check the wiring and the pull-up resistor first — that is the commonest fault.
Stage 2: the display
Add a 16×2 I2C LCD, as in Lesson 8. Show temperature and humidity on the first row and light level on the second, for example “T:23.5C H:45%” and “Light: 720”. Update the display only when a value changes by a meaningful amount — 0.5 °C for temperature, 1% for humidity — so it does not flicker. The display is the project’s face: a visitor should be able to read the weather without touching anything.
Keep the display code separate from the sensor code, in its own function. That way you can change the display — to a bigger LCD, or to a seven-segment display — without touching the sensor logic. Separation of concerns is not just tidy; it is what makes a project possible to debug and to extend.
Stage 3: logging over serial
Log each reading as one line over serial, in a format a spreadsheet can read: a timestamp in seconds, then temperature, humidity and light, separated by commas — for example “120,23.5,45,720”. This is CSV format, and you can copy the lines from the Serial Monitor into a spreadsheet to draw a graph of the day’s weather. Use the millis() timer to take a reading every 10 seconds, not every loop, so the log stays readable.
The timestamp is the time since the board started, in seconds: millis() / 1000. It is not the time of day — the board has no clock — but it is enough to show change over time, which is what a weather log is for. If you need real clock time, add a real-time clock module later; for now, the elapsed time is the right tool.
Stage 4: the enclosure and the test
Put the station in an enclosure: a small box with holes for the sensors, the LCD visible through a cut-out, and the USB cable coming out the back. The sensors must be exposed to the air — a temperature sensor inside a sealed box measures the box, not the room — so place the DHT22 near a vent and the LDR where light can reach it. Keep the electronics away from direct sunlight, which heats the box and skews the temperature.
Worked example — the main loop
The loop uses the millis() pattern from Lesson 11: every 10 seconds it reads the sensors, updates the display if values changed, and prints a CSV line. Nothing blocks, so the display stays responsive.
unsigned long lastRead = 0;
void loop() {
if (millis() - lastRead >= 10000) {
lastRead = millis();
float t = dht.readTemperature();
float h = dht.readHumidity();
int light = analogRead(A0);
showOnLCD(t, h, light);
Serial.print(millis() / 1000); Serial.print(",");
Serial.print(t); Serial.print(",");
Serial.print(h); Serial.print(",");
Serial.println(light);
}
}
Common mistakes
- Reading the DHT22 faster than once every two seconds. It is a slow sensor and returns errors if rushed.
- Putting the temperature sensor inside a sealed box, so it measures the box instead of the room.
- Logging every loop, producing thousands of lines. Log on a timer — every 10 seconds is plenty.
- Recording “nan” when a sensor read fails, instead of checking and skipping the bad reading.
Discussion questions
- Your station reads 25 °C but a thermometer in the same room reads 22 °C. Is the sensor wrong, or is something else going on? How would you find out?
- The log shows the temperature rising every afternoon. What could cause that, besides the weather?
- What would you add to this station to make it a better weather station — and what would each addition cost in power, pins or complexity?
Practice questions
- Why must the DHT22 be read no faster than once every two seconds? Answer: It is a slow sensor: it needs time to take a measurement. Reading it faster returns errors or “nan”. Explanation: Slow sensors need time.
- What is CSV format, and why is it useful for logging? Answer: Values separated by commas, one reading per line. A spreadsheet can open it directly and draw graphs. Explanation: CSV is spreadsheet-ready.
- Why should the temperature sensor be near a vent in the enclosure? Answer: So it measures the room’s air, not the warm air trapped inside the box. A sealed box measures itself. Explanation: Sensors must see the world.
- Your log shows “nan” for temperature. What should the code do? Answer: Check with isnan() and skip the log line. Recording “nan” pollutes the log with a meaningless value. Explanation: Skip bad readings.
- Why log every 10 seconds rather than every loop? Answer: Every loop would produce thousands of lines and flood the serial connection. Ten seconds is frequent enough to show change and keeps the log readable. Explanation: Log on a timer.
🎬 Watch & Learn
One free video that works through this topic step by step:
✅ Check your understanding
Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.