SD Card Data Logger Build
Hardware
- Arduino Uno/Nano/Mega
- SD card module (SPI interface — cheap, widely available)
- A real-time clock module (DS3231 — much more accurate than the cheaper DS1307) if you want timestamped logs
- Whatever sensor you're logging (temp/humidity, light, voltage, etc.)
Wiring (SPI)
- SD module CS → pin 10 (or any digital pin, configurable in code)
- SD module MOSI → pin 11
- SD module MISO → pin 12
- SD module SCK → pin 13
- SD module VCC/GND → 5V/GND (check your module — some need 3.3V, frying a 5V-intolerant module is a common mistake)
Basic Logging Code
#include <SPI.h> #include <SD.h> const int chipSelect = 10; void setup() { Serial.begin(9600); if (!SD.begin(chipSelect)) { Serial.println("SD init failed"); while (1); // halt — no point continuing without storage } } void loop() { File dataFile = SD.open("log.csv", FILE_WRITE); if (dataFile) { float sensorValue = analogRead(A0) * (5.0 / 1023.0); dataFile.print(millis()); dataFile.print(","); dataFile.println(sensorValue); dataFile.close(); } delay(1000); }Adding Real Timestamps (DS3231)
#include <RTClib.h> RTC_DS3231 rtc; void setup() { rtc.begin(); // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // uncomment once to set clock } // in loop(), before writing: DateTime now = rtc.now(); dataFile.print(now.timestamp()); dataFile.print(",");Run the adjust() line once with your computer connected to set the clock, then comment it back out — the DS3231's onboard coin cell battery keeps time afterward even with the Arduino powered off.
Power Considerations for Long-Term Logging
If this is running unattended for days/weeks, a few things matter: use SD.open/close per write rather than holding the file open (reduces data loss risk on unexpected power loss), and consider putting the Arduino into sleep mode between readings if you're on battery power (the LowPower library handles this cleanly) to extend runtime significantly.
Reading the Data Back
Pull the SD card, the resulting log.csv opens directly in Excel/Sheets/any CSV tool — no special software needed, which is the whole appeal of this approach over something requiring live network connectivity.
Related Guides
- I2C vs SPI vs UART: How to Choose and Use Serial Communication Protocols
- How to Use Sensors with Arduino and ESP32: Temperature, Distance, Load, Current, and Hall Effect
- How to Program Addressable LED Strips: WS2812B Patterns, Effects, and Power Design
- How to Use the ESP32-CAM: Video Streaming, Motion Detection, and Time-Lapse
- How to Control Motors with Arduino and ESP32: Stepper, DC, and Servo Drivers
- How to Use Advanced Anyscan A30M Workflows: CAN FD, FCA AutoAuth, Logging, and Reports
- ESP32: Setting Up for Arduino IDE
- Automated Plant Watering System with Raspberry Pi