Arduino Data Logger

    Data logging system with Arduino and sensors

    Circuit Description

    This Arduino-based data logger system records sensor data to an SD card at regular intervals. The circuit features temperature and humidity monitoring via a DHT22 sensor, real-time clock functionality for accurate timestamping, and SD card storage for data retention. This system is ideal for environmental monitoring, science experiments, or any application requiring long-term data collection.

    How It Works

    The Arduino Uno forms the core of the system, reading the temperature and humidity values from the DHT22 sensor. The DS3231 RTC module provides accurate time and date information for each reading. Data is formatted as CSV (comma-separated values) and written to the SD card through the SD card module. The system operates on battery power with a voltage regulator to ensure stable operation.

    Circuit Schematic

    Arduino Uno5VGNDD2D10D11D12D13A4/SDAA5/SCLDHT22SensorVCCDATAGND10kΩSD CardModuleSDVCCGNDMOSIMISOSCKCSDS3231 RTCModuleVCCGNDSDASCL9V Battery

    Arduino Code

    #include <SPI.h>
    #include <SD.h>
    #include <Wire.h>
    #include <RTClib.h>
    #include <DHT.h>
    
    #define DHTPIN 2      // DHT22 data pin
    #define DHTTYPE DHT22 // DHT sensor type
    #define SDPIN 10      // SD card CS pin
    
    DHT dht(DHTPIN, DHTTYPE);
    RTC_DS3231 rtc;
    File dataFile;
    
    void setup() {
      Serial.begin(9600);
      
      // Initialize DHT sensor
      dht.begin();
      
      // Initialize RTC
      if (!rtc.begin()) {
        Serial.println("Couldn't find RTC");
        while (1);
      }
      
      // Set RTC time if needed (uncomment and set once)
      // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
      
      // Initialize SD card
      Serial.print("Initializing SD card...");
      if (!SD.begin(SDPIN)) {
        Serial.println("initialization failed!");
        while (1);
      }
      Serial.println("initialization done.");
      
      // Create or open data file and write header
      dataFile = SD.open("datalog.csv", FILE_WRITE);
      if (dataFile) {
        if (dataFile.size() == 0) {
          dataFile.println("Date,Time,Temperature (C),Humidity (%)");
        }
        dataFile.close();
      }
    }
    
    void loop() {
      // Get current time
      DateTime now = rtc.now();
      
      // Read temperature and humidity
      float temp = dht.readTemperature();
      float hum = dht.readHumidity();
      
      // Check if reading was successful
      if (isnan(temp) || isnan(hum)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
      
      // Format date and time strings
      char dateStr[11];
      sprintf(dateStr, "%04d/%02d/%02d", now.year(), now.month(), now.day());
      
      char timeStr[9];
      sprintf(timeStr, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
      
      // Open file and write data
      dataFile = SD.open("datalog.csv", FILE_WRITE);
      if (dataFile) {
        dataFile.print(dateStr);
        dataFile.print(",");
        dataFile.print(timeStr);
        dataFile.print(",");
        dataFile.print(temp);
        dataFile.print(",");
        dataFile.println(hum);
        dataFile.close();
        
        // Print to serial monitor
        Serial.print(dateStr);
        Serial.print(" ");
        Serial.print(timeStr);
        Serial.print(" - Temp: ");
        Serial.print(temp);
        Serial.print("°C, Humidity: ");
        Serial.print(hum);
        Serial.println("%");
      } else {
        Serial.println("Error opening datalog.csv");
      }
      
      // Wait 5 minutes before next reading
      delay(300000); // 5 minutes in milliseconds
    }

    Components List

    Arduino Uno or compatible board1x
    DHT22 temperature and humidity sensor1x
    DS3231 RTC (Real-Time Clock) module1x
    SD card module1x
    microSD card (formatted as FAT16 or FAT32)1x
    10kΩ resistor (pull-up for DHT22)1x
    9V battery1x
    Battery connector1x
    Breadboard or PCB1x
    Jumper wires1 set
    Project enclosure (Optional)1x

    Required Libraries

    • SD.h - For SD card operations (included with Arduino IDE)
    • SPI.h - For SPI communication (included with Arduino IDE)
    • Wire.h - For I2C communication (included with Arduino IDE)
    • RTClib.h - For DS3231 RTC module (by Adafruit)
    • DHT.h - For DHT22 sensor (Adafruit DHT sensor library)

    Installation Instructions

    1. Install the required libraries using the Arduino Library Manager.
    2. Connect the DHT22 sensor: VCC to Arduino 5V, GND to Arduino GND, and DATA to Arduino D2. Add the 10kΩ pull-up resistor between VCC and DATA.
    3. Connect the SD card module: VCC to Arduino 5V, GND to Arduino GND, MOSI to Arduino D11, MISO to Arduino D12, SCK to Arduino D13, CS to Arduino D10.
    4. Connect the DS3231 RTC module: VCC to Arduino 5V, GND to Arduino GND, SDA to Arduino A4/SDA, SCL to Arduino A5/SCL.
    5. Format the microSD card as FAT16 or FAT32 and insert it into the SD card module.
    6. Connect the 9V battery to power the Arduino.
    7. Upload the provided code to the Arduino.
    8. If this is the first time using the RTC, uncomment the line rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); to set the time, upload once, then recomment and upload again.
    9. Open the Serial Monitor at 9600 baud to verify operation.
    10. The system will now log temperature and humidity readings every 5 minutes to a file named "datalog.csv" on the SD card.

    Customization Options

    • Logging interval: Change the delay(300000) value to adjust how frequently data is recorded (value in milliseconds).
    • Additional sensors: The system can be expanded with other sensors like light, pressure, or soil moisture by adding the appropriate libraries and code.
    • Battery life: Implement sleep mode or use a larger capacity battery for extended operation.
    • Data visualization: Create a simple processing or Python script to visualize the logged data.

    Cookie Consent

    We use cookies to enhance your experience and analyze site traffic. Your privacy is important to us.