You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino SD Card模块与RTC:无法写入带时间日志及日志显示求助

Hey there! Let's fix that empty log file issue you're hitting with your Arduino RTC + SD Card project. I’ve dealt with this exact problem a few times, so here’s what to check and how to get your logs showing up properly:

Common Causes & Fixes

1. You’re not flushing/closing the file after writing

Arduino buffers write operations to save SD card wear, which means your data might be stuck in memory instead of being saved to the card. Always add these lines after writing:

logFile.flush(); // Force buffered data to write to the SD card
logFile.close(); // Closing the file also triggers an automatic flush

Skipping this step is the #1 reason for empty log files, especially with small amounts of data.

2. Incorrect file open mode

Make sure you’re opening the file in write mode (FILE_WRITE), not the default read-only mode. If you use SD.open("log.txt") without specifying the mode, writes will fail silently. Here’s the correct way to open and validate the file:

File logFile = SD.open("log.txt", FILE_WRITE);
if (!logFile) {
  Serial.println("Failed to open log file for writing");
  return; // Exit the function if we can't write
}

Always check if the file opened successfully—this will help you catch wiring or card issues early.

3. SD Card Wiring/Power Issues

Even if reading works, writing requires more stable power and proper connections. Double-check:

  • Your CS (chip select) pin matches the one in your code (most modules use D10, but some use D4 or others)
  • The SD card module is getting enough power—use external power instead of relying on Arduino’s 5V/3.3V pins if possible
  • The card is properly inserted, formatted to FAT32, and not write-protected (try a known-working small-capacity card, like 8GB or less)

4. You’re trying to read the file without closing it first

If you’re trying to display the log right after writing, you need to close the file first, then reopen it for reading. Here’s how:

// Write to the file
logFile.println("RTC data: 2024/05/20 14:30:00");
logFile.close(); // Critical step to save data

// Now read and display the log
File readFile = SD.open("log.txt");
if (readFile) {
  Serial.println("Log content:");
  while (readFile.available()) {
    Serial.write(readFile.read()); // Print each byte to serial monitor
  }
  readFile.close();
} else {
  Serial.println("Failed to open log for reading");
}

5. RTC data is empty (so you’re writing nothing)

If your log is supposed to include RTC time/date, first confirm the RTC is working. Add a serial print of the RTC values before writing:

DateTime now = rtc.now();
Serial.print("Current RTC time: ");
Serial.print(now.year());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.print(now.day());
Serial.print(" ");
Serial.println(now.hour());

If these values are 0 or blank, your RTC module isn’t providing valid data—fix that first before worrying about SD card writes.

Full Working Example Code

Here’s a complete snippet combining RTC (DS3231) and SD card write/read functionality:

#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;
const int chipSelect = 10; // Adjust to your SD module's CS pin

void setup() {
  Serial.begin(9600);
  
  // Initialize RTC
  if (!rtc.begin()) {
    Serial.println("Couldn't detect RTC module!");
    while (1); // Halt if RTC fails
  }
  
  // Initialize SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed!");
    while (1); // Halt if SD card fails
  }
  Serial.println("SD card ready");
}

void loop() {
  DateTime now = rtc.now();
  
  // Write RTC data to log
  File logFile = SD.open("log.txt", FILE_WRITE);
  if (logFile) {
    // Format time/date for readability
    logFile.print(now.year(), DEC);
    logFile.print('/');
    logFile.print(now.month(), DEC);
    logFile.print('/');
    logFile.print(now.day(), DEC);
    logFile.print(' ');
    logFile.print(now.hour(), DEC);
    logFile.print(':');
    logFile.print(now.minute(), DEC);
    logFile.print(':');
    logFile.println(now.second(), DEC);
    
    logFile.close();
    Serial.println("Data written to log.txt");
  } else {
    Serial.println("Failed to open log for writing");
  }
  
  // Read and display log content
  delay(1000);
  Serial.println("\n--- Current Log Content ---");
  File readFile = SD.open("log.txt");
  if (readFile) {
    while (readFile.available()) {
      Serial.write(readFile.read());
    }
    readFile.close();
  } else {
    Serial.println("Failed to open log for reading");
  }
  
  delay(5000); // Wait 5 seconds before next write
}
Final Troubleshooting Steps
  1. Check the serial monitor for error messages—they’ll tell you if the file failed to open or the SD card isn’t detected
  2. After writing, eject the SD card and check it on your computer to confirm the data is saved (sometimes Arduino’s read function can have cache quirks)
  3. Make sure your SD card is formatted to FAT32 (exFAT or NTFS won’t work with Arduino’s SD library)

内容的提问来源于stack exchange,提问作者armando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:28:35