如何将SD卡模块与NodeMCU连接

时间:2019-12-18 07:52:38

标签: sd-card arduino-ide nodemcu

这是我用于将sdcard模块与nodemcu连接以整理文件列表的代码。

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

File root;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(SS)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
}

void loop() {
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    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.print(entry.size(), DEC);
      Serial.print("\t\t");
      time_t ft = entry.getLastWrite();
      struct tm *tm = localtime(&ft);
      // US format.  Feel free to convert to your own locale...
      Serial.printf("%02d-%02d-%02d %02d:%02d:%02d\n", tm->tm_mon + 1, tm->tm_mday, tm->tm_year % 100, tm->tm_hour, tm->tm_min, tm->tm_sec);
    }
    entry.close();
  }
}

我正在使用的Pinout是 Vcc转Vcc GND至GND MISO至D6 MOSI转D7 SCK转D5 CS到D8

但这是说串行输出中的初始化错误。 这里有什么问题?

0 个答案:

没有答案