Projects / SD card data logging lab

SD card data logging lab

Log readings with timestamps to a CSV file on SD, then open it in a spreadsheet.

Beginner45 minGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNOMICROSSANDIS

    Prepare the card

    Format as FAT32 on a computer.

  2. UNO

    Wire the SD module

    Connect the SPI lines and CS to D10.

  3. UNOMICROSSANDIS

    Test the card

    Run the CardInfo example from the SD library.

  4. UNODS323120:10

    Add the RTC

    Set the time once with the RTClib example.

  5. UNODHT2228.7°C

    Add the sensor

    Wire the DHT22 with its pull-up.

  6. UNO

    Upload the sketch

    Watch lines appear on the serial monitor.

  7. UNOMICROSSANDIS

    Check the file

    Remove the card and open LOG.CSV on a computer.

  8. UNO

    Chart it

    Plot the readings in a spreadsheet.

In short

How do you build an SD card data logging lab?

Log readings with timestamps to a CSV file on SD, then open it in a spreadsheet. You need Arduino Uno R3, MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10), DS3231 RTC module, DHT22 Sensor, Resistor 10 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 45 minutes.

Level
Beginner
Time
About 45 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
SD module VCCUno 5VPower
SD module GNDUno GNDGround
SD module CSUno D10Chip select
SD module MOSIUno D11SPI data to the card
SD module MISOUno D12SPI data from the card
SD module SCKUno D13SPI clock
DS3231 VCCUno 5VPower
DS3231 GNDUno GNDGround
DS3231 SDAUno A4I2C data
DS3231 SCLUno A5I2C clock
DHT22 dataUno D2 and 10 kΩ resistor to 5VSensor signal
DHT22 VCC and GNDUno 5V and GNDPower

Code

Format the card as FAT32 (a card of 32 GB or less is easiest). Set the RTC once with an example sketch from the RTClib library so the timestamps are correct. Each reading opens, appends to and closes the file; closing is what makes the data safe if the power is cut. After a while, take the card out and open LOG.CSV in a spreadsheet and draw a chart of temperature and humidity against time. The module has a level shifter, so a 5 V Uno works with it. Arduino IDE, board "Arduino Uno" (or "Arduino Nano" with processor ATmega328P).

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Libraries: SD (built in), RTClib (Adafruit), DHT sensor library (Adafruit).
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <DHT.h>

const byte CS = 10;
RTC_DS3231 rtc;
DHT dht(2, DHT22);

void setup() {
  Serial.begin(9600);
  dht.begin();
  if (!rtc.begin()) { Serial.println("RTC not found"); while (1) {} }
  if (!SD.begin(CS)) { Serial.println("SD card failed"); while (1) {} }
  if (!SD.exists("LOG.CSV")) {
    File f = SD.open("LOG.CSV", FILE_WRITE);
    f.println("time,temperature_c,humidity_pct");
    f.close();
  }
  Serial.println("Logging");
}

void loop() {
  DateTime n = rtc.now();
  float t = dht.readTemperature(), h = dht.readHumidity();
  if (!isnan(t) && !isnan(h)) {
    char ts[20];
    sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d", n.year(), n.month(), n.day(), n.hour(), n.minute(), n.second());
    File f = SD.open("LOG.CSV", FILE_WRITE);
    if (f) {
      f.print(ts); f.print(",");
      f.print(t, 1); f.print(",");
      f.println(h, 1);
      f.close();                       // closing writes the data to the card, so power loss loses little
      Serial.print(ts); Serial.print("  "); Serial.print(t, 1); Serial.print(" C  "); Serial.println(h, 1);
    } else {
      Serial.println("Could not open file");
    }
  }
  delay(10000);                        // every 10 seconds
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

RTC not found

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power upLogging message
Every ten secondsNew line on serial and on the card
Card removed and readCSV with times and values
Power cut during loggingEarlier lines are still there

If it does not work

SD card failed

Check the wiring, FAT32 format and CS pin 10.

RTC not found

Check A4 and A5 and the module power.

Times wrong

Set the RTC with the library example.

NaN readings

Check the DHT22 pull-up resistor.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the SD card data logging lab?

Arduino Uno R3, MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10), DS3231 RTC module, DHT22 Sensor, Resistor 10 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the SD card data logging lab take to build?

About 45 minutes for a beginner maker, in 8 steps.

How do I know the SD card data logging lab works?

Power up: Logging message. Every ten seconds: New line on serial and on the card. Card removed and read: CSV with times and values.

SD card failed: what should I check?

Check the wiring, FAT32 format and CS pin 10.

RTC not found: what should I check?

Check A4 and A5 and the module power.

Times wrong: what should I check?

Set the RTC with the library example.

NaN readings: what should I check?

Check the DHT22 pull-up resistor.