Arduino ESP32 第三方库读取SD卡信息
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino ESP32 第三方库读取SD卡信息相关的知识,希望对你有一定的参考价值。
Arduino ESP32 第三方库读取SD卡信息
- 所需库github地址:https://github.com/nhatuan84/esp32-micro-sdcard
如果网页无法加载,将域名改为镜像地址的例如:https://hub.fastgit.org/nhatuan84/esp32-micro-sdcard
- 该库支持软SPI接口和硬SPI接口.如果使用硬SPI接口直接定义:
if (!SD.begin())
Serial.println("initialization failed!");
return;
- 如果是软SPI接口就这样初始化:
if (!SD.begin(26, 14, 13, 27))
Serial.println("initialization failed!");
return;
接线说明
- 软SPI接口接线方式:
Soft SPI
[ESP32 IO26 – CS MICRO SD]
[ESP32 IO14 – MOSI MICRO SD]
[ESP32 IO13 – MISO MICRO SD]
[ESP32 IO27 – SCK MICRO SD]
[ESP32 GND – GND MICRO SD]
[VIN – VCC MICRO SD]
- 硬SPI接口接线方式:
Hard SPI
* MICROSD CS - ESP32 IO5
MICROSD SCK - ESP32 IO18
MICROSD MOSI - ESP32 IO23
MICROSD MISO - ESP32 IO19
MICROSD Vcc - ESP32 VIN
MICROSD GND - ESP32 GND
实例代码一
#include <SPI.h>
#include <mySD.h>
File root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;
void setup()
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on Arduino Uno boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
// pinMode(SS, OUTPUT);
if (!SD.begin(26, 14, 13, 27))
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)
// Begin at the start of the directory
dir.rewindDirectory();
while(true)
File entry = dir.openNextFile();
if (! entry)
// no more files
//Serial.println("**nomorefiles**");
break;
for (uint8_t i=0; i<numTabs; i++)
Serial.print('\\t'); // we'll have a nice indentation
// Print the 8.3 name
Serial.print(entry.name());
// Recurse for directories, otherwise print the file size
if (entry.isDirectory())
Serial.println("/");
printDirectory(entry, numTabs+1);
else
// files have sizes, directories do not
Serial.print("\\t\\t");
Serial.println(entry.size(), DEC);
entry.close();
- 编译信息
使用 1.0 版本的库 SPI 在文件夹: C:\\Users\\Administrator\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.6\\libraries\\SPI
使用库 esp32-micro-sdcard-master 在文件夹: C:\\Program Files (x86)\\Arduino\\libraries\\esp32-micro-sdcard-master (legacy)
"C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\Arduino15\\\\packages\\\\esp32\\\\tools\\\\xtensa-esp32-elf-gcc\\\\1.22.0-97-gc752ad5-5.2.0/bin/xtensa-esp32-elf-size" -A "d:\\\\arduino\\\\MyHexDir/nanosdcard1.ino.elf"
项目使用了 216594 字节,占用了 (16%) 程序存储空间。最大为 1310720 字节。
全局变量使用了14224字节,(4%)的动态内存,余留313456字节局部变量。最大为327680字节。
- 分别测试了一张128MB的TF卡和一张2GB的TF卡都可以,
实例代码一
读取到卡内信息,但是读不全信息。
以上是关于Arduino ESP32 第三方库读取SD卡信息的主要内容,如果未能解决你的问题,请参考以下文章
Arduino ESP32 读取Micro sd卡容量信息示例