如何使用 Arduino ESP32 将数据存储到 microsdcard(软 SPI )

esp32从传感器记录的数据或者日志,并将这些数据保存到SD卡中

1.我们将使用图书馆SD卡进行通信。您可以在此处下载:

https://github.com/nhatuan84/esp32-micro-sdcard

下载后,解压缩并将其解压缩到Arduino文件夹下的库文件夹中:

2.格式化 microSD 卡:

将 microSD 卡与 ESP32 配合使用时,应先对其进行格式化。

3.接线方式:

[ESP32 IO26 – CS(D3) MICROSD]

[ESP32 IO14 – MOSI(CMD) MICROSD]

[ESP32 IO13 – MISO(D0) MICROSD]

[ESP32 IO27 – SCK(SCLK) MICROSD]

[ESP32 GND – GND MICROSD]

[3.3V – VCC MICROSD]

4.代码以及说明:

- SD.begin(uint8_t cs , int8_t mosi , int8_t miso , int8_t sck): initialize library with SPI pins

- SD.open(filename, FILE_WRITE): open file for writing

- SD.open(filename): open file for reading

- SD.open("/"): open sdcard at root “/”

以下是代码部分:

#include <mySD.h>

ext::File root;
int a = 0;
int b = 0,c=0;

void setup()
{
  Serial.begin(115200);

  Serial.print("Initializing SD card...");
  /* 使用软 SPI 引脚初始化 SD 库,如果使用硬 SPI 替换为这个 SD.begin()*/
  if (!SD.begin(26, 14, 13, 27)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  /* Begin at the root "/" */
  root = SD.open("/");
  if (root) {    
    printDirectory(root, 0);
//    读取sd卡中的文件
    root.close();
  } else {
    Serial.println("error opening test.csv");
  }
  /* open "test.csv" for writing */
  root = SD.open("test.csv", FILE_WRITE);
  /* 如果成功打开 -> root != NULL
    然后写字符串“Hello world!”对它
  */

  if (root) {
    root.println("1,2,3,4,5");

//    root.println("250,255,255,200,100,250,255,255,200,100,250,255,255,200,100");
    root.flush();
   /* 关闭文件 */
    root.close();
  } else {
    /*  如果文件打开错误,打印错误 */
    Serial.println("error opening test.csv");
  }
  delay(1000);
  /* 写入后重新打开文件并读取 */
  root = SD.open("test.csv");
  if (root) {    
    /* 从文件中读取直到其中没有其他内容 */
    while (root.available()) {
      /* 读取文件并打印到终端 */
      Serial.write(root.read());
    }
    root.close();
  } else {
    Serial.println("error opening test.csv");
  }
  
  Serial.println("done!");
}

void loop()
{
  root = SD.open("/");
  if (root) {    
    printDirectory(root, 0);
//    读取sd卡中的文件
    root.close();
  } else {
    Serial.println("error opening test.csv");
  }
  /* open "test.csv" for writing */
  root = SD.open("test.csv", FILE_WRITE);
  /* 如果成功打开 -> root != NULL
    然后写字符串“Hello world!”对它
  */

  if (root) {
//    root.println("1,2,3,4,5");

    root.println("250,255,255,200,100,250,255,255,200,100,250,255,255,200,100,255,255,200,100,250,255,255,200,100,250");
    root.flush();
   /* 关闭文件 */
    root.close();
  } else {
    /*  如果文件打开错误,打印错误 */
    Serial.println("error opening test.csv");
  }
  delay(1000);
  /* 写入后重新打开文件并读取 */
  root = SD.open("test.csv");
  if (root) {    
    /* 从文件中读取直到其中没有其他内容 */
    while (root.available()) {
      /* 读取文件并打印到终端 */
      c=1;
      if (c == 1){
        a = micros();
        }
      Serial.write(root.read());
    }
    b = micros();
    Serial.print(b - a );
    root.close();
  } else {
    Serial.println("error opening test.csv");
  }
  
  Serial.println("done!");
}
void printDirectory(ext::File dir, int numTabs) {
  
  while(true) {
     ext::File entry =  dir.openNextFile();
     if (! entry) {
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');   // we'll have a nice indentation
     }
     // Print the name
     Serial.print(entry.name());
     /*   递归目录,否则打印文件大小 */
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       /*   files have sizes, directories do not */
       Serial.print("\t\t");
       Serial.println(entry.size());
     }
     entry.close();
   }
}

5.打开串口监视器,查看文件已经成功输入:

6.打开csv文件,内容已经写入:

文章参考来源:Demo 7: How to use Arduino ESP32 to store data to microsdcard (Soft SPI and Hard SPI) (iotsharing.com)

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值