SD card data logging lab
Log readings with timestamps to a CSV file on SD, then open it in a spreadsheet.
Build it step by step
0 of 8 donePrepare the card
Format as FAT32 on a computer.
Wire the SD module
Connect the SPI lines and CS to D10.
Test the card
Run the CardInfo example from the SD library.
Add the RTC
Set the time once with the RTClib example.
Add the sensor
Wire the DHT22 with its pull-up.
Upload the sketch
Watch lines appear on the serial monitor.
Check the file
Remove the card and open LOG.CSV on a computer.
Chart it
Plot the readings in a spreadsheet.
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
- Development BoardsBuyArduino Uno R3 × 1
- InterfacesBuy
- SBC AccessoriesBuy
- SensorsBuy
- SensorsBuyDHT22 Sensor × 1
- PassivesBuyResistor 10 kΩ × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| SD module VCC | Uno 5V | Power |
| SD module GND | Uno GND | Ground |
| SD module CS | Uno D10 | Chip select |
| SD module MOSI | Uno D11 | SPI data to the card |
| SD module MISO | Uno D12 | SPI data from the card |
| SD module SCK | Uno D13 | SPI clock |
| DS3231 VCC | Uno 5V | Power |
| DS3231 GND | Uno GND | Ground |
| DS3231 SDA | Uno A4 | I2C data |
| DS3231 SCL | Uno A5 | I2C clock |
| DHT22 data | Uno D2 and 10 kΩ resistor to 5V | Sensor signal |
| DHT22 VCC and GND | Uno 5V and GND | Power |
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.
| Check | Expected |
|---|---|
| Power up | Logging message |
| Every ten seconds | New line on serial and on the card |
| Card removed and read | CSV with times and values |
| Power cut during logging | Earlier 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 →
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.
