Lab

SD 記憶卡

[材料]

Arduino主板 x 1

麵包板 x 1

SD模組 x 1 (SD卡模組有兩排插槽,建議兩排都插滿)

2g以內SD卡 x 1 (SD卡先格式化成fat格式)

SD卡寫入、讀取


/*
  SD card test

 要注意SD記憶卡不同、SD卡模組不同、arduino主機板不同,都有可能造成錯誤訊息

  * 由上而下
  * GND - 可接可不接
  * +3 - 不接
  * +5 - pin +5
  * CS   - pin  4   (42)
  * MOSI - pin 11   (51)
  * SCK  - pin 13   (52)
  * MISO - pin 12   (50)
  * GND - pin GND
*/

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

File myFile;

void setup() {
  pinMode(10,OUTPUT);    //保留pin10, SD Library需要使用

  // 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
}


SD檢測


/*
  SD card test
 要注意SD記憶卡不同、SD卡模組不同、arduino主機板不同,都有可能造成錯誤訊息

 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:
  * 由上而下
  * GND - 可接可不接
  * +3 - 不接
  * +5 - pin +5
  * CS   - pin  4   (42)
  * MOSI - pin 11   (51)
  * SCK  - pin 13   (52)
  * MISO - pin 12   (50)
  * GND - pin GND
*/

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

// 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() {
  pinMode(10,OUTPUT);    //保留pin10, SD Library需要使用

  // 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) {

}

加入LED燈顯示讀寫狀況,綠燈可以關閉電源,綠燈閃爍時警告,紅燈不能關閉電源。

若隨意關閉電源,則會使檔案損毀,無法讀取。

加入LED顯示讀寫狀況 (綠燈亮:可以關閉電源。綠燈閃爍:警告。紅燈:禁止關閉電源,以免SD卡損毀。)


/*SD卡
CS   --> to Arduino pin4 
SCK  --> to Arduino pin13
MOSI --> to Arduino pin11
MISO --> to Arduino pin12
VCC  --> to Arduino 5V
GND  --> to Arduino GND
*/
#include <SPI.h>
#include <SD.h>
File myFile;
String filename = "test.txt";     //要寫入的檔案名稱
int i = 0;
int ledG = 7; // 綠燈 正極 --> pin 7,  負極 --> GND 
int ledR = 8; // 紅燈 正極 --> pin 8,  負極 --> GND 

void setup() {
  pinMode(ledR, OUTPUT); 
  pinMode(ledG, OUTPUT);
  pinMode(10,OUTPUT);    //保留pin10, SD Library需要使用
  
  Serial.begin(9600);
  while (!Serial) {}

  digitalWrite(ledR, HIGH);
  Serial.print("Initializing SD card...");  //SD卡初始化
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop()
{
  digitalWrite(ledG, HIGH);
  digitalWrite(ledR, LOW);

  /*

   此範圍可以加入其他程式碼


  */

  delay (3000);


  for (int i=0; i <= 10; i++){   //燈號警告,此段時間可以關閉電源、再拔除SD卡。
    digitalWrite(ledG, HIGH);
    delay(100);
    digitalWrite(ledG, LOW);
    delay(100);
  } 
  digitalWrite(ledR, HIGH);     //下面的程式是寫入SD卡,此段時間內請勿關閉電源、拔取SD卡。

  myFile = SD.open(filename, FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.print(millis());                   //寫入SD卡的時間,單位是1/1000秒。
    myFile.print(",");
    
    myFile.print("testing 1, 2, 3");    //要寫入SD卡數據時,這一行請刪掉。
    
    //myFile.print(0);                  //要寫入SD卡數據時,請以逗號分隔數據。
    //myFile.print(",");
    //myFile.print(1);
    //myFile.print(",");
    //myFile.print(2);
    // ...
    myFile.println();

    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  delay (3000);
}