数据未保存在Arduino SD中

发布于 2025-01-26 09:13:16 字数 913 浏览 4 评论 0原文

我创建了一个代码,该代码获取温度测量数据并将其保存在变量中。现在,我想将数据保存在连接到Arduino的SD卡中的.txt文件中。 SD的初始化有效,但是当它设法访问文件时,会给我错误,给我写一组奇怪的符号,而不是文件名,并且不会在文件中写入。我尝试了一个非常相似的代码来在SD内部编写,并且效果很好。可能是什么问题?我将写作代码的一部分封闭在SD上,并将照片与输出一起。 初始化部分:

Serial.println(F("Intializing SD card"));
if(!SD.begin(4))
{
  Serial.print(F("Initialization failed"));
  while(!SD.begin(4))
  {
    Serial.print(F("."));
    delay(1000);
  }
}
else Serial.println(F("Initialization done"));

写作部分:

sdFile = SD.open("records.txt",FILE_WRITE);
Serial.print("Writing to ");
Serial.println(sdFile.name());
sdFile.print(" Temperature: ");
sdFile.print(tempC);
sdFile.print(" taken at: ");
sdFile.print(hour());
sdFile.print(":");
sdFile.print(minute());
sdFile.print(":");
sdFile.println(second());

sdFile.close();

带有奇怪输出的图像

I have created a code that takes temperature measurement data and saves it in a variable. Now I would like to save the data in a .txt file inside an SD card connected to Arduino. The initialization of the SD works, but when it manages to access the file it gives me errors, writing me a set of strange symbols instead of the file name and it doesn't write inside the file. I tried a very similar code to write inside the SD and it works fine. What problem can it be? I enclose the part of the writing code on the SD and the photo with the output.
Initialization part:

Serial.println(F("Intializing SD card"));
if(!SD.begin(4))
{
  Serial.print(F("Initialization failed"));
  while(!SD.begin(4))
  {
    Serial.print(F("."));
    delay(1000);
  }
}
else Serial.println(F("Initialization done"));

Writing part:

sdFile = SD.open("records.txt",FILE_WRITE);
Serial.print("Writing to ");
Serial.println(sdFile.name());
sdFile.print(" Temperature: ");
sdFile.print(tempC);
sdFile.print(" taken at: ");
sdFile.print(hour());
sdFile.print(":");
sdFile.print(minute());
sdFile.print(":");
sdFile.println(second());

sdFile.close();

Image with the strange output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日落 2025-02-02 09:13:16

我怀疑您是否正在打开SD卡,然后才有机会初始化,解决此问题,在开始时实现检查:

if(!SD.begin(8)){ // Here, 8 is the CS (chip select) pin of your SD card.
Serial.println("initialization failed!");
    while (1);
}

sdFile = SD.open("records.txt",FILE_WRITE);

if(myFile){ // If we opened our file successfully!
// Write to our card here

}
sdFile.close();

这是否可以解决您的问题?另外,请注意尺寸限制,以了解Arduino可以读取SD卡的大小。对于全尺寸卡来说,这通常是2GB,而“微型”卡的16GB。

可以在Arduino网站上找到其他文档,以及示例在这里

I suspect you are opening the SD card before it has a chance to initialise, to fix this, implement a check at the start like so:

if(!SD.begin(8)){ // Here, 8 is the CS (chip select) pin of your SD card.
Serial.println("initialization failed!");
    while (1);
}

sdFile = SD.open("records.txt",FILE_WRITE);

if(myFile){ // If we opened our file successfully!
// Write to our card here

}
sdFile.close();

Does this fix your issue? Also, watch out for size limitations on how large the Arduino can read SD cards. This is usually 2GB for full-size cards, and 16GB for "micro" cards.

Additional documentation can be found on the arduino website, as well as examples here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文