FluxBench.AIShared project · read-only

SD Card Data Logger

ESP32 DevKit (WROOM-32)·Arduino C++·Updated Jul 27, 2026·by FluxBench TeamIncludes Lab + Schematic + PCB

Temperature/humidity logger: a DHT22 sampled every 2 seconds, appended as CSV to a MicroSD card over SPI, with a status LED blip per sample and periodic file read-back on the serial monitor. The simulator models the SD card as a real in-memory filesystem — watch /datalog.csv grow. Bundled Lab bench, schematic and routed PCB included.

sketch.ino
// SD Card Data Logger — DHT22 → MicroSD over VSPI
// Wiring (matches the Lab bench, Schematic and PCB tabs):
//   MicroSD module: CS=GPIO5, MOSI=GPIO23, CLK=GPIO18, MISO=GPIO19
//   DHT22 on GPIO4 (10 k pull-up to 3V3), status LED on GPIO2
// Every 2 s a sample is appended to /datalog.csv; every 5th sample the
// whole file is read back over serial so you can watch the log grow.

#include <SPI.h>
#include <SD.h>
#include "DHT.h"

#define DHT_PIN 4
#define SD_CS 5
#define LED_PIN 2

DHT dht(DHT_PIN, DHT22);
int sample = 0;

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  dht.begin();
  if (!SD.begin(SD_CS)) {
    Serial.println("SD init failed — check wiring!");
    while (true) delay(1000);
  }
  Serial.println("SD card ready");
  File f = SD.open("/datalog.csv", FILE_WRITE);  // fresh log with a CSV header
  f.println("millis,temp_c,humidity_pct");
  f.close();
  Serial.println("Logging to /datalog.csv every 2 s");
}

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

  File f = SD.open("/datalog.csv", FILE_APPEND);
  f.print(millis());
  f.print(",");
  f.print(t, 1);
  f.print(",");
  f.println(h, 1);
  f.close();

  digitalWrite(LED_PIN, HIGH);  // blink = sample written
  delay(60);
  digitalWrite(LED_PIN, LOW);

  sample++;
  Serial.print("sample ");
  Serial.print(sample);
  Serial.print(": ");
  Serial.print(t, 1);
  Serial.print(" C  ");
  Serial.print(h, 1);
  Serial.println(" %");

  if (sample % 5 == 0) {  // periodic read-back
    File r = SD.open("/datalog.csv");
    Serial.print("---- /datalog.csv (");
    Serial.print(r.size());
    Serial.println(" bytes) ----");
    while (r.available()) Serial.write(r.read());
    r.close();
    Serial.println("---- end ----");
  }

  delay(2000);
}

Built with FluxBench.AI — the AI workbench for embedded projects.