πŸ”§ Project: Smart Temperature and Humidity Logger with OLED Display and SD Card Storage
πŸ”§ Project: Smart Temperature and Humidity Logger with OLED Display and SD Card Storage

πŸ”§ Project: Smart Temperature and Humidity Logger with OLED Display and SD Card Storage

πŸ“Œ 1. Introduction

In this hands-on project, you will build a smart data logger that reads temperature and humidity from a DHT22 sensor, displays them on a 0.96-inch OLED screen, and logs the data onto an SD card with real-time timestamps using a DS3231 RTC module.

This kind of system is ideal for greenhouses, indoor air monitoring, server rooms, or any environment where tracking climate over time is essential.


🎯 2. Learning Objectives

By completing this project, you’ll develop key embedded systems skills:

  • Reading environmental data using digital sensors.
  • Displaying real-time data using I2C OLED screens.
  • Logging data with timestamps on an SD card.
  • Using a Real-Time Clock (RTC) for accurate timing.
  • Writing modular and efficient Arduino code.

🧰 3. Components and Tools

You will need the following:

  • One Arduino Uno or Nano board
  • One DHT22 temperature and humidity sensor
  • One 0.96-inch OLED display (I2C interface)
  • One SD card module
  • One microSD card (formatted to FAT32)
  • One DS3231 RTC module
  • One 10kΞ© resistor (for DHT22 pull-up)
  • Breadboard and jumper wires
  • A USB cable and a computer with Arduino IDE installed

πŸ“š 4. Key Concepts and Definitions

DHT22 Sensor – A digital sensor that provides precise humidity and temperature readings.

OLED Display – A small, energy-efficient screen used to display real-time data. It communicates via the I2C protocol, which requires only two data lines.

SD Card Module – Allows Arduino to store and read data from a microSD card.

RTC (DS3231) – Maintains accurate date and time, even when power is lost. Useful for time-stamping data.

I2C (Inter-Integrated Circuit) – A communication protocol where multiple devices share the same two communication lines (SDA and SCL).


πŸ› οΈ 5. Step-by-Step Assembly Guide

A. Wiring Instructions

  1. Connect the DHT22 Sensor:
    • VCC to Arduino 5V
    • GND to Arduino GND
    • DATA to Arduino digital pin D2
    • Add a 10kΞ© resistor between VCC and DATA as a pull-up resistor
  2. Connect the OLED Display (I2C):
    • VCC to 5V
    • GND to GND
    • SDA to A4
    • SCL to A5
  3. Connect the SD Card Module:
    • VCC to 5V
    • GND to GND
    • MISO to D12
    • MOSI to D11
    • SCK to D13
    • CS to D10
  4. Connect the DS3231 RTC Module:
    • VCC to 5V
    • GND to GND
    • SDA to A4
    • SCL to A5

Ensure your connections are tight and double-check wire positions before powering the circuit.


B. Install Required Libraries in Arduino IDE

Open the Library Manager (Sketch > Include Library > Manage Libraries) and install these:

  • DHT sensor library (by Adafruit)
  • Adafruit Unified Sensor
  • Adafruit SSD1306
  • Adafruit GFX
  • RTClib (by Adafruit)
  • SD (pre-installed)

C. Upload the Source Code

#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <SD.h>

#define DHTPIN 2
#define DHTTYPE DHT22
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SD_CS 10

DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;

File dataFile;

void setup() {
  Serial.begin(9600);
  dht.begin();

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED initialization failed");
    while (true);
  }

  if (!rtc.begin()) {
    Serial.println("RTC not detected");
    while (true);
  }

  if (!SD.begin(SD_CS)) {
    Serial.println("SD card initialization failed");
    while (true);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  DateTime now = rtc.now();

  if (isnan(h) || isnan(t)) {
    Serial.println("Sensor error");
    return;
  }

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Temp: "); display.print(t); display.println(" C");
  display.print("Humidity: "); display.print(h); display.println(" %");
  display.display();

  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print(now.timestamp());
    dataFile.print(" - Temp: "); dataFile.print(t);
    dataFile.print(" C, Hum: "); dataFile.println(h);
    dataFile.close();
  } else {
    Serial.println("Error opening file");
  }

  delay(5000);
}

πŸ§ͺ 6. Testing and Debugging Tips

  • If readings are not showing, check the wiring and ensure libraries are installed.
  • Use the Serial Monitor to print debug messages for each component.
  • Make sure your SD card is formatted to FAT32.
  • Use known-good sensors and test components separately if needed.
  • If the time is incorrect, set the RTC using the examples in the RTClib library.

πŸš€ 7. Extensions and Improvements

Here are ideas to upgrade your project:

  • Add Wi-Fi (e.g., ESP8266/ESP32) to upload data to the cloud.
  • Implement button navigation to browse historical readings on the OLED.
  • Add a buzzer or RGB LED for environmental warnings (e.g., high temperature).
  • Convert it into a portable, battery-powered device.