TF卡读写储存模块是互动媒体设备最常用存储模块,采用SPI接口。模块可以通过杜邦线与Arduino传感器扩展板连接,编写相应的程序可以实现各种传感器(如温湿度传感器、光线传感器,GPS 等等)数据记录等功能,通过读卡器将TF卡数据读出,便可轻松加以分析利用,模块并带有两个LED灯以指示TF卡工作状态。(指示灯说明:1、当接上电源未插卡时,一个LED常亮,表示模块供电正常。2、插入卡后常亮的LED开始闪速,表示TF卡已经插入。3、另一个LED闪烁是表示正在通信。)
在配合Arduino101的使用中接线方式可以与uno相同:
1、CD:用户可用来检测卡是否插入,不使用可不连接(例程中不使用)
2、CS:TF卡片选,例程中需连接到4号脚(在没有使用其他SPI设备时可不连接)
3、MOSI:连接到arduino的MOSI口,在101上为11号引脚
4、MISO:连接到arduino的MISO口,在101上为12号引脚
5、SCK:连接到arduino的SCLK口,在101上为13号引脚
6、VCC:电源供电正端,连接到5V
7、GND:电源供电负端,连接到电源负极,GND

硬件连接好以后,打开Arduino IDE找到文件-示例-SD-Cardlnfo。

/*
SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples
created 28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
*/
// include the SD library:
#include
#include
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile 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(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}
烧写官方的例程,上传程序后,等待系统启动(大约5s);
打开串口监视器,可以看到Arduino 101返回的信息,此时,如果TF卡里有内容的话,就会返回目录下的文件信息;

需要注意的是,TF或SD卡的文件格式必须为FAT32或FAT16,否则系统无法读取内存卡的信息。
如何读取TF卡里的内容,我们已经得到了掌握,那么接下来我们学习另一个重要的功能——在TF卡里创建一个文件,你并写入内容。
准备好硬件以后,打开ArduinoIDE,找到文件-示例-SD-readWrite,如图;

可以看到如下代码。
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include
#include
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
代码的功能,是在其读到TF卡后,使用SD.open("test.txt", FILE_WRITE);创建了一个名为test.txt的文件夹,随后使用myFile.println("testing 1, 2, 3.");语句向txt文件中写入了testing 1, 2, 3.;
我们下载后,打开串口监视器,可以在屏幕中看到输出的信息;

此时,我们把TF卡取出来插到电脑上验证一下是否有这个文件夹以及文件;

可以看到被创建的test.txt以及里面的内容testing 1,2,3,;
文章来源:Aduino中文社区