Arduino Micro SD卡模块使用指南

本章节将介绍如何使用Arduino  Micro SD卡模块

一、前期准备:

1、准备好Arduino开发板、SD卡模块(本实验使用的是spi接口)、Micro SD卡以及读卡器

2、安装本项目依赖库SD.h

      依次打开 :“项目-->加载库-->管理库” 后如图1所示,安装SD卡库文件

图1—安装SD卡库文件


3、加载完SD卡相关的库文件,我们会发现示例中多了SD选项,打开示例中的代码,按照注释内容连接SD卡模块,如下图所示:


* SD card attached to SPI bus as follows:

** MOSI - pin 11

** MISO - pin 12

** CLK - pin 13

** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)


图2

4、将sd卡通过读卡器接入电脑,创建文本文件test.txt

5、将sd卡插入sd卡模块,准备工作完成

二、烧写程序(这里使用官方实例代码)


(''')

/*

  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 (for MKRZero SD: SDCARD_SS_PIN)

created  Nov 2010

by David A. Mellis

modified 9 Apr 2012

by Tom Igoe

This example code is in the public domain.

*/

#include <SPI.h>

#include <SD.h>

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!");

    while (1);

  }

  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

}

(''')

如果上述操作都顺利进行,你将在你的窗口监视器中看到如下数据


感谢您的观看,希望我的文章在万千文章中给你带来些许帮助!!!

作者:呆呆的院长

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • 我想给私服加上一个有趣的3点注释:所有的私服都是盗版;大多数私服都是借助情怀维生;有极少数私服只为实现粉丝的情怀而...
    张书乐阅读 1,096评论 0 49
  • 2018年5月16日,育红小学第二学期赛课活动紧张有序地进行。上午安排的是语文组赛课,下午安排的是数学组赛...
    洋钉儿阅读 569评论 0 1
  • 愿你走出半生,归来仍是少年。 这是一个关于爱与成长的故事。曾经有人跟我说,当你爱上了一个对的人,就会觉得他为你打开...
    浆白阅读 169评论 0 0