Project: Complete Weather Station with LCD Display and SD Card Logging
Build a standalone weather station that measures temperature, humidity, and displays readings on an LCD while logging data to an SD card for later analysis.
Components Needed
| Component | Quantity |
|---|---|
| Arduino Uno (or Mega for more memory) | 1 |
| DHT22 Temperature & Humidity Sensor | 1 |
| LCD 1602 Display with I2C Backpack | 1 |
| Micro SD Card Module | 1 |
| 4.7kΩ resistor | 1 |
| Breadboard | 1 |
| Jumper wires | ~20 |
| Micro SD Card (formatted FAT32) | 1 |
Circuit
DHT22 VCC ────── 5V
DHT22 DATA ────── Pin 2 (with 4.7kΩ pull-up to 5V)
DHT22 GND ────── GND
LCD I2C VCC ────── 5V
LCD I2C GND ────── GND
LCD I2C SDA ────── A4 (SDA on Uno)
LCD I2C SCL ────── A5 (SCL on Uno)
SD Card Module VCC ────── 5V
SD Card Module GND ────── GND
SD Card Module MOSI ────── Pin 11
SD Card Module MISO ────── Pin 12
SD Card Module SCK ────── Pin 13
SD Card Module CS ────── Pin 10
Complete Code
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
// Pin definitions
#define DHTPIN 2
#define DHTTYPE DHT22
#define SD_CS 10
// Initialize objects
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 chars, 2 lines
// Timing
unsigned long lastRead = 0;
unsigned long lastLog = 0;
const unsigned long readInterval = 2000; // Read sensor every 2 seconds
const unsigned long logInterval = 30000; // Log to SD every 30 seconds
// File name with date (simplified - in production use RTC)
char filename[] = "WEATHER.CSV";
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("=== Weather Station Starting ===");
// Initialize DHT22
dht.begin();
Serial.println("DHT22 initialized");
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Weather Station");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
Serial.println("LCD initialized");
// Initialize SD card
if (!SD.begin(SD_CS)) {
Serial.println("SD card initialization failed!");
lcd.setCursor(0, 1);
lcd.print("SD FAIL ");
while (true); // Halt on SD failure
}
Serial.println("SD card initialized");
// Create CSV header if file doesn't exist
if (!SD.exists(filename)) {
File file = SD.open(filename, FILE_WRITE);
if (file) {
file.println("Timestamp,Temperature_C,Temperature_F,Humidity,Heat_Index_C");
file.close();
Serial.println("Created new log file with header");
}
}
lcd.setCursor(0, 0);
lcd.print("Temp: C ");
lcd.setCursor(0, 1);
lcd.print("Hum: % ");
Serial.println("=== Weather Station Ready ===");
}
void loop() {
unsigned long now = millis();
// Read sensor periodically
if (now - lastRead >= readInterval) {
lastRead = now;
readAndDisplaySensor();
}
// Log to SD card periodically
if (now - lastLog >= logInterval) {
lastLog = now;
logToSD();
}
}
void readAndDisplaySensor() {
// Read temperature and humidity
float humidity = dht.readHumidity();
float tempC = dht.readTemperature(); // Celsius
float tempF = dht.readTemperature(true); // Fahrenheit
// Check if reading failed
if (isnan(humidity) || isnan(tempC) || isnan(tempF)) {
Serial.println("Failed to read from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("SENSOR ERROR ");
return;
}
// Compute heat index
float heatIndexC = dht.computeHeatIndex(tempC, humidity, false);
// Display on LCD
lcd.setCursor(6, 0);
lcd.print(tempC, 1);
lcd.print(" C ");
lcd.setCursor(6, 1);
lcd.print(humidity, 1);
lcd.print(" % ");
// Serial output
Serial.print("Temp: ");
Serial.print(tempC);
Serial.print("°C / ");
Serial.print(tempF);
Serial.print("°F Humidity: ");
Serial.print(humidity);
Serial.print("% Heat Index: ");
Serial.print(heatIndexC);
Serial.println("°C");
}
void logToSD() {
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
float tempF = dht.readTemperature(true);
if (isnan(humidity) || isnan(tempC) || isnan(tempF)) {
Serial.println("Failed to read sensor for logging");
return;
}
float heatIndexC = dht.computeHeatIndex(tempC, humidity, false);
// Open file for append
File file = SD.open(filename, FILE_WRITE);
if (!file) {
Serial.println("Failed to open log file");
return;
}
// Write CSV line: timestamp,tempC,tempF,humidity,heatIndex
file.print(millis());
file.print(",");
file.print(tempC, 1);
file.print(",");
file.print(tempF, 1);
file.print(",");
file.print(humidity, 1);
file.print(",");
file.println(heatIndexC, 1);
file.close();
Serial.println("Data logged to SD card");
}Key Concepts
Combining multiple peripherals: This project shows how to manage I2C (LCD), SPI (SD card), and single-wire (DHT22) protocols simultaneously on one Arduino.
Code Breakdown
1. Multi-Protocol Management
- I2C (Wire library): LCD display at address 0x27
- SPI (SPI library): SD card communication
- Single-wire (DHT library): DHT22 timing protocol
2. Non-blocking Timing
Uses millis() instead of delay() to allow concurrent operations:
- Sensor reading every 2 seconds
- SD logging every 30 seconds
- LCD updates immediately after sensor read
3. Data Logging Format
CSV format for easy import into Excel, Python, or databases:
Timestamp,Temperature_C,Temperature_F,Humidity,Heat_Index_C
123456,24.5,76.1,55.2,25.1
153456,24.6,76.3,54.8,25.24. Error Handling
- Sensor read validation with
isnan() - SD card initialization check
- File open verification
Power Management for Standalone Operation
Extended Features to Implement
- Add RTC (DS3231) — Real timestamps instead of
millis() - Add BMP280 — Barometric pressure for weather forecasting
- Add WiFi (ESP32/ESP8266) — Send data to cloud (see IoT modules)
- Add OLED display — Graphics and more data
- Add data visualization — Python script to plot CSV data
Portfolio Checklist
- DHT22 + LCD 1602 I2C on breadboard
- SD card module with data file creation
- Readable serial output every 2 seconds
- CSV log file with proper headers
- LCD updates without flickering
- Git repo with meaningful commits
- README with wiring diagram and build instructions
Resources
- DHT Sensor Library: https://github.com/adafruit/Adafruit_DHT_sensor_library
- LiquidCrystal_I2C Library: https://github.com/johnrickman/LiquidCrystal_I2C
- Arduino SD Library: https://www.arduino.cc/reference/en/libraries/sd/
- Complete Weather Station Tutorial: https://create.arduino.cc/projecthub/10n2l/weather-station-with-lcd-and-sd-card-9a3b7c
- Video: Arduino Weather Station: https://www.youtube.com/watch?v=UqTxbWz_T3M
- SD Card Formatting: https://www.sdcard.org/downloads/formatter/